简体   繁体   English

如何检查访问者的IP地址是否仍处于活动状态或在线状态?

[英]How to check if visitor's IP Address is still active or online?

Is it possible to check if the visitor is still online using their IP Address? 是否可以使用他们的IP地址检查访问者是否仍在线?

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. 我已经创建了一个javascript和php来检查访问者的IP地址,当他们使用onLoad和onBeforeUnload事件打开我的一个页面。 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. 由于onBeforeUnload一直不可靠。

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. 但是,如果访问者关闭浏览器,但我的脚本无法检测到其卸载事件,我想知道他/她是否还在线。

A PHP or javascript is okay for me. PHP或javascript对我来说没问题。

You should create JavaScript code that "pings" (sends a request, could be blank) the server via ajax. 您应该创建JavaScript代码,通过ajax“ping”(发送请求,可能是空白的)服务器。 This request should in turn "mark" that user active in PHP (maybe in a database, or whatever storage method you use). 此请求应该“标记”在PHP中活动的用户(可能在数据库中,或您使用的任何存储方法)。 You can refer to that storage of yours to see if the user is online. 您可以参考您的存储空间以查看用户是否在线。

Alternatively, you can use it without AJAX, however you need to do this "ping" every page request (everytime you traverse pages) in the site. 或者,您可以在没有AJAX的情况下使用它,但是您需要在站点中“ping”每个页面请求(每次遍历页面)。 In this manner, PHP "marks" the user for every page visited as long as he's logged in. 通过这种方式,只要用户登录,PHP就会“标记”用户访问的每个页面。

If that user was "marked" active for more than a certain threshold (like say 30 mins), consider him offline. 如果该用户被“标记”活动超过某个阈值(例如30分钟),请考虑他离线。

you DON'T need an IP address for this functionality. 您不需要IP地址来实现此功能。 You just need to know that the user is still being marked as 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. 但是,如果访问者关闭浏览器,但我的脚本无法检测到其卸载事件,我想知道他/她是否还在线。

That's quite impossible. 那是不可能的。
And I doubt there is any reason to do it at all. 我怀疑有任何理由这样做。

to the best of my knowledge the most reliable way of doing it would be by using now.js for Node. 据我所知,最可靠的方法是使用now.js作为Node。 You can set up a simple server, and listen for the connect and disconnect. 您可以设置一个简单的服务器,并监听连接和断开连接。 when triggered you could send the data you need over to your php, or process it in node. 触发时,您可以将您需要的数据发送到您的php,或在节点中处理它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM