PHP Session variable isset(..)=1 after session_start()

Posted by Nicsoft on Stack Overflow See other posts from Stack Overflow or by Nicsoft
Published on 2011-01-07T10:38:16Z Indexed on 2011/01/07 10:54 UTC
Read the original article Hit count: 200

Hello!

I guess I am not understanding the scope of session variables, or the session itself, in PHP, hence this question:

This is my code

  if(!session_id()==""){
  echo "Getting rid of session"."</br>";
  session_destroy();
 }
 echo "Before session_start(): ".isset($_SESSION["first_date_of_week"])."</br>";
 session_start();
 echo "After session_start(): ".isset($_SESSION["first_date_of_week"])." ".$_SESSION["first_date_of_week"]->format("Y-m-d")."</br>";

The output is:

Before session_start():
After session_start(): 1 2011-01-09

How come that when doing the isset(..) on the session variable it is set directly after starting the session, even though I haven't even used it or set it yet? It does, however, still have the same value as before.

Also, session_id()="" since the if-clause is never triggered. I never kill the session, how come it is set to ""? I.e. I refresh the page and expects the session to still be alive.

Using the isset(..) function is then pretty useless testing if it has been set already...

Thanks in advance!

/Niklas

© Stack Overflow or respective owner

Related posts about php

Related posts about web-development