显示当前在线人数_PHP
程序员文章站
2022-05-16 14:13:56
...
介绍:
我们可以通过程序来显示当前的数据。
注意的两点:1.必须把下列代码放到扩展名为.PHP的文件中;2.在你的服务器上运行代码时,你的服务器必须支持MYSQL数据库。
代码如下:
下列代码放到你想显示信息的地方。
//EDIT THIS
$server = "localhost";
$db_user = "database username";
$db_pass = "database password";
$database = "database";
$timeoutseconds = 300;
//STOP EDITING
$timestamp = time();
$timeout = $timestamp-$timeoutseconds;
mysql_connect($server, $db_user, $db_pass);
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
('$timestamp','$REMOTE_ADDR','$PHP_SELF')");
if(!($insert)) {
print "Useronline Insert Failed > ";
}
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp
if(!($delete)) {
print "Useronline Delete Failed > ";
}
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='$PHP_SELF'");
if(!($result)) {
print "Useronline Select Error > ";
}
$user = mysql_num_rows($result);
mysql_close();
if($user == 1) {
print("$user\n");
} else {
print("$user\n");
}
?>
最后在MYSQL中修改你的 username 和 passwords
注意:把$应为半角
下一篇: 用php与mysql的电子贺卡代码