刚用PHP写了一个网站在线人数的程序,请大家进来指点下!
刚用PHP写了一个网站在线人数的程序,请大家进来指点下!K W5FewU*l zo ^我是用PHP+MYSQL来写的,原理:网站在线人数的程序代码+后台有MYSQL数据库支持,可以直接统计出网站当前的在线人数。
首先我创建MYSQL数据库表。5x ^f?E8[v
CREATE TABLE tablename (
field type(max_length) DEFAULT 'default_value' (NOT) NULL
}可以使用的SQL语句。j {m0Ud
CREATE TABLE useronline ( ZgcV$N2h#G)suY
timestamp int(15) DEFAULT '0' NOT NULL,
ip varchar(40) NOT NULL,
file varchar(100) NOT NULL,a&~yB6X#YE ^d
PRIMARY KEY (timestamp),
KEY ip (ip),
KEY file (file).n;E-j'p1TB:GI5k
);下面我们是PHP脚本,首先我定义MYSQL的信息。
$server = "localhost"; //你的服务器*B(_(Bs6c
$db_user = "root"; //你的mysql的用户名vE\#Q7q+^h"?
$db_pass = "password"; //你的mysql的密码
$database = "users"; //表的名字设置统计的时间(多少秒内在线人数)[:D!w9xzF'EKA,B2@:r
$timeoutseconds = 300;取当前时间。Z@n9C u.k
$timestamp = time();上面的完整代码:
<?php
$server = "localhost"; //your serverV6dH7k-an
$db_user = "root"; //your mysql database username
$db_pass = "password"; //your mysql database password if anyq9O"?2Opr_r y8A
$database = "users"; //the db name,R _U o3K
$timeoutseconds = 300;//timeoutseconds limitW/VF1Uui;{f
//get the current time
$timestamp = time();
//calculate the lowest timestamp allowedJ.e(Q@"|q1L_7N.`
$timeout = $timestamp-$timeoutseconds;+YHNI5Z0qMl
?>连接mysql$Y8C`7d&f{
mysql_connect('localhost', 'username', 'password');也允许使用变量形式。
mysql_connect($server, $db_user, $db_pass);如果mysql数据库没有密码的话可以使用下面代码连接!k9~Pm#b%mR5t
mysql_connect($server, $db_user);查询数据库的代码:$DXBS-sY
mysql_db_query('database', 'query');我们只要有访客就要增加一条记录。
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES(sw1v]1C
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");dl1n)AC e
如果用户用错误信息的话,这样处理。
if(!($insert)) {:T%{;z7MOYF
print "Useronline Insert Failed > ";
}然后实现当超过设置的时间就删除该用户记录。
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");同样给出删除记录出错的处理。
if(!($delete)) {"X|#pSc
print "Useronline Delete Failed > ";tJ:IG:k Q/w a"A/]
}下面我们解决数据库中不同IP的问题*DI:h4ZI m.]
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");使用WuH+t+[7[6o x
mysql_num_rows(query);来统计用户,代码如下。MI8t"O"f!@o
$user = mysql_num_rows($result);最后关闭数据库。GoH n"W
mysql_close();显示在线的人数。:?;V9QO2s)E0x$a8k
if($user == 1) {:}"_9G,ycU+FO)q
print("1 user online\n");
} else {
print("$user users online\n");
}最终把上面代码写成一个PHP文件如下。w6Q%iai{ ]x
<?php[ ndOF3^&L8rw/i
//Put your basic server info here
$server = "localhost"; //normally localhostiOgO|A.{\j
$db_user = "root"; //your MySQL database username Yh,k,ma
$db_pass = "password"; //your MySQL database password*d4v/@/k#A.j i
$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_'o/jWyF ~
$timestamp = time();8?]xhRO6fpI
//counts the timeout, all people which have been seen last online in earlier than this timestamp, will get removed
$timeout = $timestamp-$timeoutseconds;8_Tr7qKk qVA}3[
//connect to database
mysql_connect($server, $db_user);F f:D v7~ qk
//add the timestamp from the user to the online list
$insert = mysql_db_query($database, "INSERT INTO useronline VALUESky,l y'j8U:y+v/FV
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')"); L)f&U3J?myE
if(!($insert)) {)MA\qf S"i)j V.p
print "Useronline Insert Failed > ";l$]m6^cZK,@)?
},TeW6l p+t bJ
//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");p `{.m|3Qv
if(!($delete)) {1WK$S;l*T/\
print "Useronline Delete Failed > ";2zo-X(OFP
}.l JC&JL2u%t7e3`p
//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']."' ");7b!B wV u%H[@8U{?4g
if(!($result)) {
print "Useronline Select Error > ";
}"qCyQW%@@n
//Count the number of rows = the number of people online8fwnzj*^Ec
$user = mysql_num_rows($result);H#g]BwU7v_g7V"XI
//spit out the results
mysql_close();
if($user == 1) {