好的,所以我使用 PHP 脚本和 MySQL 来显示在线访问者的数量。我在每个页面上都调用该函数,并且需要一种方法来避免在同一会话期间来自同一个人的重复条目。
它工作正常,但每次页面加载都会增加访问者。我认为在会话变量上有一个“require_once”语句会起作用,但不行。下面是PHP代码,请帮忙!
//users online
function update_user_time()
{
session_start();
$session=session_id();
$time=time();
$time_check=$time-600; //SET TIME 10 minutes
$host="localhost"; // Host name
$username="______"; // Mysql username
$password="______"; // Mysql password
$db_name="______"; // Database name
$tbl_name="______"; // Table name
// Connect to server and select databse
mysql_connect("$host", "$username", "$password")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name WHERE session='$session'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
if($count=="0"){
$sql1="INSERT INTO $tbl_name(session, time)VALUES('$session', '$time')";
$result1=mysql_query($sql1);
}
else {
"$sql2=UPDATE $tbl_name SET time='$time' WHERE session = '$session'";
$result2=mysql_query($sql2);
}
$sql3="SELECT * FROM $tbl_name";
$result3=mysql_query($sql3);
$count_user_online=mysql_num_rows($result3);
echo "[ $count_user_online users online ]";
// if over 10 minute, delete session
$sql4="DELETE FROM $tbl_name WHERE time<$time_check";
$result4=mysql_query($sql4);
// Open multiple browser page for result
// Close connection
mysql_close();
}