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

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

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

2024-07-24 12:57:10
字體:
來源:轉載
供稿:網(wǎng)友
,歡迎訪問網(wǎng)頁設計愛好者web開發(fā)。

php實例教程:網(wǎng)站在線人數(shù)的程序代碼,后臺有mysql數(shù)據(jù)庫支持。可以直接統(tǒng)計出網(wǎ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"; //你的服務器

$db_user = "root"; //你的mysql的用戶名

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

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

設置統(tǒng)計的時間(多少秒內在線人數(shù))

$timeoutseconds = 300;

取當前時間。

$timestamp = time();

上面的完整代碼:

以下為引用的內容:
$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ù)庫沒有密碼的話可以使用下面代碼連接(當然建議大家一定要設置好自己的密碼,這樣起碼黑客得要解密啊)

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']."')");

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

以下為引用的內容:
if(!($insert)) {
print "useronline insert failed > ";
}

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

$delete = mysql_db_query($database, "delete from useronline where timestamp<$timeout");

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

以下為引用的內容:
if(!($delete)) {
print "useronline delete failed > ";
}

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

$result = mysql_db_query($database, "select distinct ip from useronline where file='".$_server['php_self']."' ");

我們使用

mysql_num_rows(query);

來統(tǒng)計用戶,代碼如下。

$user = mysql_num_rows($result);

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

mysql_close();

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

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

最終把上面代碼寫成一個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");
}
?>

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 鄂托克前旗| 交城县| 八宿县| 曲松县| 扎兰屯市| 上杭县| 昔阳县| 天峻县| 马尔康县| 民丰县| 田东县| 赣榆县| 新竹县| 磐石市| 承德县| 庄河市| 洪泽县| 施甸县| 永寿县| 惠安县| 宁蒗| 保康县| 黑水县| 韶关市| 南昌市| 遂宁市| 台东县| 乌拉特前旗| 公主岭市| 隆子县| 岳阳市| 桂林市| 岑巩县| 三明市| 武义县| 离岛区| 华蓥市| 涞源县| 灌南县| 宝山区| 永川市|