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

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

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

推薦:詳細(xì)學(xué)習(xí)PHP中對(duì)文件和目錄的操作方法
一:引論 在任何計(jì)算機(jī)設(shè)備中,文件是都是必須的對(duì)象,而在web編程中,文件的操作一直是web程序員的頭疼的地方,文件的操作在cms系統(tǒng)中這是必須的,非常有用的,我們經(jīng)常遇到生成文件目錄,文

PHP實(shí)例教程:網(wǎng)站在線人數(shù)的程序代碼,后臺(tái)有MYSQL數(shù)據(jù)庫(kù)支持。可以直接統(tǒng)計(jì)出網(wǎng)站當(dāng)前的在線人數(shù)。

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

以下為引用的內(nèi)容:
CREATE TABLE tablename (
field type(max_length) DEFAULT 'default_value' (NOT) NULL
}

可以使用的SQL語(yǔ)句。

以下為引用的內(nèi)容:
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)
);

下面我們開(kāi)始使用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();

上面的完整代碼:

以下為引用的內(nèi)容:
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");

連接mysql

mysql_connect('localhost', 'username', 'password');

也允許使用變量形式。

mysql_connect($server, $db_user, $db_pass);

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

mysql_connect($server, $db_user);

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

mysql_db_query('database', 'query');

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

以下為引用的內(nèi)容:
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");

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

以下為引用的內(nèi)容:
if(!($insert)) {
print "Useronline Insert Failed > ";
}

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

$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");

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

以下為引用的內(nèi)容:
if(!($delete)) {
print "Useronline Delete Failed > ";
}

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

$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ù)。

以下為引用的內(nèi)容:
if($user == 1) {
print("1 user online\n");
} else {
print("$user users online\n");
}

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

以下為引用的內(nèi)容:

<?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");
}
?>

分享:學(xué)習(xí)PHP:PHP的通用檢測(cè)函數(shù)總結(jié)
以下為引用的內(nèi)容: // ※CheckMoney($C_Money) 檢查數(shù)據(jù)是否是99999.99格式 // ※CheckEmailAddr($C_mailaddr) 判斷是否為有效郵件地址

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