PHP 结束会话
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18705239/
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
End Session in PHP
提问by Jaycee
i have problem in End session, i have MAIN.PHP where my session starts, inside my MAIN.php i have<a href = "logout.php">logout</a>
this is the code for my logout.php
我在结束会话时遇到问题,我有 MAIN.PHP 是我的会话开始的地方,在我的 MAIN.php 中,<a href = "logout.php">logout</a>
这是我的 logout.php 的代码
<?php
unset($_SESSION['user']);
unset($_SESSION['pass']);
session_destroy();
echo"<script> window.location.href = '../index.php' ; </script>";
exit();
?>
after clicking the link logout, when i press the back button i can still access the pages and still it holds my SESSION variables.
单击链接注销后,当我按下后退按钮时,我仍然可以访问页面,并且它仍然包含我的 SESSION 变量。
回答by War10ck
I'm going to take a wild stab and say that this answerwill solve your problem.
我将大胆地说这个答案将解决您的问题。
Per the PHP Docs For session_destroy():
根据PHP Docs For session_destroy():
session_destroy()
destroys all of the data associated with the current session. It does notunset any of the globalvariables associated with the session, or unset the session cookie. To use the session variables again, session_start() has to be called.In order to kill the session altogether, like to log the user out, the session id must also be unset. If a cookie is used to propagate the session id (default behavior), then the session cookie must be deleted. setcookie() may be used for that.
session_destroy()
销毁与当前会话相关的所有数据。它不会取消设置与会话关联的任何全局变量,也不会取消设置会话 cookie。要再次使用会话变量,必须调用 session_start()。为了完全终止会话,比如注销用户,会话 ID 也必须取消设置。如果使用 cookie 来传播会话 ID(默认行为),则必须删除会话 cookie。setcookie() 可用于此目的。
To sum up what seems to be your problem, the pesky session cookie (which identifies the session) is not being unset. Therefore, when you hit the back button, and session_start();
appears at the top of your script, the global session variables are still being referenced. As the other answer suggests, try:
总结一下似乎是您的问题,讨厌的会话 cookie(标识会话)没有被取消设置。因此,当您点击后退按钮并session_start();
出现在脚本顶部时,全局会话变量仍在被引用。正如另一个答案所暗示的那样,请尝试:
$_SESSION = array();
to clear all of the global session data. Also, as the docs say, unset the session cookie.
清除所有全局会话数据。此外,正如文档所说,取消设置会话 cookie。
回答by Liam Allan
you might want to add session_start()
in there too
你可能也想session_start()
在那里添加
session_start();
unset($_SESSION['user']);
unset($_SESSION['pass']);
session_destroy();
header('Location: index.php');
also, i personally prefer to unset the entire session like unset($_SESSION);
, but everyone has their own ways of doing things
另外,我个人更喜欢取消整个会话unset($_SESSION);
,但每个人都有自己的做事方式