php 使用后清除会话变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3347672/
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 09:25:27  来源:igfitidea点击:

Clear session variable after use

phpsessionformspostlogin

提问by steveneaston

Is it possible to use a session variable, then unset it directly after?

是否可以使用会话变量,然后直接取消设置?

Example:

例子:

//====
//Process Form
if ($_POST['Submit']) {
    $update = $userSettings->update($_POST);

    //If there are form errors
 if (!$update) {
        //Load the errors into an array
        $errors = $update[1];
    } else {
        //Set the session
        $_SESSION['showUpdated'] = true;

        //Redirect to this page
        header("Location: http://www.mysite.com/settings");
    }
}

//==================

if ($_SESSION['showUpdated']) {
 echo "Settings Updated";
    unset($_SESSION['showUpdated'];
}

So after the form is submitted, if there are no errors:

所以表单提交后,如果没有错误:

  • Set a session to say the form submission was okay
  • Reload the page (to prevent re-submitted POST data)
  • If the 'showUpdated' session variable is set, display the "Updated" message
  • Unset the session variable (so we don't see the message on next reload)
  • 设置一个会话说表单提交没问题
  • 重新加载页面(防止重新提交POST数据)
  • 如果设置了“showUpdated”会话变量,则显示“Updated”消息
  • 取消设置会话变量(因此我们在下次重新加载时看不到消息)

Currently the problem is, if you unset the session variable straight after; It is as if you have un-set it before the "if exists" part.

目前的问题是,如果您之后立即取消设置会话变量;就好像您在“如果存在”部分之前取消了它。

Any solutions? Is this even the best way to do it?

任何解决方案?这甚至是最好的方法吗?

Many thanks!

非常感谢!

采纳答案by Scott Saunders

That looks like it should work. Make sure you call session_start() before trying to use the session, and always exit() or die() after a redirect header.

看起来它应该工作。确保在尝试使用会话之前调用 session_start(),并且始终在重定向标头之后 exit() 或 die()。

I accomplish what you're doing a little differently. I keep a 'message' element in the session. I'll stick text in like 'Your data was saved', error messages, etc. Then, on each page (actually in a page template class), I check to see if the $_SESSION['message']is set and not empty. If there's something there, I display the message and set the value to an empty string or null.

我完成你正在做的事情有点不同。我在会话中保留了一个“消息”元素。我将粘贴文本,例如“您的数据已保存”、错误消息等。然后,在每个页面上(实际上是在页面模板类中),我检查是否$_SESSION['message']已设置并且不为空。如果那里有东西,我会显示消息并将值设置为空字符串或 null。

回答by donlaur

I noticed a small error in the original example that might cause other problems.

我注意到原始示例中的一个小错误可能会导致其他问题。

unset($_SESSION['showUpdated'];

needs to be

需要是

unset($_SESSION['showUpdated']);

Not including that end )in the unsetwill cause an error.

不包括为此)unset会导致错误。

回答by spinon

I do this from time to time. I never have any problems with it. But what I would add to yours is an exit()function call after the header redirect.

我不时这样做。我从来没有遇到任何问题。但是我要添加到您的是exit()标题重定向后的函数调用。

EDIT: The reason for the exit()is that it will prevent it from processing any further code and will eliminate the possibility of unset before you wanted to check after the redirect.

编辑: 原因exit()是它会阻止它处理任何进一步的代码,并且会在您想在重定向后检查之前消除未设置的可能性。

回答by Jim W.

The header call without an exit after will continue running the page.

没有退出之后的标头调用将继续运行页面。

header("Location: http://www.mysite.com/settings");
exit;

Using that instead, should kill the page and not unset the session variable on the same page call.

改用它,应该杀死页面并且不会在同一个页面调用中取消设置会话变量。

回答by TheJacobTaylor

Just check to see if it exists. This is safe to do before it has been defined and will tell you your answer after it has been defined.

只需检查它是否存在。在定义之前这样做是安全的,并且会在定义之后告诉您答案。

if(!empty($_SESSION['showUpdated'])) {

回答by tomasbelusky

Or you can just set it to false.

或者您可以将其设置为false。

if ($_SESSION['showUpdated']) {
 echo "Settings Updated";
 $_SESSION['showUpdated'] = false;
}

And it looks like you use smaller version of PHP than 5.3, because in 5.3 you'll get notice when you use uninitialized value. So you should use isset function:

而且看起来您使用的 PHP 版本比 5.3 小,因为在 5.3 中,当您使用未初始化的值时,您会收到通知。所以你应该使用 isset 函数:

if (isset($_SESSION['showUpdated']) && $_SESSION['showUpdated']) {
 echo "Settings Updated";
 $_SESSION['showUpdated'] = false;
}