简体   繁体   English

在SESSION_DESTROY上减少PHP中的在线用户数量

[英]On SESSION_DESTROY decrease the number of online users in PHP

I have a simple file-system-ed-login (not mysql) just to track number of users who are registered to my website. 我有一个简单的文件系统版本登录名(不是mysql),只是为了跟踪注册到我的网站的用户数。

I want to know who are all online too. 我也想知道谁都在线。

  1. When the browser is closed the session is destroyed automatically. 关闭浏览器后,会话将自动销毁。
  2. I use a explicit logout command too. 我也使用显式注销命令。
  3. I can handle the number of online users on pressing "logout" button, thus by reducing the count in file "nouser.txt" which handles the number of users who are online. 我可以通过按“注销”按钮来处理在线用户的数量,从而减少文件“ nouser.txt”中的数量,该文件可以处理在线用户的数量。

So the thing I want to know is, if the user closes his browser with out pressing "logout", though the session_destroy() clears out the login implicitly, I want to decrease the number of online users in the nouser.txt file 因此,我想知道的是,如果用户不按“注销”就关闭浏览器,尽管session_destroy()隐式清除了登录名,我希望减少nouser.txt文件中的在线用户数

edit 1: Just like handling onSession_Destroy() on closing the browser. 编辑1:就像在关闭浏览器时处理onSession_Destroy()一样。

The best is to have an AJAX call when the page is closed. 最好是在页面关闭时进行AJAX调用。

As Marc B suggested in a comment, you can use the onbeforeunload event. 正如Marc B在评论中建议的那样,您可以使用onbeforeunload事件。

<html>
<head>
<script>
function closeIt()
{
  // Perform your AJAX hit here using your favorite method (jquery or whatever).
}
window.onbeforeunload = closeIt;
</script>
</head>
<body>
  <a href="http://www.google.com">Click here to navigate to 
      www.google.com</a>
</body>
</html>

Thanks a lot for the tries Dudes. 非常感谢Dudes的尝试。

I finally realized by searching through various forums' build structure, I get that the number of users who are online is always tracked by the cookies. 通过搜索各种论坛的构建结构,我终于意识到,cookie始终跟踪在线用户的数量。

So, when a user is logged in (say for a 30-day-scheme), a flag is set in his name for a period of time and is removed after that. 因此,当用户登录时(例如30天的计划),会在其名称中设置一个标志一段时间,然后将其删除。 So the number of users shown in any forum who are online is always an inaccurate-measure. 因此,在任何论坛上显示的在线用户总数始终是不准确的指标。 :) :)

This was also confirmed by my PHP teacher (my-brain, lol), who says, "As long as the cookie or the session stays on, the user is considered to be online". 我的PHP老师(my-brain,大声笑)也证实了这一点,他说:“只要cookie或会话一直存在,该用户就被视为在线”。

But still I had one more problem. 但是我仍然有另一个问题。 How in the hell they track down the user whenever he/she is online in Google, Yahoo, Facebook.. 每当用户在Google,Yahoo,Facebook中在线时,他们到底如何跟踪用户。

I got the answer for that too. 我也得到了答案。 As MARC-B and ROYCHRI said, the sessions are held as long as the browser or browser tab is open and the user is eventually tracked down by a cookie. 正如MARC-B和ROYCHRI所说,只要打开浏览器或浏览器选项卡,会话就会一直保持,并且最终用户会被cookie跟踪。

phoo..., what a theory man. hoo ...,真是个理论家。 As I told, I actually solved my own problem ;) and I love doing it. 就像我说的,我实际上解决了自己的问题;)并且我喜欢这样做。

Once again, thanks for the try guys, remember the theory. 再次感谢您的尝试,请记住理论。

Have a great day. 祝你有美好的一天。

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

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