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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
harryd
Forum Newbie
Posts: 4
Joined: Sat Jul 10, 2004 8:03 am

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

Post 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]
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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?
harryd
Forum Newbie
Posts: 4
Joined: Sat Jul 10, 2004 8:03 am

Post 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.
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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.
harryd
Forum Newbie
Posts: 4
Joined: Sat Jul 10, 2004 8:03 am

Post by harryd »

Thanks, that helped.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

harryd, when you post code, use

Code: Select all

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