捌玖网络工作室's Archiver

lilcy88 发表于 2008-5-28 10:40

刚用PHP写了一个网站在线人数的程序,请大家进来指点下!

刚用PHP写了一个网站在线人数的程序,请大家进来指点下! y5P q?nUih
我是用PHP+MYSQL来写的,原理:网站在线人数的程序代码+后台有MYSQL数据库支持,可以直接统计出网站当前的在线人数。
.V*Gt~K @l 首先我创建MYSQL数据库表。
4JUj!^kk/w CREATE TABLE tablename (Pnx;o X(^
field type(max_length) DEFAULT 'default_value' (NOT) NULL
oi(i1F h Lj@D }可以使用的SQL语句。
SE"OS.} V~ CREATE TABLE useronline (
N#k'[b0])J timestamp int(15) DEFAULT '0' NOT NULL,
-NJou6?6k ip varchar(40) NOT NULL,
%nw;Cb6u'g?7Rt file varchar(100) NOT NULL,
!~ij okR PRIMARY KEY (timestamp),
Z/a T%? s2gE KEY ip (ip),N;\5B*sjt)k{ Y(Fm
KEY file (file)4~f Nt]4Q1tv3eJ
);下面我们是PHP脚本,首先我定义MYSQL的信息。*OQ#yzy$x \
$server = "localhost"; //你的服务器
4b"CKd^D:}i $db_user = "root"; //你的mysql的用户名 b4vOf{O#A
$db_pass = "password"; //你的mysql的密码
5csb9jR i x K $database = "users"; //表的名字设置统计的时间(多少秒内在线人数)
-z+GTz^?!rG*gm $timeoutseconds = 300;取当前时间。
Z+h1Zs K8D $timestamp = time();上面的完整代码:
Ak4md:gL6b <?php
X1](N@&dMi)O0B8i $server = "localhost"; //your server
+^+s3R5_N?9h $db_user = "root"; //your mysql database username7WjM0@)`rQ
$db_pass = "password"; //your mysql database password if any
m`X_}.| $database = "users"; //the db name
dW*Uc6U+c $timeoutseconds = 300;//timeoutseconds limit
c"FW!D Yz fQ //get the current time
V(tm\2uF $timestamp = time();
!Y ]pXP*@&rJ D;T //calculate the lowest timestamp allowed
yNH:P} K $timeout = $timestamp-$timeoutseconds;"VviBczZ%a
?>连接mysqlReL NF w*s+I
mysql_connect('localhost', 'username', 'password');也允许使用变量形式。
1N,ZF7sy/V0~ mysql_connect($server, $db_user, $db_pass);如果mysql数据库没有密码的话可以使用下面代码连接.A | L'J@*moi
mysql_connect($server, $db_user);查询数据库的代码:\Lm ^e1i#zf
mysql_db_query('database', 'query');我们只要有访客就要增加一条记录。
7w0B U5]3X8P $insert = mysql_db_query($database, "INSERT INTO useronline VALUES
"jy7u%b3E#O| ('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");$XQR2B0VcQa5A:? T*N
如果用户用错误信息的话,这样处理。
DEr1fa(KNz i)~ if(!($insert)) {
i#hBa{1}gyhO8mD print "Useronline Insert Failed > ";/Xd6`yn'~v\7|
}然后实现当超过设置的时间就删除该用户记录。
FM h.e+N P.K $delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");同样给出删除记录出错的处理。E4D2@8` u)x+qE Q;^ J
if(!($delete)) { @-Dq p ?5\/E$SN
print "Useronline Delete Failed > ";
Y y|5S$cX }下面我们解决数据库中不同IP的问题-F6qr,rKp\Q?
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");使用kn7S:R4i
mysql_num_rows(query);来统计用户,代码如下。
Q*?4Uo8W $user = mysql_num_rows($result);最后关闭数据库。:HB?^.h8A
mysql_close();显示在线的人数。
(oMzf/f(Q [ if($user == 1) {
8aS7B(b!e.}cffE!p#w print("1 user online\n");
{B_x*} } else {
t t6h*H2x;s.H4w print("$user users online\n");
(H-n z"b&Pja }最终把上面代码写成一个PHP文件如下。
E,^b}Q_ s <?php [D7zs%YEI1{7v
//Put your basic server info here
TIoZ}y9G9p*E $server = "localhost"; //normally localhost
#x+p[P7?r8X $db_user = "root"; //your MySQL database username
0j(O1g4Z~HD(V-|#^ $db_pass = "password"; //your MySQL database password
E0L;Icv.N $database = "users";h,q-TR`!o(p
$timeoutseconds = 300; //it will delete all people which haven't refreshed(so probbably are
g0qo I)t // offline or inactive) in $timieoutseconds time (so it actually checks the people that are active in the last
V6{j]i8N+h$n // $timeoutseconds seconds)O f,MS P
//this is where PHP gets the time
;q9U2UfAG $timestamp = time();
H0Z6cHA5O e I2a //counts the timeout, all people which have been seen last online in earlier than this timestamp, will get removed
:y.e?xX6]q2K $timeout = $timestamp-$timeoutseconds;j}Ej1m9w'Wa/pi
//connect to databaseMoTPVEl)G'S2}2u
mysql_connect($server, $db_user);
tGh0^R4m //add the timestamp from the user to the online list4j|)ZJ6Fi} [0|
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
!]c4aD,_a Zk1r B ('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
@^'VTM r&Zs if(!($insert)) {K,il}s
print "Useronline Insert Failed > ";G/^,z{ a7G L3mb2l5}_q
}3n!g2R6lh
//delete the peoples which haven't been online/active in the last $timeoutseconds seconds.
VcM:o?R7}!V $delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");
ek!`4^:oA6N G9~3z2O if(!($delete)) {I5b v#U9WrHm
print "Useronline Delete Failed > ";hbYm WJ!s
}1RD:{$_7q~
//select the amount of people online, all uniques, which are online on THIS pageUH.{ ["gp`"i
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");
J0^t u9t4G if(!($result)) {/g E?!l~7h
print "Useronline Select Error > ";
k-E&AX,F0kEd+S }
c!~r"a:yuw //Count the number of rows = the number of people online
+C%d4d+fZ ?8p9at!L $user = mysql_num_rows($result);^%]3_;}%O1a6t0F
//spit out the results)f+b:@4c-qrg;L:Q
mysql_close();(R!av+d4J+o
if($user == 1) {
!w%n#{2h }{ print("1 user online\n");
!D^wzV*~-R-re'j } else { O5zUS!{J
print("$user users online\n");3M n_Gx;~0v
}#L(^!Yz j2U9_Nh
?>8R0vyU)]s
[url=http://www.now.cn/vhost/][img]http://b.todayisp.com/bbs/760x90h.gif[/img][/url]
2[1tuC"xBVN`!^;eE 以上代码我是在时代互联提供的免费试用主机上测试的,感觉效果还不错,性能很稳定,不信的话你也可以去试试。当然,记得指出我这个程序中的不足先哦。]W}:@-H`I3}K ^
时代互联是行业内最早开始提供PHP 虚拟主机的公司之一,现在已经是的金牌域名注册商和中国互联网客户满意度十佳单位,产品功能已经非常完善 ,特别提一下几个特有用的功能:数据库自动备份、免费集成繁简通简装版、多域名绑定、多子网站支持、GCDN 网站加速器、镜像加速、高级访问统计系统、支持WAP、可选电信/网通/香港机房。
Mif#X_e"g,| 我之前建站的时候选的是他们公司标准商务E型,因为这款机型还特别支持ASP.NET3.5(3.0/2.0/1.1)/ASP。
.zb/CeZ 当然啦,这两款主机也是相当不错的。
8zM5E"zkL4Gr 智强商务B型:Unix /Linux 操作系统+独立网页空间1000MB+送1000MB邮局空间+100MB MySQL数据库空间=1869元/年
m]o;j@7i 标准商务B型:Unix /Linux 操作系统+独立网页空间500MB+送800MB邮局空间+60MB MySQL数据库空间=1365元/年4p@J(ti
提供一下这个公司的联系方式:请见:[url=http://www.now.cn/vhost/]http://www.now.cn/vhost/[/url]
+P`D["N(x 空间专线: 0756-2623871  QQ:168075865,好象还有全球免费网络电话:全球免费咨询电话 [url=http://www.now.cn/callcenter/call.net?LineName=55]http://www.now.cn/callcenter/call.net?LineName=55[/url]-C _h1Z mN
自己加QQ去问吧。

页: [1]
【捌玖网络】已经运行:


Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.