tags:

views:

67

answers:

3

Hi,

I'm having a strange issue with sessions in PHP. Basically, when a user submits a contact form, the processing script sets a session on completion ( $_SESSION['action']='sent'; ). The user is then sent back to the page they sent the form from and a message is displayed using the following code:

$action = $_SESSION['action'];

if ( $action == 'sent' )
{
echo '<p>Thank you for contacting us, we will be in touch with you ASAP.</p>';
unset($_SESSION['action']);
}

The session is unset so if they refresh the page or navigate away and come back the message won't be displaying any more.

Basically the problem is that when the session is unset it seems to unset it from the very beginning of the script so that the message doesn't display. The if statement is obviously running as the session is being unset, but the message isn't displaying.

I've used this exact same script many times before and it works absolutely perfectly on other sites (on the same server, with all the same settings).

Any help/advice would be appreciated!

A: 

Are you initialized a session? session_start(); before output something in browser?

Alexander.Plutov
Yes I am. The message displays if I remove the unset($_SESSION['action']); from the code. It's unsetting it which is causing the problem.
thebiggianthead
A: 

Try to do a session_destroy(); instead of unset($_SESSION); Could you give us the part where you start the session and where you set the "action" to "sent"?

ITroubs
session_destory(); has the same effect, I'm afraid. The session is set in the form processing script with $_SESSION['action']='sent'; and all the files involved have session_start(); as their first lines.
thebiggianthead
Try this: if ( $action == 'sent' ){echo "I'm here"; ......} if it outputs the "I'm here" then you have a serious bug somewhere before the if
ITroubs
What's the difference between this and my code? Changing what is echoed makes no change. Or have I misinterpreted you?
thebiggianthead
it's a mean of debugging ;-)
ITroubs
A: 

Hi Tom are you making sure the script that start the session is in the same directory - eg are the commands accessing the same session - could be on under one is under https, and one is under http OR if One is under /, another is under /dir1, and /dir1 was run first . The cookie created by the session is for /dir1 and deeper, so the other script can't read it; it sees no session so it starts a new one.

I'm not brill at this sessions stuff but it might be worth a check. - Dad

Mark Smith
Not sure that this would make a difference regardless, but the PHP files are all in the same directory.
thebiggianthead