且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

计算我的网站访问者访问我网站的确切时间。

更新时间:2023-12-02 16:28:04

嗯......在实践中你不能准确,即使你强迫他们登录和退出。



没有什么可以告诉你的服务器这个用户已经离开了这个页面(更不用说这个用户已经停止了阅读本页并正在查看新闻) - 特别是如果你没有会话提供可靠的使用结束指示器。



那里是在javascript中检测页面关闭的方法 - 但它们并不总是有效 - 并且没有会话就没有办法将它们与用户开始阅读它的日期和时间联系起来。如果没有别的,同一个路由器上的所有用户都获得相同的IP地址,所以如果你有两个用户,你就不知道关闭页面的用户何时打开它。



你认为自己需要做什么?可能有更好的解决方案。
Um...in practice you can't be accurate, even if you force them to log in and out.

There is nothing that tells your server "this user has moved away from this page" (much less "this user has stopped reading this page and is looking at the news instead") - particularly if you don't have a Session to provide a solid "end of use" indicator.

There are ways to detect that a page is closed in javascript - but they don't always work - and without a session there is no way to tie even them to the date and time that the user started reading it. If nothing else, all users on the same router get the same IP address, so if you have two users, you don't know when the user closing the page opened it.

What are you trying to do that you think you need this? There may be a better solution.


您可以做的***是在每个页面上检查用户是否有您定义的cookie,如果没有创建具有当前date.\\time的cookie 。如果您读取此cookie的值并从当前日期时间中减去该值,那么这将让您知道他们在该请求时已经在您的网站上停留了多长时间。



您不知道用户何时关闭浏览器或导航离开您的网站,并且您无法根据此数据将内容推送到客户端,因此对您来说可能毫无用处。
Best you could do is that on every page check if the user has a cookie you define, and if not create one that has the current date\time. If you read the value of this cookie and subtract it from the current datetime then that'll let you know how long they've been on your site at the point of that request.

You don't know when the user is closing their browser or navigating away from your site and you can't "push" content to the client based on this data either so it's probably useless to you.