php 取消设置会话变量的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1025300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:40:51  来源:igfitidea点击:

Problem unsetting a session variable

phpsessionsession-variables

提问by Seth

There's a form on my site that's used for inviting friends. It's a simple text field and a submit button. If there is an error I redirect back to this page and display an error message if their is a session variable set.

我的网站上有一个用于邀请朋友的表格。它是一个简单的文本字段和一个提交按钮。如果出现错误,我会重定向回此页面并显示错误消息(如果它们是会话变量集)。

if (isset($_SESSION['invite_error'])) {
   echo $_SESSION['invite_error'];
   unset($_SESSION['invite_error']);
}

However, if I navigate away from this page and come back to it -the error message is still being displayed. If I navigate away and come back one more time it will be done. It's the same when I refresh that page...1 refresh won't get rid of it, but 2 will. I can't destroy the entire session, I just want to unset this one variable. PHP version is 5.2.5 build 6, register globals is off, I'm calling session_start() at the top of this page, I've tried using a no-cache header as well.

但是,如果我离开此页面并返回它,错误消息仍会显示。如果我离开并再回来一次,它就会完成。当我刷新该页面时也是如此......刷新1次不会摆脱它,但2会。我不能破坏整个会话,我只想取消设置这个变量。PHP 版本是 5.2.5 build 6,注册全局变量关闭,我在本页顶部调用 session_start(),我也尝试使用无缓存标头。

Edit: Added full code.

编辑:添加了完整代码。

<?php 
ob_start();
session_start();

$user_id = $_SESSION['user_id'];
$user_name = $_SESSION['user_name'];

if ($user_id==null) header("Location: /login.php");

if (isset($_SESSION['invite_errors'])) {

    $error = $_SESSION['invite_errors'];
    unset($_SESSION['invite_errors']);

}

require_once("ui/header.php");
?>



<div id="invite" class="content">

    <?php if($error) { ?>
        <div class="errors round">
            <?php echo $error ?>
        </div>
    <?php } ?>

    <h3>Invite Your Friends</h3>

    <div class="invite-form">
        <form method="post" action="controllers/invite.php">
            <div class="row">
                <textarea class="txt-area" name="emails" id="emails" rows="5"></textarea>
                <div class="tip">Separate multiple email addresses with ,</div>
            </div>
            <div class="row-submit">
                <input type="submit" name="submit" id="submit" class="submit-btn" value="Submit" />
            </div>
        </form>
    </div>

</div>

<?php
    require_once("ui/footer.php");
?>

回答by James Skidmore

Start the session before you start the output buffering.So, switch the ob_start()and session_start()calls.

在开始输出缓冲之前启动会话。所以,切换ob_start()session_start()调用。

Since session cookies are defined in the headers sent to the browser, and headers are sent to the browser when you start the buffer, you must start the sessions beforethe buffer.

由于会话 cookie 是在发送到浏览器的标头中定义的,并且标头是在您启动缓冲区时发送到浏览器的,因此您必须在缓冲区之前启动会话。

回答by Sander Marechal

The example you provided should work. Perhaps some kind of session cache is getting in your way. You could try modifying your code as follows:

您提供的示例应该可以工作。也许某种会话缓存正在妨碍您。您可以尝试如下修改您的代码:

if (isset($_SESSION['invite_errors']) && $_SESSION['invite_errors']) {

    $error = $_SESSION['invite_errors'];
    $_SESSION['invide_errors'] = false;
    unset($_SESSION['invite_errors']);

    // Explicitly write and close the session for good measure
    session_write_close();

}

回答by thomas-peter

In my experience, PHP sessions aren't worth using and were only designed as a quick and easy solution. $_SESSION behaviour is not always obvious but using sessions in the traditional sense is mostly unavoidable. Issue your own id and token cookies and store session values against these, perhaps in a database. This also has the benefit of being scalable across servers.

根据我的经验,PHP 会话不值得使用,它只是被设计为一种快速简便的解决方案。$_SESSION 行为并不总是很明显,但使用传统意义上的会话几乎是不可避免的。发布您自己的 id 和令牌 cookie 并根据这些存储会话值,也许在数据库中。这还具有跨服务器可扩展的好处。

回答by sqram

Maybe you can change the value after echo'ing the error

也许您可以在回显错误后更改值

if (isset($_SESSION['invite_error'])) {
   echo $_SESSION['invite_error'];
   $_SESSION['invite_error'] = null;
   unset($_SESSION['invite_error']);
}