網站線上人數的程式碼,後台有MySQL(和PHP搭配之最佳組合)資料庫支援。可以直接統計出網站當前的線上人數。
首先是建立MySQL(和PHP搭配之最佳組合)資料庫表。
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,
Prima(最完善的虛擬機器主機管理系統)RY KEY (timestamp),
KEY ip (ip),
KEY file (file)
);
下面我們開始使用PHP指令碼,首先定義MySQL(和PHP搭配之最佳組合)的資訊。
$server = "localhost"; //你的伺服器
$db_user = "root"; //你的MySQL(和PHP搭配之最佳組合)的使用者名稱
$db_pass = "password"; //你的MySQL(和PHP搭配之最佳組合)的密碼
$database = "users"; //表的名字
設定統計的時間(多少秒內線上人數)
$timeoutseconds = 300;
取目前時間。
$timestamp = time();
上面的完整代碼:
<?php
$server = "localhost"; //your server
$db_user = "root"; //your MySQL(和PHP搭配之最佳組合) database username
$db_pass = "password"; //your MySQL(和PHP搭配之最佳組合) 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(和PHP搭配之最佳組合)
MySQL(和PHP搭配之最佳組合)_connect('localhost', 'username', 'password');
也允許使用變數形式。
MySQL(和PHP搭配之最佳組合)_connect($server, $db_user, $db_pass);
如果MySQL(和PHP搭配之最佳組合)資料庫沒有密碼的話可以使用下面代碼串連(當然建議大家一定要設定好自己的密碼,這樣起碼駭客得要解密啊)
MySQL(和PHP搭配之最佳組合)_connect($server, $db_user);
查詢資料庫的代碼:
MySQL(和PHP搭配之最佳組合)_db_query('database', 'query');
我們只要有訪客就要增加一條記錄。
$insert = MySQL(和PHP搭配之最佳組合)_db_query($database, "INSERT INTO useronline VALUES
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
然後我們給出如果使用者用錯誤資訊的處理方式。
if(!($insert)) {
print "Useronline Insert Failed > ";
}
然後我們得實現當超過我們設定的時間我們就要刪除該使用者記錄。
$delete = MySQL(和PHP搭配之最佳組合)_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");
同樣給出刪除記錄出錯的處理。
if(!($delete)) {
print "Useronline Delete Failed > ";
}
下面我們顯示資料庫中有多少個不同的IP
$result = MySQL(和PHP搭配之最佳組合)_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");
我們使用MySQL(和PHP搭配之最佳組合)_num_rows(query);來統計使用者,代碼如下:
$user = MySQL(和PHP搭配之最佳組合)_num_rows($result);
最後我們要關閉資料庫。
MySQL(和PHP搭配之最佳組合)_close();
顯示線上的人數。
if($user == 1) {
print("1 user onlinen");
} else {
print("$user users onlinen");
}
最終把上面代碼寫成一個PHP檔案如下。
<?php
//Put your basic server info here
$server = "localhost"; //normally localhost
$db_user = "root"; //your MySQL(和PHP搭配之最佳組合) database username
$db_pass = "password"; //your MySQL(和PHP搭配之最佳組合) 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(和PHP搭配之最佳組合)_connect($server, $db_user);
//add the timestamp from the user to the online list
$insert = MySQL(和PHP搭配之最佳組合)_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(和PHP搭配之最佳組合)_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(和PHP搭配之最佳組合)_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(和PHP搭配之最佳組合)_num_rows($result);
//spit out the results
MySQL(和PHP搭配之最佳組合)_close();
if($user == 1) {
print("1 user onlinen");
} else {
print("$user users onlinen");
}
?>
本篇文章來自<A href='http://www.soidc.net'>IDC專家網</a> 原文連結:http://www.soidc.net/articles/1213781103032/20080721/1215945385357_1.html