捌玖网络工作室's Archiver

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

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

刚用PHP写了一个网站在线人数的程序,请大家进来指点下!b6@X7uj6E
我是用PHP+MYSQL来写的,原理:网站在线人数的程序代码+后台有MYSQL数据库支持,可以直接统计出网站当前的在线人数。
Vq"LRn6Sj.R 首先我创建MYSQL数据库表。7vwT6N3[`
CREATE TABLE tablename (
by S1E? field type(max_length) DEFAULT 'default_value' (NOT) NULL
*` _4o%@k5ox` }可以使用的SQL语句。
Rijq G(K tX,X CREATE TABLE useronline (
r JO)a |CJ~,s'l$Bj timestamp int(15) DEFAULT '0' NOT NULL,y{-I-~u;{ H
ip varchar(40) NOT NULL,#sY;DL1}i ^
file varchar(100) NOT NULL,%]"_B&sR?$~
PRIMARY KEY (timestamp),5D2LZk&j"d#gT5kG
KEY ip (ip), d+Q;[3Zg6M7a S8nf
KEY file (file)4@R'z9@-~Y{-v dE$[
);下面我们是PHP脚本,首先我定义MYSQL的信息。F;u P6l7\G'H8v7XGV
$server = "localhost"; //你的服务器0n2PM9bBb!GN
$db_user = "root"; //你的mysql的用户名sgTr+C@~6Q;y
$db_pass = "password"; //你的mysql的密码
N"ag;j$f%C YZL1vv $database = "users"; //表的名字设置统计的时间(多少秒内在线人数)
u T5`xU ]CJ? $timeoutseconds = 300;取当前时间。k@f2Uw,? KK:^ {
$timestamp = time();上面的完整代码:
J8OA4[,g8yy5i@$[ <?php4I#N3Rr]#\
$server = "localhost"; //your server
-_1V:R C,qz+{ $db_user = "root"; //your mysql database username bZ(d-J`|
$db_pass = "password"; //your mysql database password if any
ND3PE(Y+D:NX $database = "users"; //the db name
w$I&jD-FW k:{-BKH $timeoutseconds = 300;//timeoutseconds limit^ `8K3mc7s#O`
//get the current timeMFZ&]*ks
$timestamp = time();
7OCA4}[ //calculate the lowest timestamp allowed8jA$e(["w
$timeout = $timestamp-$timeoutseconds;
)_t.i)M l'g ?>连接mysqlO(UMH8N0{{f
mysql_connect('localhost', 'username', 'password');也允许使用变量形式。E o*F%?l^u
mysql_connect($server, $db_user, $db_pass);如果mysql数据库没有密码的话可以使用下面代码连接]K?f,dm4uUW
mysql_connect($server, $db_user);查询数据库的代码:}]4bRn x)oAp
mysql_db_query('database', 'query');我们只要有访客就要增加一条记录。
f}(J[8Z8w)` $insert = mysql_db_query($database, "INSERT INTO useronline VALUES$nPE t-]Bf M"d}Gm
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
7a:q(Kk x4ig 如果用户用错误信息的话,这样处理。
v1x2zkkI if(!($insert)) {/c nxOpeF,O
print "Useronline Insert Failed > ";
3qi2a']:jy }然后实现当超过设置的时间就删除该用户记录。
5ue#P XR.SO $delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");同样给出删除记录出错的处理。v4Ko_hc t6h8M
if(!($delete)) {c h:sF/L!R
print "Useronline Delete Failed > ";
:N4Yr Q)X q }下面我们解决数据库中不同IP的问题
)GI'@ I_&Vg $result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");使用
p2d0C \v:_y'L mysql_num_rows(query);来统计用户,代码如下。P8j)`[:C Se5qG|I*|@
$user = mysql_num_rows($result);最后关闭数据库。
Q8_])r_ S2Qn!` mysql_close();显示在线的人数。#E1r^;}C:KtY
if($user == 1) {\!F `6[+U4E$~.n
print("1 user online\n");
QT+M?1J f4F g)c.Q \ } else {
Dtp O2x}.xY print("$user users online\n");
Y"\gS~jnxN }最终把上面代码写成一个PHP文件如下。
A)AX$[ c!C^ <?php
ikL(S[r //Put your basic server info here
&FL5\'@9t4WT $server = "localhost"; //normally localhost
8u8m @(c1OG:p)LiC $db_user = "root"; //your MySQL database username(I/l gX"q t
$db_pass = "password"; //your MySQL database password
;n [+j(vM S $database = "users";qE9vXJN0U
$timeoutseconds = 300; //it will delete all people which haven't refreshed(so probbably are m$_PF%FM0JN
// offline or inactive) in $timieoutseconds time (so it actually checks the people that are active in the last.c+X6k:a@-y6cvFj
// $timeoutseconds seconds) hU[x0? }(Q&P1t
//this is where PHP gets the timej"LR&O,|]e
$timestamp = time();)a TMT]2kV pJ9a
//counts the timeout, all people which have been seen last online in earlier than this timestamp, will get removed
J}C`(iG $timeout = $timestamp-$timeoutseconds;rZ9g;w5N#XU)} h
//connect to database
7B"u*fT-p,^ mysql_connect($server, $db_user);
/x*xD'_5U~ //add the timestamp from the user to the online listn1y6T ?!]HR
$insert = mysql_db_query($database, "INSERT INTO useronline VALUESa;D+vSK
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");{d1} }9e;Zrye)`
if(!($insert)) {
Vc{,?,V?7a print "Useronline Insert Failed > ";
ve`6e"A{t}*S*~ }!k_(b8f1u,C
//delete the peoples which haven't been online/active in the last $timeoutseconds seconds. M?|pWeK-o`Y
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");&u%k/U%c l'z3N'HR
if(!($delete)) {
"k%[2CJ"u-k print "Useronline Delete Failed > ";,s"k+A1`2[0X G-A
}
)f7I?Evrps //select the amount of people online, all uniques, which are online on THIS page*F Fy;jb$X lB
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");
;v }$p ]q{`#R2wJ if(!($result)) {`g(E1p9lNN\
print "Useronline Select Error > ";
!`sr)KXL,i[sb }
"Y|#NzJk-j#b1[.q&xz //Count the number of rows = the number of people online}Mj2~Ydh#i[8A
$user = mysql_num_rows($result);
rU|P5['k^8` U/R+a //spit out the results
dHQ+nS~wh)Y mysql_close();
9p5I-RJ"j[ G if($user == 1) {B c1k?)H6s
print("1 user online\n");%w_p_M s$O~L
} else {!zk h5N:\ Z1rYP
print("$user users online\n");
4`%m$k6y d)vE2~\ }
2uglP.z ?>yz"j)o K7V
[url=http://www.now.cn/vhost/][img]http://b.todayisp.com/bbs/760x90h.gif[/img][/url]
~{\d(Hs t/Y 以上代码我是在时代互联提供的免费试用主机上测试的,感觉效果还不错,性能很稳定,不信的话你也可以去试试。当然,记得指出我这个程序中的不足先哦。
:Rk DW\+d)K:V9O 时代互联是行业内最早开始提供PHP 虚拟主机的公司之一,现在已经是的金牌域名注册商和中国互联网客户满意度十佳单位,产品功能已经非常完善 ,特别提一下几个特有用的功能:数据库自动备份、免费集成繁简通简装版、多域名绑定、多子网站支持、GCDN 网站加速器、镜像加速、高级访问统计系统、支持WAP、可选电信/网通/香港机房。
J9@~ C}O:w 我之前建站的时候选的是他们公司标准商务E型,因为这款机型还特别支持ASP.NET3.5(3.0/2.0/1.1)/ASP。
`f v5^ `?(TV?J 当然啦,这两款主机也是相当不错的。
8T u5d{3f-Qr 智强商务B型:Unix /Linux 操作系统+独立网页空间1000MB+送1000MB邮局空间+100MB MySQL数据库空间=1869元/年q+y$xo%gu
标准商务B型:Unix /Linux 操作系统+独立网页空间500MB+送800MB邮局空间+60MB MySQL数据库空间=1365元/年
B@%h,H1f9{ 提供一下这个公司的联系方式:请见:[url=http://www.now.cn/vhost/]http://www.now.cn/vhost/[/url] fQwK}V%u!@
空间专线: 0756-2623871  QQ:168075865,好象还有全球免费网络电话:全球免费咨询电话 [url=http://www.now.cn/callcenter/call.net?LineName=55]http://www.now.cn/callcenter/call.net?LineName=55[/url]0zu2o1K ?m+AA1I
自己加QQ去问吧。

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


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