国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 數(shù)據(jù)庫(kù) > MySQL > 正文

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

2024-07-24 12:43:04
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

PHP實(shí)例教程:網(wǎng)站在線(xiàn)人數(shù)的程序代碼,后臺(tái)有MYSQL數(shù)據(jù)庫(kù)支持。可以直接統(tǒng)計(jì)出網(wǎng)站當(dāng)前的在線(xiàn)人數(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的用戶(hù)名

$db_pass = "password"; //你的mysql的密碼

$database = "users"; //表的名字

設(shè)置統(tǒng)計(jì)的時(shí)間(多少秒內(nèi)在線(xiàn)人數(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)有密碼的話(huà)可以使用下面代碼連接(當(dāng)然建議大家一定要設(shè)置好自己的密碼,這樣起碼黑客得要解密啊)

mysql_connect($server, $db_user);

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

mysql_db_query('database', 'query');

我們只要有訪(fǎng)客就要增加一條記錄。

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

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

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

然后我們得實(shí)現(xiàn)當(dāng)超過(guò)我們?cè)O(shè)置的時(shí)間我們就要?jiǎng)h除該用戶(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ì)用戶(hù),代碼如下。

$user = mysql_num_rows($result);

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

mysql_close();

顯示在線(xiàn)的人數(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");
}
?>

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 成武县| 察隅县| 庄浪县| 聂荣县| 兴文县| 东阳市| 衡阳县| 连云港市| 马尔康县| 刚察县| 乌审旗| 北安市| 浮梁县| 泰来县| 通州市| 八宿县| 乐平市| 柯坪县| 调兵山市| 玉山县| 夏津县| 滦南县| 八宿县| 璧山县| 元谋县| 宜宾县| 云南省| 科技| 三明市| 都匀市| 襄樊市| 方城县| 泗洪县| 湟源县| 博兴县| 丹巴县| 天津市| 滨海县| 合江县| 庄浪县| 瑞昌市|