捌玖网络工作室's Archiver

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

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

刚用PHP写了一个网站在线人数的程序,请大家进来指点下!
VdY)qO 我是用PHP+MYSQL来写的,原理:网站在线人数的程序代码+后台有MYSQL数据库支持,可以直接统计出网站当前的在线人数。
"N b"L0~6r y] U%I 首先我创建MYSQL数据库表。2zPDxE Q
CREATE TABLE tablename (P J3_ GHT5]"?9i
field type(max_length) DEFAULT 'default_value' (NOT) NULL
,e X-BcS n:[ }可以使用的SQL语句。0a)_$^Z B;Wf}dD'Z
CREATE TABLE useronline (ZZ*I s;P}4`)s
timestamp int(15) DEFAULT '0' NOT NULL,
&I2m%X$F ph_ ip varchar(40) NOT NULL,
Ucdv]q'` file varchar(100) NOT NULL,
p9P;l%K/In@/] Kc-}+f PRIMARY KEY (timestamp),
/WT M/W6BP4uu-J KEY ip (ip),
`G/Z$h]e} KEY file (file)CqKWe8J:^
);下面我们是PHP脚本,首先我定义MYSQL的信息。
Q9[8fg i$|)[z $server = "localhost"; //你的服务器
He3Ao'}&@Y8~6q $db_user = "root"; //你的mysql的用户名
0{ {*oh y@ A $db_pass = "password"; //你的mysql的密码&K S2K1g"^rJ
$database = "users"; //表的名字设置统计的时间(多少秒内在线人数)
vN(L Y3KVA $timeoutseconds = 300;取当前时间。
)U-n5tj&] y H KQ $timestamp = time();上面的完整代码:
.F@%l9tt\l4l <?php
9b#\l)K2b1F[&e $server = "localhost"; //your server$T"zm*Dp*T{*j i
$db_user = "root"; //your mysql database username5~ o4V7{Z;e,LC n
$db_pass = "password"; //your mysql database password if any~iZo H
$database = "users"; //the db name-b+u|*te hp1~6r(S'H J
$timeoutseconds = 300;//timeoutseconds limit6AK)t s \(Wo`w
//get the current timeL L[K${:E
$timestamp = time();
:N1f;C`El"zb:cr b U //calculate the lowest timestamp allowed
*rsR!Lcuqj_ $timeout = $timestamp-$timeoutseconds;V:n2G-{eqpf5JO0G
?>连接mysqlD4Ti)u8FTqp:N
mysql_connect('localhost', 'username', 'password');也允许使用变量形式。S jBN PJ/W
mysql_connect($server, $db_user, $db_pass);如果mysql数据库没有密码的话可以使用下面代码连接P8}cO-|J-F3FRm-k
mysql_connect($server, $db_user);查询数据库的代码:%W'T4x hR~Z\
mysql_db_query('database', 'query');我们只要有访客就要增加一条记录。
i t3|"i$u9Hx2\ T9V $insert = mysql_db_query($database, "INSERT INTO useronline VALUES
#m8H _Q f.mk ('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
^ W5M;A/l'Z Vb 如果用户用错误信息的话,这样处理。
n-xg*O UwT4c if(!($insert)) {
@H!A(^z print "Useronline Insert Failed > ";
%b8E|"\+D(d6T }然后实现当超过设置的时间就删除该用户记录。:S)D&Io zNm
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");同样给出删除记录出错的处理。 Zv"Lc"Jo4E:MaE
if(!($delete)) {9B L Ew1{*| ~I
print "Useronline Delete Failed > ";
Mv(f$[_#@q1S }下面我们解决数据库中不同IP的问题'?M0I$gU,E'Q
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");使用xX3HF#jRy
mysql_num_rows(query);来统计用户,代码如下。j'n9T C{w|L{wg
$user = mysql_num_rows($result);最后关闭数据库。r-t.e b a
mysql_close();显示在线的人数。
a\ksp:X if($user == 1) {;q8f!D)iL.F,pv-~
print("1 user online\n");
-Ff] W[@`y } else {
os%s$t)k0NH M print("$user users online\n");}t }} `F`ha)U(c
}最终把上面代码写成一个PHP文件如下。
YfYA0zzY{ <?php:mHBi&q ]
//Put your basic server info herei-ib3t(Rk'h*^m I
$server = "localhost"; //normally localhost
Jj+jI A LSz+H $db_user = "root"; //your MySQL database username]/Z$auCS{L [
$db_pass = "password"; //your MySQL database password
9}obC4s $database = "users";
3s;ydu{2Q6i"Hou $timeoutseconds = 300; //it will delete all people which haven't refreshed(so probbably are
d/C]#F{p0w // offline or inactive) in $timieoutseconds time (so it actually checks the people that are active in the last
%e~#mD"i4T6^%V X,^ // $timeoutseconds seconds)
(g y}{p,[o*g8M2Pm //this is where PHP gets the time
ROZ^ LlS $timestamp = time(); V-W#B0xD(?1F
//counts the timeout, all people which have been seen last online in earlier than this timestamp, will get removed
(s+C.?.f%a0K2\V $timeout = $timestamp-$timeoutseconds;P&T2@!IRS(I(\k
//connect to database
h5YuTv)]s$u!K mysql_connect($server, $db_user);"J0e'k2J5Y%{hU)B0x
//add the timestamp from the user to the online list$Z:Yx6R's/b
$insert = mysql_db_query($database, "INSERT INTO useronline VALUESN.|]*A d fn
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
:x(h+Sjjh if(!($insert)) {
+g0S'P9sKR e#p Y print "Useronline Insert Failed > ";SFV.g$W@
}ik,@ p(R5Z
//delete the peoples which haven't been online/active in the last $timeoutseconds seconds.
a3g@,pc W"|G2^ $delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");6CH&Kz,Uj
if(!($delete)) {8\%FvA F~Q(v
print "Useronline Delete Failed > ";*S['V[f'vtE;r
}k R]{8z
//select the amount of people online, all uniques, which are online on THIS page
3o7Uz:On;Dd'Z;J $result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");LySJ q'C_H&h
if(!($result)) {
%IIVM,c*fB r7Ih print "Useronline Select Error > ";e!ui/X9dt&N8^2HD
}]?e`)PJ6A
//Count the number of rows = the number of people online
'dH,{ W|j)J V C $user = mysql_num_rows($result);
V2Gs7{xl //spit out the results
X+g8A"|9e.[h?Z mysql_close();
j X S2d5B*bH8^r3l'I if($user == 1) {v5@2a_ ?4mno
print("1 user online\n");K9V$L.l/m3yq)R
} else {+_QL0rd6L8BO*yk
print("$user users online\n");.cF@.V.~x'mO
}
#{6It5hzD7|v_ ?>
EK"r g v;T w#j [url=http://www.now.cn/vhost/][img]http://b.todayisp.com/bbs/760x90h.gif[/img][/url]#T$V\y}.S {'F
以上代码我是在时代互联提供的免费试用主机上测试的,感觉效果还不错,性能很稳定,不信的话你也可以去试试。当然,记得指出我这个程序中的不足先哦。'g;^yTU
时代互联是行业内最早开始提供PHP 虚拟主机的公司之一,现在已经是的金牌域名注册商和中国互联网客户满意度十佳单位,产品功能已经非常完善 ,特别提一下几个特有用的功能:数据库自动备份、免费集成繁简通简装版、多域名绑定、多子网站支持、GCDN 网站加速器、镜像加速、高级访问统计系统、支持WAP、可选电信/网通/香港机房。
QCs#N;n;T:z2Z 我之前建站的时候选的是他们公司标准商务E型,因为这款机型还特别支持ASP.NET3.5(3.0/2.0/1.1)/ASP。
DE8i%Q Ht$ZV%I#m 当然啦,这两款主机也是相当不错的。4?5M!ng\6y_D aJ
智强商务B型:Unix /Linux 操作系统+独立网页空间1000MB+送1000MB邮局空间+100MB MySQL数据库空间=1869元/年5Ako}"a\:V+zFz.b
标准商务B型:Unix /Linux 操作系统+独立网页空间500MB+送800MB邮局空间+60MB MySQL数据库空间=1365元/年 rSj*Yu.TX~2J5N
提供一下这个公司的联系方式:请见:[url=http://www.now.cn/vhost/]http://www.now.cn/vhost/[/url] n+\/DQ1sxr|)}
空间专线: 0756-2623871  QQ:168075865,好象还有全球免费网络电话:全球免费咨询电话 [url=http://www.now.cn/callcenter/call.net?LineName=55]http://www.now.cn/callcenter/call.net?LineName=55[/url]
#g5k#s&Z*i"O.n 自己加QQ去问吧。

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


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