关闭时销毁 PHP 会话
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4146647/
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
Destroy PHP Session on closing
提问by sikas
I have created a simple login page which is based on the sessions.
我创建了一个基于会话的简单登录页面。
session_start();
and added a logout page that contains this
并添加了一个包含此的注销页面
session_destroy();
Now when I close the browser/page and reopen it, the values of the session are still there.
现在,当我关闭浏览器/页面并重新打开它时,会话的值仍然存在。
I want to know how to completely destroy the session on page/browser close.
我想知道如何在页面/浏览器关闭时完全销毁会话。
回答by superfro
if you use:
如果您使用:
session_set_cookie_params(0);
session_start();
Your session cookie will destroy when the browser is closed... so your session will be good until they close the browser. IE. You login, and you are logged in, you close the browser, re-open it, go to the site again, and you wont be logged in.
您的会话 cookie 将在浏览器关闭时销毁...因此您的会话将一直有效,直到他们关闭浏览器。IE。您登录,您已登录,关闭浏览器,重新打开它,再次访问该站点,您将无法登录。
回答by Treffynnon
You will only be able to detect if the browser window has been closed using javascript at which point you might be able to trigger an Ajax request to perform a logout action.
您将只能使用 javascript 检测浏览器窗口是否已关闭,此时您可能能够触发 Ajax 请求以执行注销操作。
回答by Fahmi
Server can't detect browser or tab closed, you could use Javascript or Ajax but sorry I don't have knowledge about that.
服务器无法检测到浏览器或选项卡已关闭,您可以使用 Javascript 或 Ajax,但抱歉我不了解这方面的知识。
My suggestion is use Session Timeout, so session will be destroyed if there's no action from user. This is an example :
我的建议是使用会话超时,因此如果用户没有操作,会话将被销毁。这是一个例子:
// destroy every 2 minutes
if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 120)) {
// last request was more than 2 minutes ago
session_destroy(); // destroy session data in storage
}
$_SESSION['LAST_ACTIVITY'] = time(); // update last activity time stamp
// end of code
Hope this help you
希望这对你有帮助
回答by csandreas1
If the session exists, log out by destroying the session and redirecting the user to the home page. A temporary cookie was used to store the session identity. This cookie is also destroyed.
如果会话存在,则通过销毁会话并将用户重定向到主页来注销。临时 cookie 用于存储会话标识。这个cookie也被销毁了。
<?php
// This is the logout page for the site.
session_start();//access the current session.
//if no session variable then redirect the user
if (!isset($_SESSION['user_id'])) {
header("location:index.php");
exit();
}else{ //cancel the session
$_SESSION = array(); // Destroy the variables
session_destroy(); // Destroy the session
setcookie('PHPSESSID', ", time()-3600,'/', ", 0, 0);//Destroy the cookie
header("location:index.php");
exit();
}
?>
回答by Giwayshans
to remove session variables- session_unset();
删除会话变量- session_unset();
to destroy the session- session_destroy();
销毁会话- session_destroy();
session_unset();
session_destroy();