取消设置会话变量的问题 [英] Problem unsetting a session variable

查看:37
本文介绍了取消设置会话变量的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

if (isset($_SESSION['invite_error'])) {回声 $_SESSION['invite_error'];取消设置($_SESSION['invite_error']);}

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

添加完整代码.

<div id="invite" class="content"><?php if($error) { ?><div class="errors round"><?php echo $error ?>

<?php } ?><h3>邀请您的朋友</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">使用 ,</div> 分隔多个电子邮件地址

<div class="row-submit"><input type="submit" name="submit" id="submit" class="submit-btn" value="Submit"/>

</表单>

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

解决方案

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

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

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.

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

解决方案

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

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 before the buffer.

这篇关于取消设置会话变量的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆