PHP JavaScript?当用户关闭页面或浏览器时执行某些操作

发布于 2025-01-04 02:41:37 字数 365 浏览 1 评论 0原文

可能的重复:
如何如果用户关闭浏览器窗口或离开 php 页面,我会销毁会话吗?

基本上,我想通过更新我的 MySQL 数据库将用户设置为“离线”,并可能在他们关闭浏览器或 页。

我见过一个网站是这样做的。

谁能解释一下它是如何完成的并发布一个例子吗?

提前谢谢您。

Possible Duplicate:
How can I destroy sessions if user closes the browser window or navigates away from page in php?

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?

Thank you in advanced.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

追风人 2025-01-11 02:41:37

当页面关闭或导航离开时,您可以执行某些操作的唯一方法是将事件处理程序附加到卸载事件,如 Rocket 建议的那样。但是,您不应该依赖此事件来触发,因为很多事情可能会阻止它。浏览器可能会停止它,以便将其资源集中在其他任务上,用户可能会失去连接,浏览器可能会被终止,等等。

跟踪用户和会话的最可靠方法是让他们将 keepAlive 消息发送到服务器以给定的时间间隔。然后您就会知道用户在给定时间戳和 keepAlive 消息间隔之间的某个时间离开了。

在服务器上,您可以遍历一段时间内未保持活动状态的会话并执行您需要的任何操作。

但是,如果您只需要创建一些很酷的“注销”效果,则不需要这种方法。

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.

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.

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

GRAY°灰色天空 2025-01-11 02:41:37

我将使用 jQuery 来执行此任务..类似这样并使用上面提到的 window.onunload :

$.ajax({
  type: 'POST',
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

I would use jQuery to perform this task.. something like this and using the window.onunload as mentioned above:

$.ajax({
  type: 'POST',
  url: url,
  data: data,
  success: success,
  dataType: dataType
});
微暖i 2025-01-11 02:41:37

您可以使用 window.onunload 来触发 AJAX当用户离开页面/关闭选项卡时调用。

window.onunload = function(){
  // AJAX call to mark user "offline"
}

编辑:我建议在单击链接时设置一个变量,以便仅在用户离开页面时运行。

使用 jQuery,可以这样完成:

$('a').click(function(){ // Run for all links
  $('body').data('linkClicked', true); // Set global variable
});

$(window).unload(function(){ // jQuery version of window.onunload
   if(!$('body').data('linkClicked')){ // Check global variable
      $.ajax({
        url: 'url',
        data: {some: data},
        async: false // this locks the browser, but it may be needed to make
                     // sure the ajax call runs before the tab is closed
      });
   }
});

You can use window.onunload to fire an AJAX call when the user leaves the page/closes the tab.

window.onunload = function(){
  // AJAX call to mark user "offline"
}

EDIT: I suggest setting a variable when clicking on links, so that this only runs when the user leaves the page.

Using jQuery, it can be done like this:

$('a').click(function(){ // Run for all links
  $('body').data('linkClicked', true); // Set global variable
});

$(window).unload(function(){ // jQuery version of window.onunload
   if(!$('body').data('linkClicked')){ // Check global variable
      $.ajax({
        url: 'url',
        data: {some: data},
        async: false // this locks the browser, but it may be needed to make
                     // sure the ajax call runs before the tab is closed
      });
   }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文