Jump to content

PHP $_SESSION superglobal question.


pthurmond

Recommended Posts

I have a site that I have already started a session with session_start(), yet when I check the session superglobal with isset($_SESSION) it returns false. Now it was my understanding that once the session is started the session superglobal becomes immediately available. Does it not become available until I put a value into it?

Link to comment
https://forums.phpfreaks.com/topic/114090-php-_session-superglobal-question/
Share on other sites

Hmm, well when I try to start a session it tells me that it is already started, yet when I then check to see if the session superglobal is available it says no. To make things even more confusing I save a variable to the superglobal then try to check it a few pages later and the data is gone.

Try this simple two page session test

 

page1.php

<?php

session_start();

if ( !isset($_SESSION['counter']) )
$_SESSION['counter'] = 0;

$_SESSION['counter']++;

echo $_SESSION['counter'] . ' -> <a href="page2.php">To page2.php</a>';

?>

 

page2.php

<?php

session_start();

if ( !isset($_SESSION['counter']) )
$_SESSION['counter'] = 0;

$_SESSION['counter']++;

echo $_SESSION['counter'] . ' -> <a href="page1.php">To page1.php</a>';

?>

 

If that doesn't work, there's something wrong with your setup. You may be forcing cookies and have them disabled/blocked by your client as well.

Actually I think I know the problem now.

We are working in Expression Engine and instead of creating an extension, the other developer decided to start the session in the layout head template. Which leaves us with a session start location that is a bit unpredictable. So I think that the code execution order is part of the problem. I will work on developing an extension for it.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.