Bug #18468: "global $_SESSION;" breaks sessions handling

From: Date: Mon, 22 Jul 2002 16:52:00 +0000
Subject: Bug #18468: "global $_SESSION;" breaks sessions handling
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-14777@lists.php.net to get a copy of this message
From: pratesi@telug.it Operating system: Red Hat Linux 7.3 PHP version: 4.2.1 PHP Bug Type: Session related Bug description: "global $_SESSION;" breaks sessions handling The following code evidences the problem: <?php session_start(); if (!isset($_SESSION["foobar"])) { echo "Foobar not set, now we set it to 1"; $_SESSION["foobar"] = 1; } else { $_SESSION["foobar"]++; echo "Foobar is set and is = " . $_SESSION["foobar"]; } echo "\n<br>"; if (!isset($_SESSION["counter"])) { echo "Counter not set, now we set it to 1"; set_counter(); } else { $_SESSION["counter"]++; echo "Counter is set and is = " . $_SESSION["counter"]; } echo "\n<br>"; if (!isset($_SESSION["foobar2"])) { echo "Foobar2 not set, now we set it to 1"; $_SESSION["foobar2"] = 1; } else { $_SESSION["foobar2"]++; echo "Foobar2 is set and is = " . $_SESSION["foobar2"]; } function set_counter() { global $_SESSION; $_SESSION["counter"] = 1; } echo "<pre>"; var_dump($_SESSION); echo "</pre>"; ?> Loading and reloading the page, $_SESSION["counter"] and $_SESSION["foobar2"] remain undefined, due to the global $_SESSION; put in function set_counter() The problem disappears commenting out // global $_SESSION; I realize that it makes no sense to declare as "global" a superglobal, but I was doing it only for compatibility with PHP 4.0, after the following code: if (!isset($_SESSION)) $_SESSION = $HTTP_SESSION_VARS; I think that it would be nice to get a warning due to the attempt of declaring a superglobal as "global"; it would also be nice if such a statement would be sistematically ignored (this would also allow to avoid breaking compatibility with PHP 4.0) BTW, a really strange thing: I ahve observed this problem both with PHP 4.1.2 and PHP 4.2.1, but not on each platform tested by me and my friends: Red Hat 7.3 and SuSE are affected, while Debian Woody and Mandrake 8.2 are not. -- Edit bug report at http://bugs.php.net/?id=18468&edit=1 -- Fixed in CVS: http://bugs.php.net/fix.php?id=18468&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=18468&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=18468&r=needtrace Try newer version: http://bugs.php.net/fix.php?id=18468&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=18468&r=support Expected behavior: http://bugs.php.net/fix.php?id=18468&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=18468&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=18468&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=18468&r=globals

« previous php.bugs (#14777) next »