如何跟踪我网站用户的在线状态? [英] How to Track the Online Status of Users of my WebSite?

查看:56
本文介绍了如何跟踪我网站用户的在线状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想跟踪当前在线的用户.

在线的定义是当他们在网站的索引页面上时有聊天功能.

到目前为止,我能想到的就是为用户设置一个 cookie,当下次访问时发现 cookie 时,会调用 ajax 来更新包含用户名、在线状态和时间的表.

现在我的实际问题是,我怎样才能在他们离开时可靠地将他们的状态关闭网站?我唯一能想到的就是设置一个预定的无用户交互时间,然后将状态设置为关闭.

但我真正想要的是保持状态on,只要他们在网站上,有或没有互动,只有在他们离开时才off网站.

解决方案

完整解决方案.从头到尾.

如果您只想在 index.php 页面上执行此操作,您可以异步(AJAX 样式)向服务器发送更新,提醒服务器 $_SESSION[userid"] 仍然存在在线.

setInterval("update()", 10000);//每 10 秒更新一次函数更新(){$.post(update.php");//发送请求到 update.php}

你的 update.php 文件会有一些像这样的代码:

session_start();if ($_SESSION["userid"])updateUserStatus($_SESSION["userid"]);

这一切都假设您在用户登录您的网站时将您的用户 ID 存储为会话变量.updateUserStatus() 函数只是一个简单的查询,如下所示:

更新用户SET lastActiveTime = NOW()WHERE userid = $userid

这样就可以照顾您的存储空间.现在检索在线"的用户列表.为此,您需要另一个 jQuery 调用和另一个 setInterval() 调用:

setInterval("getList()", 10000)//每10秒获取用户在线函数 getList() {$.post(getList.php", function(list) {$("listBox").html(list);});}

此函数每 10 秒从服务器请求一些 HTML.getList.php 页面如下所示:

session_start();if (!$_SESSION["userid"])死;//不要将列表提供给未登录的任何人$users = getOnlineUsers();/* 获取所有具有 lastActiveTime 的用户最后一分钟*/$output = "
    ";foreach ($users as $user) {$output .= "
  • ".$user["userName"]."
  • ";}$output .= "</ul>";打印 $output;

这将输出以下 HTML:

    <li>乔纳森·桑普森</li><li>Paolo Bergantino</li><li>约翰斯基特</li>

该列表包含在名为list"的 jQuery 变量中.回顾我们最后一个 jQuery 块,你会在那里看到它.

jQuery 将获取此列表,并将其放置在类名为listBox"的 div 中.

希望这能让你前进.

I want to track users that are online at the moment.

The definition of being online is when they are on the index page of the website which has the chat function.

So far, all I can think of is setting a cookie for the user and, when the cookie is found on the next visit, an ajax call is made to update a table with their username, their status online and the time.

Now my actual question is, how can I reliably turn their status to off when they leave the website? The only thing I can think of is to set a predetermined amount of time of no user interaction and then set the status to off.

But what I really want is to keep the status on as long as they are on the site, with or without interaction, and only go to off when they leave the site.

解决方案

Full Solution. Start-to-finish.

If you only want this working on the index.php page, you could send updates to the server asynchronously (AJAX-style) alerting the server that $_SESSION["userid"] is still online.

setInterval("update()", 10000); // Update every 10 seconds

function update() {
  $.post("update.php"); // Sends request to update.php
}

Your update.php file would have a bit of code like this:

session_start();
if ($_SESSION["userid"])
  updateUserStatus($_SESSION["userid"]);

This all assumes that you store your userid as a session-variable when users login to your website. The updateUserStatus() function is just a simple query, like the following:

UPDATE users 
SET lastActiveTime = NOW()
WHERE userid = $userid

So that takes care of your storage. Now to retrieve the list of users who are "online." For this, you'll want another jQuery-call, and another setInterval() call:

setInterval("getList()", 10000) // Get users-online every 10 seconds

function getList() {
  $.post("getList.php", function(list) {
    $("listBox").html(list);
  });
}

This function requests a bit of HTML form the server every 10 seconds. The getList.php page would look like this:

session_start();
if (!$_SESSION["userid"])
  die; // Don't give the list to anybody not logged in

$users = getOnlineUsers(); /* Gets all users with lastActiveTime within the
                              last 1 minute */

$output = "<ul>";
foreach ($users as $user) {
  $output .= "<li>".$user["userName"]."</li>";
}
$output .= "</ul>";

print $output;

That would output the following HTML:

<ul>
  <li>Jonathan Sampson</li>
  <li>Paolo Bergantino</li>
  <li>John Skeet</li>
</ul>

That list is included in your jQuery variable named "list." Look back up into our last jQuery block and you'll see it there.

jQuery will take this list, and place it within a div having the classname of "listBox."

<div class="listBox"></div>

Hope this gets you going.

这篇关于如何跟踪我网站用户的在线状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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