开发者

PHP Log user out after closing the page (tab)

I'm keeping track of the time that users are logged in. After they close the whole browser they are logged out; but when they only close the tab (there's only one tab), and开发者_开发百科 navigate back to the website within a few minutes they are logged in again.

Someone told me that this behavior can be changed in the server configuration. Does anyone know how?

I'm using PHP 5.2 and Apache. Just a normal webserver. I'm also using the Kohana 3 PHP framework. For logging users in there's being a session set with a cookie, in the cookie there's a session id.

Thanks!


You cannot reliably find out when the user closes your page - unload-related events also trigger when navigating to another subpage on your side. So the most common solution is to simply make a session time out after x minutes of inactivity.

Additionally, if you set your session (id) cookies without an expiry time ("session cookies") they will be deleted when the browser is closed.

By the way, a not really good "solution" for your request would be setting the session expiry time to a very very low value (30 seconds) maybe and "refresh" the session through an AJAX request in the background every ~15-20 seconds. However, if someone's connection is very slow the request might arrive too late and besides that, this solution would cause lots of unnecessary traffic.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜