剛用PHP寫了一個(gè)網(wǎng)站在線人數(shù)的程序,請(qǐng)大家進(jìn)來(lái)指點(diǎn)下!
我是用PHP+MYSQL來(lái)寫的,原理:網(wǎng)站在線人數(shù)的程序代碼+后臺(tái)有MYSQL數(shù)據(jù)庫(kù)支持,可以直接統(tǒng)計(jì)出網(wǎng)站當(dāng)前的在線人數(shù)。
首先我創(chuàng)建MYSQL數(shù)據(jù)庫(kù)表。
CREATE TABLE tablename (
field type(max_length) DEFAULT 'default_value' (NOT) NULL
}可以使用的SQL語(yǔ)句。
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"; //你的服務(wù)器
$db_user = "root"; //你的mysql的用戶名
$db_pass = "password"; //你的mysql的密碼
$database = "users"; //表的名字設(shè)置統(tǒng)計(jì)的時(shí)間(多少秒內(nèi)在線人數(shù))
$timeoutseconds = 300;取當(dāng)前時(shí)間。
$timestamp = time();上面的完整代碼:
$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數(shù)據(jù)庫(kù)沒(méi)有密碼的話可以使用下面代碼連接
mysql_connect($server, $db_user);查詢數(shù)據(jù)庫(kù)的代碼:
mysql_db_query('database', 'query');我們只要有訪客就要增加一條記錄。
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
如果用戶用錯(cuò)誤信息的話,這樣處理。
if(!($insert)) {
print "Useronline Insert Failed > ";
}然后實(shí)現(xiàn)當(dāng)超過(guò)設(shè)置的時(shí)間就刪除該用戶記錄。
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp
if(!($delete)) {
print "Useronline Delete Failed > ";
}下面我們解決數(shù)據(jù)庫(kù)中不同IP的問(wèn)題
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");使用
mysql_num_rows(query);來(lái)統(tǒng)計(jì)用戶,代碼如下。
$user = mysql_num_rows($result);最后關(guān)閉數(shù)據(jù)庫(kù)。
mysql_close();顯示在線的人數(shù)。
if($user == 1) {
print("1 user online\n");
} else {
print("$user users online\n");
}最終把上面代碼寫成一個(gè)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
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");
}
?>
?
?
PHP怎么學(xué)習(xí)?PHP怎么入門?PHP在哪學(xué)?PHP怎么學(xué)才快?不用擔(dān)心,這里為大家提供了PHP速學(xué)教程(入門到精通),有需要的小伙伴保存下載就能學(xué)習(xí)啦!
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)