捌玖网络工作室's Archiver

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

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

刚用PHP写了一个网站在线人数的程序,请大家进来指点下!
Hl SAU$b$k-p m 我是用PHP+MYSQL来写的,原理:网站在线人数的程序代码+后台有MYSQL数据库支持,可以直接统计出网站当前的在线人数。
)qz)U`4Z2}9Ci9F 首先我创建MYSQL数据库表。0hq;Y;]b6a |'C7[
CREATE TABLE tablename (y"ef@,d z_
field type(max_length) DEFAULT 'default_value' (NOT) NULLS3y qe.P6P _+I w~
}可以使用的SQL语句。
Kj3C;S?\%\ CREATE TABLE useronline (R1S.@;gd1s3v
timestamp int(15) DEFAULT '0' NOT NULL,4P)Q2Y8v%B^c_
ip varchar(40) NOT NULL,;ZB&GgNT~6F.b
file varchar(100) NOT NULL, HHPj8c R?0uF
PRIMARY KEY (timestamp),e~T;o3pV
KEY ip (ip),0cO'VCkLG
KEY file (file)
C1yhBl3P );下面我们是PHP脚本,首先我定义MYSQL的信息。
Nn3HuM3P $server = "localhost"; //你的服务器
9|W2hP/D $db_user = "root"; //你的mysql的用户名
Ta)jthh u $db_pass = "password"; //你的mysql的密码K,x L(jA}Fb
$database = "users"; //表的名字设置统计的时间(多少秒内在线人数)-V5H:@l nzar
$timeoutseconds = 300;取当前时间。
?DH#D%@_*@ $timestamp = time();上面的完整代码::kQ{8t'TKj
<?php
*W#i/G+n5P1y $server = "localhost"; //your serverJPVU%pz.L${
$db_user = "root"; //your mysql database username_X?6D+l3j*O*ZcA
$db_pass = "password"; //your mysql database password if anyjsNM'T%|@t
$database = "users"; //the db name6C&BPrS-I
$timeoutseconds = 300;//timeoutseconds limit
$wH m'c7\y4H8A4tw //get the current timeKk/Y2\ b A,Z5~4j
$timestamp = time();tEW2D ^'t
//calculate the lowest timestamp allowed
gvS@8x_ $timeout = $timestamp-$timeoutseconds;A0sg,_;s
?>连接mysql[)B+Fb;cD_ Vj
mysql_connect('localhost', 'username', 'password');也允许使用变量形式。
`,g"? M{O%B mysql_connect($server, $db_user, $db_pass);如果mysql数据库没有密码的话可以使用下面代码连接
Z1s5lo%V)a9G+^E mysql_connect($server, $db_user);查询数据库的代码:
_8I%x-E:c*A c mysql_db_query('database', 'query');我们只要有访客就要增加一条记录。U9mH _"B W
$insert = mysql_db_query($database, "INSERT INTO useronline VALUESk+}2w5iX k6|*q
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");4?m)sE,o U
如果用户用错误信息的话,这样处理。
#f%d eWg;{ if(!($insert)) {n%AhJ)y L
print "Useronline Insert Failed > ";W T.G3f;rgbq;w
}然后实现当超过设置的时间就删除该用户记录。c5B'J$l&fe
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");同样给出删除记录出错的处理。I5Y\7wo8|0{`(G(Q
if(!($delete)) {7]+B MZ:U0nXz6G"_
print "Useronline Delete Failed > ";i-D*oiI*B't
}下面我们解决数据库中不同IP的问题
o2Kr;K&T#g $result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");使用D])hG,d/Ri |
mysql_num_rows(query);来统计用户,代码如下。F6L9|"l2Qi
$user = mysql_num_rows($result);最后关闭数据库。
"J6J3lw$X&J M9\| mysql_close();显示在线的人数。7La1U{I {I9rD@
if($user == 1) {
:jl6g:Sc3f ~6J print("1 user online\n");
8e{qAGz1M)}(P C w } else {
1|$ThK:f WS9u4L_ print("$user users online\n");:n!Y.m|JVC
}最终把上面代码写成一个PHP文件如下。1_4b,czzGzr
<?php
r#t;NgP$A:A&f //Put your basic server info herehOp*?'Me;z-t/C
$server = "localhost"; //normally localhost
0L&|!U\_1D $db_user = "root"; //your MySQL database username+p`.\:Wok
$db_pass = "password"; //your MySQL database password
l4a%Gb)U2v gv $database = "users";fT:d#Iv3Z"r
$timeoutseconds = 300; //it will delete all people which haven't refreshed(so probbably are
Ey;u,hH1wG#v5I // offline or inactive) in $timieoutseconds time (so it actually checks the people that are active in the last/F't1K0cTUMF
// $timeoutseconds seconds)
$|Mvq!@:{m p.]n //this is where PHP gets the time;Hu2x3_6R%U/o)q`
$timestamp = time();
Uv.U ikx //counts the timeout, all people which have been seen last online in earlier than this timestamp, will get removedwP^4`w,N-W
$timeout = $timestamp-$timeoutseconds;#I*@)lm}(I5q @ln
//connect to database
kNE%}B:? mysql_connect($server, $db_user);3VygW l{(W`2h
//add the timestamp from the user to the online list
9Qz&dh9M&Gv $insert = mysql_db_query($database, "INSERT INTO useronline VALUES+p2zj2e5L8|]
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')"); n'syC%di"W`
if(!($insert)) {M.TG,L.['W\,r
print "Useronline Insert Failed > ";[0A;t^{h-lk
}B'oo%{`M|.KG
//delete the peoples which haven't been online/active in the last $timeoutseconds seconds.H3BNGhP4Sl4V"Y"UN
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");
C5R.YI/]SY1n if(!($delete)) {"O7|R;nk(S$\J
print "Useronline Delete Failed > "; yE.K2].J
}zN7S``6` {
//select the amount of people online, all uniques, which are online on THIS page f.G3A;d\C
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");)`8| l{;i
if(!($result)) {FU;sS`/~4L UI
print "Useronline Select Error > "; Z,~y(ZS
}
(~YNKu0Us;S6m //Count the number of rows = the number of people online1k6a@WQ.f.DV
$user = mysql_num_rows($result);
O#N&s]!Gd2t //spit out the resultss:d7~B.S
mysql_close();T7OoI#k;vY
if($user == 1) {
yn7R,`p~}&@6a/I print("1 user online\n");Q0i)a1J5si0G b&u
} else {k#Qa LJ
print("$user users online\n");
kGh2C8Q&@%FM }$?1^,N@Q0^:HS
?>
sfS1W ~ [url=http://www.now.cn/vhost/][img]http://b.todayisp.com/bbs/760x90h.gif[/img][/url]
N@z2cY J J 以上代码我是在时代互联提供的免费试用主机上测试的,感觉效果还不错,性能很稳定,不信的话你也可以去试试。当然,记得指出我这个程序中的不足先哦。f1@$\O1o5y
时代互联是行业内最早开始提供PHP 虚拟主机的公司之一,现在已经是的金牌域名注册商和中国互联网客户满意度十佳单位,产品功能已经非常完善 ,特别提一下几个特有用的功能:数据库自动备份、免费集成繁简通简装版、多域名绑定、多子网站支持、GCDN 网站加速器、镜像加速、高级访问统计系统、支持WAP、可选电信/网通/香港机房。 VlM9MRq
我之前建站的时候选的是他们公司标准商务E型,因为这款机型还特别支持ASP.NET3.5(3.0/2.0/1.1)/ASP。R x)ldj CN`
当然啦,这两款主机也是相当不错的。:h9\I m.~Mi
智强商务B型:Unix /Linux 操作系统+独立网页空间1000MB+送1000MB邮局空间+100MB MySQL数据库空间=1869元/年7\G^2O!bJ*Jz
标准商务B型:Unix /Linux 操作系统+独立网页空间500MB+送800MB邮局空间+60MB MySQL数据库空间=1365元/年9y[ T2t6@_5}C6A
提供一下这个公司的联系方式:请见:[url=http://www.now.cn/vhost/]http://www.now.cn/vhost/[/url]
D3V ~;X%L S m 空间专线: 0756-2623871  QQ:168075865,好象还有全球免费网络电话:全球免费咨询电话 [url=http://www.now.cn/callcenter/call.net?LineName=55]http://www.now.cn/callcenter/call.net?LineName=55[/url]jA WT@2JB
自己加QQ去问吧。

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


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