Jump to content

Session variable comes back from the dead


moogal

Recommended Posts

Hi,

I'm having a bizarre problem with a project I'm working on. Basically, on completion of certain functions I need to display a message to the user on the next page they visit, so I have a function which sets a session variable with the message text. Then, on page load, there is another function which checks if this has been set, and if so displays the message and unsets the session variable.

 

The problem I'm having is that on some pages, despite the session variable being unset, the message keeps appearing until I log out/clear the session completely. I've tried dumping the session and the variable is definitely being unset in the right place. I have no idea why this should be happening, and Googling didn't turn up anything useful.

 

Has anyone encountered anything similar to this, and if so - any tips for solving it?

 

Thanks in advance (from me and my sanity!)

 

A (simplified) example of the code in question:

function flash($strMessage) {
  $_SESSION['message'] = $strMessage;
}

function showFlash() {
  if(isset($_SESSION['message']) {
    echo "<div class='flash'>{$_SESSION['message']}</div>";
    unset($_SESSION['message']);
  }
}

Link to comment
Share on other sites

It couldn't really be set anywhere else. There's no specific path you have to follow to get the message to come up - the point of me putting it in the session was that I could then have it display on whichever page the user goes to next, so it's a bit difficult to post code as I have no way of controlling which page they then move on to. The only way that the variable is ever modified or accessed is via the two methods above.

 

What I've just tried is putting a counter variable in, so that each time the message is set, the counter is incremented and displayed with the message. This worked as expected - the same counter value was displayed on every page until another page set a different message, which makes me think it's not being set on each subsequent page.

 

I'm trying something else at this very moment - moving the variable into a class stored in the session just to see if that makes any difference.

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.