PHP的Javascript?做一些事情,当用户关闭网页或浏览器 [英] PHP Javascript? Do something when user closes page or browser

查看:147
本文介绍了PHP的Javascript?做一些事情,当用户关闭网页或浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/2485621/how-can-i-destroy-sessions-if-user-closes-the-browser-window-or-navigates-away-f\">How我可以摧毁会话,如果用户关闭浏览器窗口或页面导航走在PHP?

基本上我想更新我的MySQL数据库设置我的用户离线和可能,当他们关闭浏览器或页面结束他们的会话。

Basically I want to set my users to "Offline" by updating my MySQL DB and possibly end their session when they close their browser or the page.

我看到一个网站,做到这一点。

I have seen a website that does this.

任何人都可以解释它如何做,并张贴一个例子吗?

Can anyone explain how its done and post an example please?

感谢您先进的。

推荐答案

在页面被关闭或导航远离是一个事件处理程序附加到卸载事件,因为火箭建议你可以做一些事情的唯一途径。但是,你不应该依赖于这个事件来触发,因为很多事情可以prevent它。浏览器可能会停止它以专注于其他任务,它的资源,用户可能会失去它的连接,浏览器可能被终止等。

The only way you can do something when the page is closed or navigated away from is to attach an event handler to the unload event, as Rocket suggests. However, you shouldn't rely on this event to trigger, as a lot of things may prevent it. The browser may stop it on order to focus it's resources on other tasks, the user may loose it's connection, the browser may be terminated, etc.

来跟踪用户和会话的最可靠的办法是让他们在给定的时间间隔发送keepalive消息到服务器。然后你就会知道用户给定的时间戳和keepalive报文的间隔之间留有一段时间。

The most reliable way to keep track of users and session is to have them send keepAlive messages to the server at a given interval. Then you'll know that the user left sometime between a given timestamp and the interval of the keepAlive message.

在服务器上,然后你可以遍历还没有被保持存活了一段时间的会话和执行所需的任何操作。

On the server, you can then traverse the sessions which has not been kept alive for a while and perform any operation you need.

但是,这种方法也不会,如果你只需要创建一些很酷的注销-effect是必要的。

However, this approach won't be necessary if you only need to create some cool "logging off"-effect.

这篇关于PHP的Javascript?做一些事情,当用户关闭网页或浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆