Page 1 of 1

[SOLVED] $_Session takes on unexpected value. Bug?

Posted: Sat Jul 10, 2004 8:03 am
by harryd
Run the following code and see (and please explain to me why this should happen) that $_SESSION['froop'] mysteriously takes on the value of a variable. What is going on here?

Code: Select all

<?php
session_start();
echo phpversion();
echo '<pre>$_SESSION (0): ';
print_r($_SESSION);
echo '</pre>';

$_SESSION['froop']=0;
$froop='strawberry';

// The second time around we see that mysteriously $_SESSION['froop'] has 
// taken on the value 'strawberry'.  Watch the birdie...
echo '<pre>$_SESSION (1): ';
print_r($_SESSION);
echo '</pre>';

// The following condition should NEVER be true as $_SESSION['froop'] 
// is never explicitly set to anything but 0. However...
if(strcmp($_SESSION['froop'],'strawberry')==0)
{
	echo '<pre>$_SESSION (2): ';
	print_r($_SESSION);
	echo '</pre>';

	unset($_SESSION);
	session_destroy();
	echo '<p>Session gone.';
}
else
{
	echo 'This was the first run.  Now do Refresh and see.';
}
?>

feyd | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Sat Jul 10, 2004 8:24 am
by redmonkey
My output with a few different versions...

PHP5...

Code: Select all

5.0.0RC3

$_SESSION (0): Array
(
)

$_SESSION (1): Array
(
    &#1111;froop] => 0
)

This was the first run. Now do Refresh and see.
After refresh...

Code: Select all

5.0.0RC3

$_SESSION (0): Array
(
    &#1111;froop] => 0
)

$_SESSION (1): Array
(
    &#1111;froop] => 0
)

This was the first run. Now do Refresh and see.
PHP 4.3.7

Code: Select all

4.3.7

$_SESSION (0): Array
(
)

$_SESSION (1): Array
(
    &#1111;froop] => 0
)

This was the first run. Now do Refresh and see.
After refresh...

Code: Select all

4.3.7

$_SESSION (0): Array
(
    &#1111;froop] => 0
)

$_SESSION (1): Array
(
    &#1111;froop] => 0
)

This was the first run. Now do Refresh and see.
PHP 4.3.4

Code: Select all

4.3.4

$_SESSION (0): Array
(
)

$_SESSION (1): Array
(
    &#1111;froop] => 0
)

This was the first run. Now do Refresh and see.
After refresh....

Code: Select all

4.3.4

$_SESSION (0): Array
(
    &#1111;froop] => 0
)

$_SESSION (1): Array
(
    &#1111;froop] => 0
)

This was the first run. Now do Refresh and see.
Which version are you running?

Posted: Sat Jul 10, 2004 9:11 am
by harryd
I ran it on different servers. One running 4.3.6 and one running 4.3.1.

Check out http://test.foxobn.org/aolweb/signup1.php. That runs exactly the code I entered in my previous post.

On a local 4.3.3 EasyPHP/Win98 testbed I got the same results you show. Makes me wonder if it might be a config issue.

Posted: Sat Jul 10, 2004 9:22 am
by redmonkey
The servers giving you problems have register globals set to on.

If you have access to the php.ini for the server turn off register globals from there.

Or alternatively, are you running Apache and able to use .htaccess files? If so create the .htaccess file with the following line...

Code: Select all

php_value register_globals Off
and upload that to the directory where you are running the test script and all should be good.

Posted: Sat Jul 10, 2004 9:51 am
by harryd
Thanks, that helped.

Posted: Sat Jul 10, 2004 10:20 am
by patrikG
harryd, when you post code, use

Code: Select all

tags around it and read: http://forums.devnetwork.net/viewtopic.php?t=8815