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
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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.