萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> 顯示當前在線人數

顯示當前在線人數

介紹:

  我們可以通過程序來顯示當前的數據。

  注重的兩點:1.必須把下列代碼放到擴展名為.PHP的文件中;2.在你的服務器上運行代碼時,你的服務器必須支持MYSQL數據庫。

  代碼如下:

  下列代碼放到你想顯示信息的地方。

<?php

//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<$timeout");

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("$usern");

} else {

print("$usern");

}

?>

  最後在MYSQL中修改你的 username 和 passwords

copyright © 萬盛學電腦網 all rights reserved