PHP實例教程:網站在線人數的程序代碼,後台有MySQL數據庫支持。可以直接統計出網站當前的在線人數。
首先是創建MySQL數據庫表。
CREATE TABLE tablename (
fIEld type(max_length) DEFAULT 'default_value' (NOT) NULL
}
可以使用的SQL語句。
CREATE TABLE useronline (
timestamp int(15) DEFAULT '0' NOT NULL,
ip varchar(40) NOT NULL,
file varchar(100) NOT NULL,
PRIMARY KEY (timestamp),
KEY ip (ip),
KEY file (file)
);
下面我們開始使用PHP腳本,首先定義MySQL的信息。
$server = "localhost"; //你的服務器
$db_user = "root"; //你的MySQL的用戶名
$db_pass = "passWord"; //你的MySQL的密碼
$database = "users"; //表的名字
設置統計的時間(多少秒內在線人數)
$timeoutseconds = 300;
取當前時間。
$timestamp = time();
上面的完整代碼:
<?PHP
$server = "localhost"; //your server
$db_user = "root"; //your MySQL database username
$db_pass = "passWord"; //your MySQL database passWord if any
$database = "users"; //the db name
$timeoutseconds = 300;//timeoutseconds limit
//get the current time
$timestamp = time();
//calculate the lowest timestamp allowed
$timeout = $timestamp-$timeoutseconds;
?>
連接MySQL
MySQL_connect('localhost', 'username', 'passWord');
也允許使用變量形式。
MySQL_connect($server, $db_user, $db_pass);
如果MySQL數據庫沒有密碼的話可以使用下面代碼連接(當然建議大家一定要設置好自己的密碼,這樣起碼黑客得要解密啊)
MySQL_connect($server, $db_user);
查詢數據庫的代碼:
MySQL_db_query('database', 'query');
我們只要有訪客就要增加一條記錄。
$insert = MySQL_db_query($database, "INSERT INTO useronline VALUES
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['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 > ";
}
下面我們顯示數據庫中有多少個不同的IP
$result = MySQL_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");
我們使用MySQL_num_rows(query);來統計用戶,代碼如下:
$user = MySQL_num_rows($result);
最後我們要關閉數據庫。
MySQL_close();
顯示在線的人數。
if($user == 1) {
print("1 user online\n");
} else {
print("$user users online\n");
}
最終把上面代碼寫成一個PHP文件如下。
<?PHP
//Put your basic server info here
$server = "localhost"; //normally localhost
$db_user = "root"; //your MySQL database username
$db_pass = "passWord"; //your MySQL database passWord
$database = "users";
$timeoutseconds = 300; //it will delete all people which haven't refreshed(so probbably are
// offline or inactive) in $timIEoutseconds time (so it actually checks the people that are active in the last
// $timeoutseconds seconds)
//this is where PHP gets the time
$timestamp = time();
//counts the timeout, all people which have been seen last online in earlIEr than this timestamp, will get removed
$timeout = $timestamp-$timeoutseconds;
//connect to database
MySQL_connect($server, $db_user);
//add the timestamp from the user to the online list
$insert = MySQL_db_query($database, "INSERT INTO useronline VALUES
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
if(!($insert)) {
print "Useronline Insert Failed > ";
}
//delete the peoples which haven't been online/active in the last $timeoutseconds seconds.
$delete = MySQL_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");
if(!($delete)) {
print "Useronline Delete Failed > ";
}
//select the amount of people online, all uniques, which are online on THIS page
$result = MySQL_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");
if(!($result)) {
print "Useronline Select Error > ";
}
//Count the number of rows = the number of people online
$user = MySQL_num_rows($result);
//spit out the results
MySQL_close();
if($user == 1) {
print("1 user online\n");
} else {
print("$user users online\n");
}
?>