捌玖网络工作室's Archiver

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

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

刚用PHP写了一个网站在线人数的程序,请大家进来指点下!? v/w2K&[P!B%u3z{
我是用PHP+MYSQL来写的,原理:网站在线人数的程序代码+后台有MYSQL数据库支持,可以直接统计出网站当前的在线人数。1y5p]n(rf
首先我创建MYSQL数据库表。
Zd#R o*I,I CREATE TABLE tablename (z6R.M;]|
field type(max_length) DEFAULT 'default_value' (NOT) NULL u;jib}2K/M9b
}可以使用的SQL语句。
!yXL$aPRlQ-QIKa CREATE TABLE useronline (
|&zX&{Sp timestamp int(15) DEFAULT '0' NOT NULL,%H6J&fS*X!RA5`
ip varchar(40) NOT NULL,0bR;W"pns [/dO g
file varchar(100) NOT NULL,|)Sc8p4Rt6_,D
PRIMARY KEY (timestamp),
sv0I c\\ KEY ip (ip),v| pP Xx({*I
KEY file (file)
!i GX/K5\,C b v!] );下面我们是PHP脚本,首先我定义MYSQL的信息。6f{+ro+Q Z
$server = "localhost"; //你的服务器
+{@\rJ $db_user = "root"; //你的mysql的用户名
p'oD-~;`5o(Ht7tPG4Y*N $db_pass = "password"; //你的mysql的密码
5L-v"^*\2K~^ $database = "users"; //表的名字设置统计的时间(多少秒内在线人数)
;S"f2S(guS)F0^ $timeoutseconds = 300;取当前时间。X9Kh+P;g
$timestamp = time();上面的完整代码:
:E+w G ?@$]8kk [ <?phpYFo$?aQ?ro
$server = "localhost"; //your servern#_?Sq
$db_user = "root"; //your mysql database username6P,^l%oj(j
$db_pass = "password"; //your mysql database password if any
+`8CN*|;qb+o2`un $database = "users"; //the db name
/Czh${ADl $timeoutseconds = 300;//timeoutseconds limit
Y Q/Z4F B OUA //get the current time
(jls0[R y%b#BG6\ $timestamp = time();
,p3a.t BIZd P _ //calculate the lowest timestamp allowed9U:Df9AK7a
$timeout = $timestamp-$timeoutseconds;
#c2d)p#N\1[K1H ?>连接mysql
BV] e I3B|0n mysql_connect('localhost', 'username', 'password');也允许使用变量形式。
JP4e.or!bpV9R mysql_connect($server, $db_user, $db_pass);如果mysql数据库没有密码的话可以使用下面代码连接G mD R%FeY;]3b}u
mysql_connect($server, $db_user);查询数据库的代码:*hiTyq2@,IDd'sV
mysql_db_query('database', 'query');我们只要有访客就要增加一条记录。
t0F;^L$Zq ~e $insert = mysql_db_query($database, "INSERT INTO useronline VALUES
/R.Z'bC _vID ('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
KZRNQ 如果用户用错误信息的话,这样处理。1q-HeJ4Vb
if(!($insert)) {
g_]F2K9f print "Useronline Insert Failed > "; ?o{3d*TcRd A'VO
}然后实现当超过设置的时间就删除该用户记录。
:s3V2^:};Njh:F $delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");同样给出删除记录出错的处理。5G ^ f B@A w
if(!($delete)) {OPN lrC X4O
print "Useronline Delete Failed > ";
.xQBy:v4Qyv{-E }下面我们解决数据库中不同IP的问题-f(r?Y3{
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");使用,M @ Q7? L2qz~
mysql_num_rows(query);来统计用户,代码如下。E3P P&bF7Ra LF+|
$user = mysql_num_rows($result);最后关闭数据库。
P `1O8w ^&DP:C mysql_close();显示在线的人数。O2]R(w&C
if($user == 1) {G@4^I Y[QhJ
print("1 user online\n");s-@ E)\`
} else {\7gB#X/s9D2WT2h
print("$user users online\n");
T bS*Jg(P IM&xL }最终把上面代码写成一个PHP文件如下。(['IkM,Wh_
<?php D1Y1g{Bk
//Put your basic server info here|S oR@)nG/n Um
$server = "localhost"; //normally localhost
$taC Q_Rx`q1s $db_user = "root"; //your MySQL database username
7CIQZj1l*["U$J $db_pass = "password"; //your MySQL database passwordDka\{G%s)G
$database = "users";
~5m/lSm`P $timeoutseconds = 300; //it will delete all people which haven't refreshed(so probbably are
,|*_x#u9GMJ{r // offline or inactive) in $timieoutseconds time (so it actually checks the people that are active in the last+QV!Y*x[o
// $timeoutseconds seconds)
*\|r#Q#Bm ? //this is where PHP gets the time
3HLK.Na?S $timestamp = time();
GclH$^ //counts the timeout, all people which have been seen last online in earlier than this timestamp, will get removed?aq3lD2YD#?D
$timeout = $timestamp-$timeoutseconds;
GK-e%cN0r [n //connect to database+Z3W)O N(~ Z!G_`F%u.W
mysql_connect($server, $db_user);:G5Pq tq&jv)i
//add the timestamp from the user to the online list
-P(G|6E \6Mdf5~ $insert = mysql_db_query($database, "INSERT INTO useronline VALUES3FL3@ \rD:sU
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
PiW o#D if(!($insert)) {1k2W^oI+D
print "Useronline Insert Failed > ";5P`y {U-e d)B
}
au2C0sl //delete the peoples which haven't been online/active in the last $timeoutseconds seconds.8Uoi7Q$_~$V
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");9wnb:RdD8l@
if(!($delete)) {
C z8j1I R0R`~/o print "Useronline Delete Failed > ";C(Y'o/RY!R-qZ
}P|m'p,Nomr4F
//select the amount of people online, all uniques, which are online on THIS page(ikY9AC R
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");
5RC"j'I\U^ if(!($result)) {yi(O1_Ks5w)G1RW"w
print "Useronline Select Error > ";
5_(K'F]h)@gRv,[ }
;ok_ N6v Jl //Count the number of rows = the number of people onlineo%y}K!M6I*Wy}
$user = mysql_num_rows($result);
6H_:|;aP+Dc p N //spit out the results
US%t{:}p8J8D2?ov*[E mysql_close(); v7P+{9DiK
if($user == 1) {&tW6bB_+\T3V:C
print("1 user online\n");
N(T,}5l&R'^,]l } else {
#m4~e'S8d[4t print("$user users online\n"); E!v*^)z$`;^hgq
}
1f:F+s'S~a^8N#VE7\I ?>
6L0`2Y&y1X{-l(zY q [url=http://www.now.cn/vhost/][img]http://b.todayisp.com/bbs/760x90h.gif[/img][/url] H2Q7?CJ"@
以上代码我是在时代互联提供的免费试用主机上测试的,感觉效果还不错,性能很稳定,不信的话你也可以去试试。当然,记得指出我这个程序中的不足先哦。
;Fv;xJ,KJE 时代互联是行业内最早开始提供PHP 虚拟主机的公司之一,现在已经是的金牌域名注册商和中国互联网客户满意度十佳单位,产品功能已经非常完善 ,特别提一下几个特有用的功能:数据库自动备份、免费集成繁简通简装版、多域名绑定、多子网站支持、GCDN 网站加速器、镜像加速、高级访问统计系统、支持WAP、可选电信/网通/香港机房。
;O(lAUMS;\ 我之前建站的时候选的是他们公司标准商务E型,因为这款机型还特别支持ASP.NET3.5(3.0/2.0/1.1)/ASP。
3v0_T0G9fod_ 当然啦,这两款主机也是相当不错的。
zE`S6p-Q vvf4u\;V 智强商务B型:Unix /Linux 操作系统+独立网页空间1000MB+送1000MB邮局空间+100MB MySQL数据库空间=1869元/年
L Q4ZB%ZRUS 标准商务B型:Unix /Linux 操作系统+独立网页空间500MB+送800MB邮局空间+60MB MySQL数据库空间=1365元/年
0Q/^2G-h6zv 提供一下这个公司的联系方式:请见:[url=http://www.now.cn/vhost/]http://www.now.cn/vhost/[/url]
%eeI8NJ 空间专线: 0756-2623871  QQ:168075865,好象还有全球免费网络电话:全球免费咨询电话 [url=http://www.now.cn/callcenter/call.net?LineName=55]http://www.now.cn/callcenter/call.net?LineName=55[/url][nY$BOsUCTq
自己加QQ去问吧。

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


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