webForumDet fria alternativet

Kul, köpt en redan uråldrig PHP bok..

8 svar · 295 visningar · startad av wolfangle

wolfangleMedlem sedan jan. 200214 inlägg
#1

Webbprogrammering med PHP av Viktor Jonsson.

Problemen börjar med sessionerna, det som han skriver funkar inte. Jag har kollat upp detta och sett att PHP har ändrat mycket vad gäller sessioner.

Jag har lyckats komma på hur man nu gör för att hämta variabler ifrån formulär. Med lösningen till nedanstående kod kommer jag inte på. Hjälp! Vad bör ändras?

sida1:

<?php
session_start();
$namn = "Viktor Jonsson";
session_register("namn");
?>
<a href="sida2.php">test</a>

sida2:

<?php
session_start();
echo $namn;

?>

SPiNMedlem sedan mars 20007 896 inlägg
#2

Hm, det ser ju rätt så rätt ut. :)

Testa detta på sida2.php:

session_start ();
echo $HTTP_SESSION_VARS['namn'];

------------------
SPiN, bjorne.w@telia.com

-- Wise men talk because they have something to say; fools, because they have to say something. --

wolfangleMedlem sedan jan. 200214 inlägg
#3

Nej, det där funkar inte.

Jag vet inte om du har nyaste PHP versionen installerad(4.1.1). Men från och med den så verkar dom påstå att man måste göra på ett annat sätt därför att register_globals är avstängd eller nått.

Här har du förklaringen:

While it is quite possible to write secure code in PHP, we felt that the fact that PHP makes it too easy to write insecure code was bad, and we've decided to attempt a far-reaching change, and deprecate register_globals. Obviously, because the vast majority of the PHP code in the world relies on the existence of this feature, we have no plans to actually remove it from PHP anytime in the foreseeable future, but we've decided to encourage people to shut it off whenever possible.

To help users build PHP applications with register_globals being off, we've added several new special variables that can be used instead of the old global variables. There are 7 new special arrays:

$_GET - contains form variables sent through GET
$_POST - contains form variables sent through POST
$_COOKIE - contains HTTP cookie variables
$_SERVER - contains server variables (e.g., REMOTE_ADDR)
$_ENV - contains the environment variables
$_REQUEST - a merge of the GET variables, POST variables and Cookie variables. In other words - all the information that is coming from the user, and that from a security point of view, cannot be trusted.
$_SESSION - contains HTTP variables registered by the session module
Now, other than the fact that these variables contain this special information, they're also special in another way - they're automatically global in any scope. This means that you can access them anywhere, without having to 'global' them first. For example:

<?php
function example1()
{
print $_GET["name"]; // works, 'global $_GET;' is not necessary!
}
?>

would work fine! We hope that this fact would ease the pain in migrating old code to new code a bit, and we're confident it's going to make writing new code easier. Another neat trick is that creating new entries in the $_SESSION array will automatically register them as session variables, as if you called session_register(). This trick is limited to the session module only - for example, setting new entries in $_ENV will *not* perform an implicit putenv().

PHP 4.1.0 still defaults to have register_globals set to on. It's a transitional version, and we encourage application authors, especially public ones which are used by a wide audience, to change their applications to work in an environment where register_globals is set to off. Of course, they should take advantage of the new features supplied in PHP 4.1.0 that make this transition much easier.

As of the next semi-major version of PHP, new installations of PHP will default to having register_globals set to off. No worries! Existing installations, which already have a php.ini file that has register_globals set to on, will not be affected. Only when you install PHP on a brand new machine (typically, if you're a brand new user), will this affect you, and then too - you can turn it on if you choose to.

Note: Some of these arrays had old names, e.g. $HTTP_GET_VARS. These names still work, but we encourage users to switch to the new shorter, and auto-global versions.

Thanks go to Shaun Clowes (shaun@securereality.com.au) for pointing out this problem and for analyzing it.

[Redigerat av wolfangle den 10 jan 2002]

SPiNMedlem sedan mars 20007 896 inlägg
#4

Ah, visste inte att du körde senaste. Testa isf med $_SESSION['namn'] istället för $HTTP_SESSION_VARS['namn']. :)

Red.} Märkte precis:

Note: Some of these arrays had old names, e.g. $HTTP_GET_VARS. These names still work, but we encourage users to switch to the new shorter, and auto-global versions.

------------------
SPiN, bjorne.w@telia.com

-- Wise men talk because they have something to say; fools, because they have to say something. --

[Redigerat av SPiN den 10 jan 2002]

wolfangleMedlem sedan jan. 200214 inlägg
#5

Tro det eller ej, men sida2 är fortfarande helt vit.

SPiNMedlem sedan mars 20007 896 inlägg
#6

Har du ställt in allting rätt i php.ini? Det brukar ju annars generera felmeddelanden om man inte gjort det, men eftersom att sidan är helt vit... *hmm*

Det är en del som brukar sätta session_register ( "namn" ); precis efter session_start ();. Det ska dock inte behövas, men testa ivf. :)

------------------
SPiN, bjorne.w@telia.com

-- Wise men talk because they have something to say; fools, because they have to say something. --

visslarenMedlem sedan juli 20011 222 inlägg
#7

om du kör exemplet på en iis server så kan det vara felet. sessions i php fungerar tydligen inte på iis om man inte har aktiverat session.autostart() som jag har fattat det.

------------------
*GaBlimp*
www.spelbasen.net

vissler/yobiko

wolfangleMedlem sedan jan. 200214 inlägg
#8

OK, jag har aktiverat error funktionen och så här säger den:

Warning: open(/tmp\sess_da107cf601eb32a7335e28075a3d234b, O_RDWR) failed: No such file or directory (2) in C:\none\global.php on line 3

Vad betyder detta och framförallt, hur åtgärdar man problemet.

Tack..

SPiNMedlem sedan mars 20007 896 inlägg
#9

Som jag trodde, du har fel sökväg i din php.ini. :)
Du måste skapa en katalog där dina sessioner kan sparas, och sedan gå in och ändra session.save_path i php.ini till den mappen. Sen är det bara att köra! :)

------------------
SPiN, bjorne.w@telia.com

-- Wise men talk because they have something to say; fools, because they have to say something. --

[Redigerat av SPiN den 14 jan 2002]

134 ms totalt · 3 externa anrop · v20260731065814-full.fb544a5a
0 ms — hämta forumlista (cache)
0 ms — hämta statistik (cache)
131 ms — hämta tråd, inlägg och bilagor (db)