PHP+MYSQL實(shí)例--網(wǎng)站在線人數(shù)的程序代碼_PHP教程
推薦:PHP天氣查詢實(shí)例代碼?php city_name=_GET['city']; //用正則在city.txt中查找相應(yīng)城市的天氣城市代碼 city_name=trim(ereg_replace([ ]{1,},,city_name)); time_file=city.txt; city_all=file_get_contents(time_file); preg=|city_name,(.*)\s|i; preg_match_all(preg,
網(wǎng)站在線人數(shù)的程序代碼,后臺(tái)有MYSQL數(shù)據(jù)庫支持�?梢灾苯咏y(tǒng)計(jì)出網(wǎng)站當(dāng)前的在線人數(shù)。
首先是創(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的信息。
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();
上面的完整代碼:
<?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數(shù)據(jù)庫沒有密碼的話可以使用下面代碼連接(當(dāng)然建議大家一定要設(shè)置好自己的密碼,這樣起碼黑客得要解密�。�
mysql_connect(server, db_user);
查詢數(shù)據(jù)庫的代碼:
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)超過我們設(shè)置的時(shí)間我們就要?jiǎng)h除該用戶記錄。
delete = mysql_db_query(database, "DELETE FROM useronline WHERE timestamp<timeout");
同樣給出刪除記錄出錯(cuò)的處理。
if(!(delete)) {
print "Useronline Delete Failed > ";
}
下面我們顯示數(shù)據(jù)庫中有多少個(gè)不同的IP
result = mysql_db_query(database, "SELECT DISTINCT ip FROM useronline WHERE file='"._SERVER['PHP_SELF']."' ");
我們使用
mysql_num_rows(query);
來統(tǒng)計(jì)用戶,代碼如下。
user = mysql_num_rows(result);
最后我們要關(guān)閉數(shù)據(jù)庫。
mysql_close();
顯示在線的人數(shù)。
if(user == 1) {
print("1 user online\n");
} else {
print("user users online\n");
}
最終把上面代碼寫成一個(gè)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");
}
?>
分享:PHP技巧:PHP中密碼加密函數(shù)function ChangeMsg(msgu,msgp) { if(msgu!= msgp!=) { delmsg = md5(msgu); rname = substr(delmsg,5,1).,.substr(delmsg,7,1).,.substr(delmsg,15,1).,.substr(delmsg,17,1); rnamearray = explode(',',rname); rpass = md5(msgp); r_msg = str_
- PHPNOW安裝Memcached擴(kuò)展方法詳解
- php記錄頁面代碼執(zhí)行時(shí)間
- PHP中獎(jiǎng)概率的抽獎(jiǎng)算法程序代碼
- apache設(shè)置靜態(tài)文件緩存方法介紹
- php對圖像的各種處理函數(shù)代碼小結(jié)
- PHP 關(guān)于訪問控制的和運(yùn)算符優(yōu)先級介紹
- 關(guān)于PHP語言構(gòu)造器介紹
- php/js獲取客戶端mac地址的實(shí)現(xiàn)代碼
- php5.5新數(shù)組函數(shù)array_column使用
- PHP preg_match的匹配多國語言的技巧
- php 中序列化和json使用介紹
- php采集文章中的圖片獲取替換到本地
- 相關(guān)鏈接:
- 教程說明:
PHP教程-PHP+MYSQL實(shí)例--網(wǎng)站在線人數(shù)的程序代碼
。