我目前正在使用此 php 代码来计算当前在线的用户数:
$session_id = session_id();
$time_stamp = time();
$time_limit = $time_stamp - 300; // We give the session only 5 minutes if it exists
$result = $this->select("SELECT * FROM `online_visitors` WHERE `session_id`='$session_id' LIMIT 1");
if (!mysql_num_rows($result)) {
$tb_col[0] = "visitor_ip";
$tb_col[1] = "country";
$tb_col[2] = "session_id";
$tb_col[3] = "time_stamp";
$tb_col[4] = "last_time_stamp";
$tb_data[0] = "'" . $this->visitor_ip . "'";
$tb_data[1] = "'" . $this->visitor_country . "'";
$tb_data[2] = "'" . $session_id . "'";
$tb_data[3] = "'" . $time_stamp . "'";
$tb_data[4] = "'" . $time_stamp . "'";
$this->insert("`online_visitors`", $tb_col, $tb_data);
} else {
$this->update("`online_visitors`", "`visitor_ip`='$this->visitor_ip', `country`='$this->visitor_country', `last_time_stamp`='$time_stamp'", "`session_id`='$session_id'");
}
$this->delete("`online_visitors`", "`last_time_stamp`<'$time_limit'");
但它不会实时更新。我想每 30 秒检查一次用户数。我可能还想将此表连接到 jQuery 心跳功能,以便更可靠地知道当前有多少用户在线。
我是否正确地接近这个?关于如何在 jQuery 中实现这一点的任何提示(我不太擅长)?或者我的方法可以改进吗?
我也想改进我的php代码