tags:

views:

58

answers:

3

I have this small PHP script:

<?php
session_start();

$var = array();
$var['key'] = 'Var -> Key';

if ($_GET['set']) {
  $_SESSION = array();
  $_SESSION['var'] = 'Session -> Var';
}
print_r($_SESSION);
?>

I would expect it to return this, for set=0 and set=1:

Array
(
    [var] => Session -> Var
)

However it returns this for set=0 (after set=1 of course):

Array
(
    [var] => Array
        (
            [key] => Var -> Key
        )

)

Have a look yourselfe over here: http://dev.gruppenunterkuenfte.de/index_test.php?set=1

What seams to happen is that $_SESSION['var'] gets replaced by $var. But only after the next page load.

Any idea why?

I can switch my PHP version in my hosters admin interface and I tried 5.2.11, 5.3.2 and 4.4.8.

Is it a setting I can change in PHP, so it will not overwrite Session Variables? Cause I don't have this issue on another server.

There seams to be some kind of setting to make PHP write $var in $_SESSION['var'], if $_SESSION['var'] is defined.

+1  A: 

nothing strange, it's documented behavior.
just turn register_globals off

Col. Shrapnel
+1  A: 

It's being overwritten because of register_globals, which causes wacky behavior in request and session variables by exporting them to the global scope.

You'll need to set that to Off; it'll save you a lot of trouble dealing with superglobals, and give your PHP a slight performance boost at that.

BoltClock
+2  A: 

Turn off register_globals option

Alexander.Plutov
Thanks, that helped.. It was actually the first thing I checked. I turned it off in my hosters interface, but didn't check with phpinfo() :-/ Now I checked and realized it wasn't turned off. Thanks for the quick help
JochenJung