关闭浏览器后如何使会话结束 [英] how to make session ends after closing browser
问题描述
我使用了一个简单的会话来创建一个登录系统,
i used a simple session to create a login system,
<?php
session_start();
$_SESSION['username'] = $username;
?>
这只是代码的一部分,但它是生成我的会话的原因,直到现在它都可以正常工作,问题是此会话不会以浏览器关闭而结束,我尝试登录关闭浏览器,但是当我再次访问该网站我发现我仍然登录,注销的唯一方法是使用注销系统,我想要的是一种在浏览器关闭时自动关闭会话的方法,在此先感谢
this is just a part of the code but it is what generate my session , it works fine until now, the problem is that this session don't end with browser closure, i tryed to login the close the browser , but when i visit the website again i find that i'm still logged in, the only way to logout is using a log out system, what i want is an automatic way to close the session on browser's closure, thanks in advance
推荐答案
简单的答案:你不能.会话存在于服务器上,而浏览器存在于客户端的机器上.浏览器在关闭时不会通知服务器.在许多情况下(例如:计算机故障或网络断开连接),浏览器即使想要通知您的服务器也无法通知.
Simple Answer: You can't. The session exists on the server and the browser exists on the client's machine. The browser does not notify the server when it is closing. In many cases (ex: a computer failure or network disconnect), the browser is unable to notify your server even if it wanted to.
复杂答案:您可以设置一个非常低的会话超时时间(即:5 分钟)并在每次用户 ping 服务器时更新它,但是如果人们离开键盘超过您的超时时间.
Complex Answer: You could set a very low session timeout time (ie: 5 minutes) and update it every time the user pings the server, but this will log people out if they've been away from their keyboard for more than your timeout time.
这篇关于关闭浏览器后如何使会话结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!