会话变量在浏览器之间的行为不同

时间:2014-03-04 11:56:07

标签: php session

我正在处理不同浏览器之间的奇怪行为,甚至是不同系统上的相同浏览器。我有一个表格被处理并发送邮件。为了避免在刷新会话变量后再次发送相同的邮件。

由于某些原因,即使周围的if不正确,我系统上的Firefox也会取消设置变量。这在其他系统上的Chrome或Firefox中不会发生。据我所知,PHP根本不应该依赖于浏览器。

echo '<p>$_SESSION: <br />';
var_dump($_SESSION); //for testing only
echo '</p>'; 

if(!isset($_POST['csent'])): 
    unset($_SESSION['call-sent']); // to reenable the form for a different message
?>

    <form id="call-form" action="" method="POST">
        <input type="hidden" name="csent" value="1">
        ...
    </form>

<?php elseif( ( isset($_POST['csent']) ) 
              && ( !isset($_SESSION['call-sent']) ) ):

    $_SESSION['call-sent'] = 'x';

      /*all the stuff that handels the form*/
?>

    <div id="formsent">
        <!-- Success Message -->
    </div>

<?php else: ?> 
    <div id="formsent" class="repeat">
        <!-- Success Message -->
    </div>
<?php endif; ?>

在我的FF var_dump($_SESSION)始终是array(0) { }。表单只在发送之前显示。如果我评论未设置转储是array(1) { ["call-sent"]=> string(1) "x" }按预期的那样。

在Chrome发送表单并重新加载页面后,在Chrome中为array(1) { ["call-sent"]=> string(1) "x" }

如果重要的话,这是wordpress模板的一部分。

1 个答案:

答案 0 :(得分:0)

一旦处理完POST数据,就应该重定向到另一个页面。这样用户就无法刷新页面。

在POST处理后简单地放置header('Location: http://yoursite.com/path/to/script.php'); die();