检查用户是否在PHP中关闭了页面? [英] Check if user closed the page in PHP?

查看:78
本文介绍了检查用户是否在PHP中关闭了页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PHP和JavaScript聊天进行了聊天,并且有一个断开连接按钮,用户从聊天中删除用户首先将其从用户列表中删除。但是如果用户关闭浏览器,那么他将保留在用户列表中。如何检查他是否离开?

I made a chat using PHP and JavaScript chat and there is a disconnect button which removes user from the chat removing him from user list first. But if the user closes browser then he will remain in the user list. How do I check if he left?

这必须在JS中完成而不在页面关闭处理任何句柄,因为如果用户杀死了浏览器那么他将继续聊天。

This must be done without putting any handles on page closing in JS because if user kills the browser then he will remain in chat.

顺便说一下,JS脚本总是向PHP页面发送请求,该页面不断检查循环中的新消息以及何时出现有些,脚本将它们打印出来并退出。然后它重复一遍。

编辑:我如何在PHP中制作一个心跳的东西?如果用户关闭页面,脚本执行将被终止,因此我们将无法检查用户是否仍在同一脚本中连接。

EDIT : How do I make a heartbeat thing in PHP? If a user closes the page the script execution will be terminated therefore we won't be able to check if the user is still connected in the same script.

推荐答案

抱歉,没有可靠的方法,这就是HTTP的构建方式 - 它是一种拉协议。
我能想到的唯一解决方案是有效和登录的客户端必须在很短的时间间隔内查询服务器。如果他们不这样做,他们就会退出。

Sorry, there is no reliable way of doing this, that's the way HTTP was built - it's a "pull" protocol. The only solution I can think of is that "valid" and logged in clients must query the server in a very small interval. If they don't, they're logged out.

这篇关于检查用户是否在PHP中关闭了页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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