开发者

Strange session issue in PHP

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 开发者_StackOverflowfrom 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!


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


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"?


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


The code you have is correct. And since the session is being unset, we know that the statements in the if block are being executed. May be the output is actually being displayed by echo, but is just not shown by the browser (this can happen if your css code is configured so). So, just check the source of the output page and check if the source contains the out put message.

In other way, you can put a javascript alert box in your echo and see if it displays an alert box.

echo "<script type='text/javascript'> alert('Hi'); </script>";

This should override any hiding css code.


Old thread, but I'll add that I would prefer isset() in this situation:

<?php
session_start();
if(isset($_SESSION['sent'])){
    echo "Successfully submitted form!";
    $_SESSION = array();
    session_regenerate_id();
    session_unset();
    session_destroy();
    exit;
}
if(isset($_POST['submit'])){
    //validate input & process form
    $_SESSION['sent'] = 1;
    header("location:form.php"); // name of this file
    exit;
}
echo "Enter your email<br />
<form action='' method='post'>
<input type='text' name='email' />
<input type='submit' name='submit' />
</form>";
exit;
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜