日韩天天综合网_野战两个奶头被亲到高潮_亚洲日韩欧美精品综合_av女人天堂污污污_视频一区**字幕无弹窗_国产亚洲欧美小视频_国内性爱精品在线免费视频_国产一级电影在线播放_日韩欧美内地福利_亚洲一二三不卡片区

通過(guò)PHP MYSQL實(shí)現(xiàn) 網(wǎng)站在線(xiàn)人數(shù)統(tǒng)計(jì)_PHP教程

編輯Tag賺U幣
教程Tag:暫無(wú)Tag,歡迎添加,賺取U幣!

推薦:《PHP設(shè)計(jì)模式介紹》關(guān)于本書(shū)說(shuō)明
中國(guó)站長(zhǎng)站關(guān)于《PHP設(shè)計(jì)模式介紹》的說(shuō)明 《PHP設(shè)計(jì)模式介紹》一書(shū)的英文原名是“ Guide.to.PHP.Design.Patterns ”,此書(shū)由Marco Tabini & Associates, Inc.出版,原作者是Ja

網(wǎng)站在線(xiàn)人數(shù)的程序代碼,后臺(tái)有MySQL(和PHP搭配之最佳組合)數(shù)據(jù)庫(kù)支持�?梢灾苯咏y(tǒng)計(jì)出網(wǎng)站當(dāng)前的在線(xiàn)人數(shù)。

首先是創(chuàng)建MySQL(和PHP搭配之最佳組合)數(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,
Prima(最完善的虛擬主機(jī)管理系統(tǒng))RY KEY (timestamp),
KEY ip (ip),
KEY file (file)
);

下面我們開(kāi)始使用PHP腳本,首先定義MySQL(和PHP搭配之最佳組合)的信息。

$server = "localhost"; //你的服務(wù)器
$db_user = "root"; //你的MySQL(和PHP搭配之最佳組合)的用戶(hù)名
$db_pass = "password"; //你的MySQL(和PHP搭配之最佳組合)的密碼
$database = "users"; //表的名字


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

$timeoutseconds = 300;

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

$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搭配之最佳組合)數(shù)據(jù)庫(kù)沒(méi)有密碼的話(huà)可以使用下面代碼連接(當(dāng)然建議大家一定要設(shè)置好自己的密碼,這樣起碼黑客得要解密�。�
MySQL(和PHP搭配之最佳組合)_connect($server, $db_user);

查詢(xún)數(shù)據(jù)庫(kù)的代碼:
MySQL(和PHP搭配之最佳組合)_db_query('database', 'query');


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

$insert = MySQL(和PHP搭配之最佳組合)_db_query($database, "INSERT INTO useronline VALUES
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");

然后我們給出如果用戶(hù)用錯(cuò)誤信息的處理方式。

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

然后我們得實(shí)現(xiàn)當(dāng)超過(guò)我們?cè)O(shè)置的時(shí)間我們就要?jiǎng)h除該用戶(hù)記錄。

$delete = MySQL(和PHP搭配之最佳組合)_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");

同樣給出刪除記錄出錯(cuò)的處理。

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


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

$result = MySQL(和PHP搭配之最佳組合)_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");


我們使用MySQL(和PHP搭配之最佳組合)_num_rows(query);來(lái)統(tǒng)計(jì)用戶(hù),代碼如下:

$user = MySQL(和PHP搭配之最佳組合)_num_rows($result);


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

MySQL(和PHP搭配之最佳組合)_close();


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

if($user == 1) {
print("1 user online\n");
} else {
print("$user users online\n");
}


最終把上面代碼寫(xiě)成一個(gè)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 online\n");
} else {
print("$user users online\n");
}
?>

分享:《PHP設(shè)計(jì)模式介紹》第十八章 結(jié)論
我們已經(jīng)一起進(jìn)行到這次討論旅程的最后一個(gè)部分了。希望我們一起完成了對(duì)你有用的東西,包括了通過(guò)PHP的一些例子來(lái)介紹設(shè)計(jì)模式的概念和更有用的成果,比如測(cè)試驅(qū)動(dòng)開(kāi)發(fā)這樣的更好的開(kāi)發(fā)技術(shù)。

來(lái)源:模板無(wú)憂(yōu)//所屬分類(lèi):PHP教程/更新時(shí)間:2008-08-22
相關(guān)PHP教程