PHP+MYSQL實(shí)例:網(wǎng)站在線人數(shù)的代碼

2022-06-12發(fā)布者:ylm大?。?/span> 下載:0

文件大小:

軟件介紹

首先是創(chuàng)建MYSQL數(shù)據(jù)庫表。

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的信息。

= "localhost"; //你的服務(wù)器
= "root"; //你的mysql的用戶名
= "password"; //你的mysql的密碼
= "users"; //表的名字

設(shè)置統(tǒng)計(jì)的時(shí)間(多少秒內(nèi)在線人數(shù))

= 300;

取當(dāng)前時(shí)間。

= time();

上面的完整代碼:

= "localhost"; //your server
= "root"; //your mysql database username
= "password"; //your mysql database password if any
= "users"; //the db name
= 300;//timeoutseconds limit
//get the current time
= time();
//calculate the lowest timestamp allowed
= -;
?>

連接mysql

mysql_connect(localhost, username, password);

也允許使用變量形式。

mysql_connect(, , );

如果mysql數(shù)據(jù)庫沒有密碼的話可以使用下面代碼連接(當(dāng)然建議大家一定要設(shè)置好自己的密碼,這樣起碼黑客得要解密啊)

mysql_connect(, );

查詢數(shù)據(jù)庫的代碼:

mysql_db_query(database, query);

我們只要有訪客就要增加一條記錄。

= mysql_db_query(, "INSERT INTO useronline VALUES
(,".58.20.26.170.","./jiaocheng/sc.php.")");

然后我們給出如果用戶用錯誤信息的處理方式。

if(!()) {
print "Useronline Insert Failed > ";
}

然后我們得實(shí)現(xiàn)當(dāng)超過我們設(shè)置的時(shí)間我們就要刪除該用戶記錄。

= mysql_db_query(, "DELETE FROM useronline WHERE timestamp<");

同樣給出刪除記錄出錯的處理。

if(!()) {
print "Useronline Delete Failed > ";
}

下面我們顯示數(shù)據(jù)庫中有多少個(gè)不同的IP

= mysql_db_query(, "SELECT DISTINCT ip FROM useronline WHERE file="./jiaocheng/sc.php." ");

我們使用mysql_num_rows(query);來統(tǒng)計(jì)用戶,代碼如下:

= mysql_num_rows();

最后我們要關(guān)閉數(shù)據(jù)庫。

mysql_close();

顯示在線的人數(shù)。

if( == 1) {
print("1 user onlinen");
} else {
print(" users onlinen");
}

最終把上面代碼寫成一個(gè)PHP文件如下。

//Put your basic server info here
= "localhost"; //normally localhost
= "root"; //your MySQL database username
= "password"; //your MySQL database password
= "users";
= 300; //it will delete all people which havent refreshed(so probbably are
// offline or inactive) in time (so it actually checks the people that are active in the last
// seconds)
//this is where PHP gets the time
= time();
//counts the timeout, all people which have been seen last online in earlier than this timestamp, will get removed
= -;
//connect to database
mysql_connect(, );
//add the timestamp from the user to the online list
= mysql_db_query(, "INSERT INTO useronline VALUES
(,".58.20.26.170.","./jiaocheng/sc.php.")");
if(!()) {
print "Useronline Insert Failed > ";
}
//delete the peoples which havent been online/active in the last seconds.
= mysql_db_query(, "DELETE FROM useronline WHERE timestamp<");
if(!()) {
print "Useronline Delete Failed > ";
}
//select the amount of people online, all uniques, which are online on THIS page
= mysql_db_query(, "SELECT DISTINCT ip FROM useronline WHERE file="./jiaocheng/sc.php." ");
if(!()) {
print "Useronline Select Error > ";
}
//Count the number of rows = the number of people online
= mysql_num_rows();
//spit out the results
mysql_close();
if( == 1) {
print("1 user onlinen");
} else {
print(" users onlinen");
}
?>

發(fā)表評論(共0條評論)
請自覺遵守互聯(lián)網(wǎng)相關(guān)政策法規(guī),評論內(nèi)容只代表網(wǎng)友觀點(diǎn),發(fā)表審核后顯示!

版權(quán)聲明:

1 本站所有資源(含游戲)均是軟件作者、開發(fā)商投稿,任何涉及商業(yè)盈利目的均不得使用,否則產(chǎn)生的一切后果將由您自己承擔(dān)!

2 本站將不對任何資源負(fù)法律責(zé)任,所有資源請?jiān)谙螺d后24小時(shí)內(nèi)刪除。

3 若有關(guān)在線投稿、無法下載等問題,請與本站客服人員聯(lián)系。

4 如侵犯了您的版權(quán)、商標(biāo)等,請立刻聯(lián)系我們并具體說明情況后,本站將盡快處理刪除,聯(lián)系QQ:2499894784

返回頂部