I'm a bit confused by session variables. I'm of the impression that I can do the following:

$_SESSION['var'] = $somevariable;

and then in subsequent scripts do the following:

$somevariable = $_SESSION['var'];

this seems to work just fine, by the way, and I did not register the variable, i.e., I didn't do a
session_register('var');

the problem somes when I do the following:

unset($_SESSION['var']);

the session variable is NOT reset. So when I do:

isset($_SESSION['var']) it returns true even if I unset it immediately before calling the new script.

I know there is quite a bit of e-mail on unsetting globals within functions but this specifically is dealing with the $_SESSION global.

Any insights out there?

r

    I'm not exactly sure what you're trying to do but I seem to get along fine just using PHP's session functions.

    session_register('var')
    session_unregister('var')
    session_is_registered('var')

    ... and all the others

      Originally posted by rharter
      The problem comes when I do the following:

      unset($_SESSION['var']);

      the session variable is NOT reset. So when I do:

      isset($_SESSION['var']) it returns true even if I unset it immediately before calling the new script.

      Odd; could you supply some more information? The following works as it should for me, does it for you?

      session_start();
      echo "1. ";
      print_r($_SESSION);
      $_SESSION['foo']='bar';
      echo "2. ";
      print_r($_SESSION);
      echo "\$_SESSION['foo']".(isset($_SESSION['foo'])?" set to $_SESSION[foo]\n":" unset\n");
      unset($_SESSION['foo']);
      echo "3. ";
      print_r($_SESSION);
      echo "\$_SESSION['foo']".(isset($_SESSION['foo'])?" set to $_SESSION[foo]\n":" unset\n");
      

        Your snippet works fine - I believe it fails across pages. Here's two little snippets.

        session1.php:

        <?php

        session_start();

        if (!isset($_SESSION['var'])) {

        echo 'var is not set <br/>';

        } else {
        echo 'var is set <br/>';
        echo 'and the value is = ' . $SESSION['var'] . '<br/>';
        }
        $somevar = "this is a test";
        $
        SESSION['var'] = $somevar;

        echo " <a href='session2.php'>Click Here</a>";

        ?>

        and session2.php:

        <?php

        session_start();

        if (!isset($_SESSION['var'])) {

        echo 'var is not set <br/>';

        } else {
        echo 'var is set <br/>';
        echo 'the value of session var= ' . $_SESSION['var'] . '<br/>';
        }

        unset($_SESSION['var']);

        echo " <a href='session1.php'>Click Here</a>";

        ?>

        session2.php unset's the session variable before it exits. click the link and you should have session1.php say the var is not set, but it is.

        See what you find,

        ron

          Hm. I see what you mean, now. The variable is being unset in the $SESSION[] array on session2, but the change isn't being made in the session data. I guess the situation is - if it's not set in the $SESSION[] array it gets missed during the session data writeback at script's end. 'Course, that should mean it no longer appears, but instead it looks like PHP just forgets about it instead. I'd call this a bug, but I can guess why it occurs (edit: it is indeed a bug, and my guess looks like it may be right).

          But I did find a workaround!

          $_SESSION['var']=null;
          

          While the bug discussion hinted at another: put ini_set('register_globals',0); before you session_start().

          And that partly explains why my initial experiments worked: I operate with register_globals off anyway - and only experienced the bug when using a server with register_globals=on.

            thanks - yes, I think it is a bug. I think my snippets are pretty simple to understand, more so than some of the cryptic crap people seem to like to submit ....

            anyway, thanks for the workaround - it's better than mine. I had just did:

            $_SESSION['var'] = "";

            and then

            if ($_SESSION['var'] == "") {do something}

            but your workaround let's me continue to use:

            if (isset($_SESSION['var']))

            so it's better - less to change when/if it gets fixed.

            sure did waste some time on that one .....

            r

              2 months later

              happy i found this thread 😃 😃 😃

                Write a Reply...