问题描述
是否可以使用其IP地址检查访问者是否仍然在线?
Is it possible to check if the visitor is still online using their IP Address?
当访问者使用onLoad和onBeforeUnload事件打开我的页面之一时,我已经创建了一个javascript和php来检查访问者的IP地址.但是在某些情况下,我的脚本无法检测到访问者是否仍在打开我的网站.由于onBeforeUnload一直都不可靠.
I have already created a javascript and php to check the visitors ip address when they open one of my page using onLoad and onBeforeUnload event. But there are some cases that my script cannot detect if the visitor is still opening my site. Since onBeforeUnload is not reliable all the time.
在这种情况下,我想知道是否还有其他方法可以检测访客是否仍然在线.
In this case, I want to know if there are some other method to detect if the visitor is still online.
如果访问者仍在打开页面但由于可能正在阅读其他网站或正在运行其他程序而没有阅读它,我仍会认为他/她在线.
If the visitor still open the page but don't read it because may be he/she is reading other site or running other program I would still consider he/she is online.
但是,如果访问者关闭了浏览器,但是我的脚本无法检测到其卸载事件,我想知道他/她是否仍然在线.
If the visitor however, closed the browser but my script fail to detect its unload event I want to know if he/she still online.
PHP或javascript对我来说还可以.
A PHP or javascript is okay for me.
推荐答案
您应该创建JavaScript代码,以通过Ajax对服务器执行"ping"操作(发送请求,可以为空白).该请求应依次标记"该用户在PHP中处于活动状态(可能在数据库中,或使用的任何存储方法).您可以参考自己的存储空间,以查看用户是否在线.
You should create JavaScript code that "pings" (sends a request, could be blank) the server via ajax. This request should in turn "mark" that user active in PHP (maybe in a database, or whatever storage method you use). You can refer to that storage of yours to see if the user is online.
或者,您可以在不使用AJAX的情况下使用它,但是您需要在站点中的每个页面请求(每次遍历页面)上执行此操作.这样,只要用户登录,PHP就会为用户标记"访问的每个页面.
Alternatively, you can use it without AJAX, however you need to do this "ping" every page request (everytime you traverse pages) in the site. In this manner, PHP "marks" the user for every page visited as long as he's logged in.
如果该用户被标记"为活动的时间超过某个阈值(例如说30分钟),请认为他处于离线状态.
If that user was "marked" active for more than a certain threshold (like say 30 mins), consider him offline.
您不需要此功能的IP地址.您只需要知道该用户仍被标记为在线即可.
you DON'T need an IP address for this functionality. You just need to know that the user is still being marked as online.
这篇关于如何检查访客的IP地址是否仍处于活动状态或在线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!