我创建了一个基于会话的简单登录页面。
session_start();
并添加了一个包含该页面的注销页面
session_destroy();
现在,当我关闭浏览器/页面并重新打开它时,会话的值仍然存在。
我想知道如何完全关闭页面/浏览器关闭时的会话。
最新回答
- 2021-1-111 #
- 2021-1-112 #
您将只能检测浏览器是否 窗口已使用javascript关闭,此时您可能可以触发Ajax请求以执行注销操作。
- 2021-1-113 #
服务器无法检测到浏览器或选项卡关闭,您可以使用Javascript或Ajax,但抱歉 我对此一无所知。
我的建议是使用会话超时,因此如果用户未采取任何措施,会话将被破坏.这是一个例子:
// 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
希望这对您有帮助
- 2021-1-114 #
如果会话存在,请通过销毁会话并将用户重定向到首页来注销.临时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(); } ?>
- 2021-1-115 #
to remove session variables -- session_unset();
to destroy the session -- session_destroy();
session_unset(); session_destroy();
相关问题
- php:会话数据仅在Chrome中丢失phpsessiongooglechrome2021-01-11 23:28
- 如何使用PHP从一个页面继续到另一个页面phpsession2021-01-10 19:26
如果您使用:
关闭浏览器时,您的会话cookie将会被破坏...因此,在关闭浏览器之前,您的会话将保持良好状态. IE浏览器 登录并登录后,关闭浏览器,重新打开浏览器,再次进入该站点,您将无法登录。