1

According to the manual "Do NOT unset the whole $_SESSION with unset($_SESSION) as this will disable the registering of session variables through the $_SESSION superglobal."

<?php
session_start();

$_SESSION['name'] = 'my_name';

unset($_SESSION);

echo $_SESSION['test'] = 'ok';

So according to the manual, the above code should not register the last statement. But the code outputs the string 'ok'. What is going on here?

2
  • 1
    "It won't set variable" != "you can't set variable"
    – Emissary
    Commented Apr 5, 2013 at 14:43
  • once you have unset the session, it is free to have new values stored in it, its not like it goes away forever. Unset merely clears the current values.
    – user2095686
    Commented Apr 5, 2013 at 14:46

3 Answers 3

3

It doesn't disable sessions. It simply disables the use of session_register. This is not a big deal, as session_register has been deprecated for a long time:

e.g. with this code:

<?php

$foo = 'bar';
session_register('foo');
print_r($_SESSION);
unset($_SESSION);
session_register('foo', 'bar2');
print_r($_SESSION);               // line 8
$_SESSION['foo'] = 'bar3';
print_r($_SESSION);

you get

Array
(
    [foo] =>
)
PHP Notice:  Undefined variable: _SESSION in /home/marc/z.php on line 8
Array
(
    [foo] => bar3
)

note the warning. You've unset $_SESSION, then try to session_register... but this doesn't create a NEW $_SESSION, it simply fails.

1

This is a better testcase:

session_start();
$_SESSION['test'] = 'value';
unset($_SESSION);
var_dump($_SESSION);

This code however, will output 'something' because you assign it just before echoing.

echo $_SESSION['something'] = 'something';
0

I believe what it means is that $_SESSION['test'] will not automatically be saved in the session any more. If you print $_SESSION['name'] on another page, it should be myname, but $_SESSION['test'] should be empty.

Not the answer you're looking for? Browse other questions tagged or ask your own question.