捌玖网络工作室's Archiver

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

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

刚用PHP写了一个网站在线人数的程序,请大家进来指点下!u_}:B~"Z
我是用PHP+MYSQL来写的,原理:网站在线人数的程序代码+后台有MYSQL数据库支持,可以直接统计出网站当前的在线人数。
3`m(S!w5?%b My{ 首先我创建MYSQL数据库表。g*b,H.a9} b
CREATE TABLE tablename (
I*AMcy.axw field type(max_length) DEFAULT 'default_value' (NOT) NULL0L n7bS8W)]eg2l B}b
}可以使用的SQL语句。z`Au)b$AH3Iq
CREATE TABLE useronline (
QW&DJ q'w2X timestamp int(15) DEFAULT '0' NOT NULL,
yvb"Nk'S0B so[ ip varchar(40) NOT NULL,
e,gJT~|!Lr file varchar(100) NOT NULL,5CX)b ifDJ/TU
PRIMARY KEY (timestamp),P},NgsjKHS5{
KEY ip (ip),
'~GoY;y KEY file (file)
8H-zZ2H b-G(A );下面我们是PHP脚本,首先我定义MYSQL的信息。4Y%z0]a^ ]
$server = "localhost"; //你的服务器y_+[[5H'e#S&W
$db_user = "root"; //你的mysql的用户名
4`[r R}[+d $db_pass = "password"; //你的mysql的密码8CR G9va-~
$database = "users"; //表的名字设置统计的时间(多少秒内在线人数)l0C7@v*w/J2}]
$timeoutseconds = 300;取当前时间。g[ Epu0`s DH%z
$timestamp = time();上面的完整代码:L P-o5C7Kx
<?php
!F b|+a6h!r(m)XD?*e $server = "localhost"; //your server
$o+`7sA%J $db_user = "root"; //your mysql database username
-N6t)n:DIG/HgJ| $db_pass = "password"; //your mysql database password if any
A&Czw\(T(Uy ^'e $database = "users"; //the db nameL1^6qL#}},[9RD
$timeoutseconds = 300;//timeoutseconds limit
L:D&`z%no-|_U$Ol //get the current time4f8Z$viZ9m
$timestamp = time();
:~| ~0D8K3om3L.U~ //calculate the lowest timestamp allowed
^UNE9r4sKR $timeout = $timestamp-$timeoutseconds;F$l@ O/TAb x!|.])TZ
?>连接mysql
9j&s2z/Cp j g mysql_connect('localhost', 'username', 'password');也允许使用变量形式。
!fNB(zH3s#^ f3F z$Y mysql_connect($server, $db_user, $db_pass);如果mysql数据库没有密码的话可以使用下面代码连接
,l Kdf(E*UC4l9n mysql_connect($server, $db_user);查询数据库的代码:1eO`Z;k(c,E w
mysql_db_query('database', 'query');我们只要有访客就要增加一条记录。9Q"Dtw0d;R;?
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
|8K!N~e&E ('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");V*dB(M7?~F1d5ckLg
如果用户用错误信息的话,这样处理。
`d%c:~,Y7l if(!($insert)) {
:jUBs4v.\2_(B(Qhg print "Useronline Insert Failed > ";
d7aY)J(O+D,eK-g }然后实现当超过设置的时间就删除该用户记录。` cl\Z Vt(q a/[J
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");同样给出删除记录出错的处理。hz,sG-U1Bq
if(!($delete)) {
Rm^ a%[[.s.S print "Useronline Delete Failed > ";
e8y7y4b6QX R(? u'n }下面我们解决数据库中不同IP的问题o7m:@*FIKN_5z
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");使用1y7H am%?~"e~
mysql_num_rows(query);来统计用户,代码如下。K eY3HY{.z
$user = mysql_num_rows($result);最后关闭数据库。
R K^3Pf"fy] mysql_close();显示在线的人数。
1rn G_d if($user == 1) {lElDY D$b
print("1 user online\n");L"tb,i7k3}*y'y U
} else {
3yG5?lv0Ot4k%iIJ print("$user users online\n");
1l}KFKB }最终把上面代码写成一个PHP文件如下。*y,ME1\n&f
<?php
.nnsFa]? //Put your basic server info here
^pjoGF3X{(w $server = "localhost"; //normally localhostc^ X]*nle
$db_user = "root"; //your MySQL database usernameo U(s q$~$}!XH
$db_pass = "password"; //your MySQL database password3yMC/Okz
$database = "users";
$W.PQ3i;{b+D4I $timeoutseconds = 300; //it will delete all people which haven't refreshed(so probbably are+B:{Rl5|,P#A ez|b
// offline or inactive) in $timieoutseconds time (so it actually checks the people that are active in the last2jWVo5Ws5p0hW ]
// $timeoutseconds seconds)
3N `*[sNk(R //this is where PHP gets the time
!N#lINB)P%c6o $timestamp = time();2F~^ f7V
//counts the timeout, all people which have been seen last online in earlier than this timestamp, will get removed%bu;f RGc]N I
$timeout = $timestamp-$timeoutseconds;M5ixc.a9{s E.O7h Qq
//connect to database$FD'd&tV!b:? Y1Kl.@`
mysql_connect($server, $db_user);
FRo*I e!o4S //add the timestamp from the user to the online list
s'L9[S1o+{q $insert = mysql_db_query($database, "INSERT INTO useronline VALUES
'Q.Q\oayI ('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");Tz|O)z(E
if(!($insert)) {eb| x Rp2i9e+Wo(t'_C
print "Useronline Insert Failed > ";
Ej1Mi+t v ]E M }
&u)Kk&ED1E //delete the peoples which haven't been online/active in the last $timeoutseconds seconds.'b a|2sTF(ul
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");}D.T4d4]f-\
if(!($delete)) {
9R-j9v2_ i print "Useronline Delete Failed > "; z/R9i/f2?+bKr
}1F"~5U o2s
//select the amount of people online, all uniques, which are online on THIS page,s*r0Pt+]!N
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");7g_Q3Z;@'J
if(!($result)) {
5c@^UG0jm&o3H'D print "Useronline Select Error > ";5n x6Y7\:F"T
}y!_D`eq0H
//Count the number of rows = the number of people online/hn*nxrf
$user = mysql_num_rows($result);
!Z9H2T6|0~-IIz //spit out the results
f1l,| iB+v mysql_close();
v}4bDAug0i/\.q:J if($user == 1) {0u3}_ p!g*y?(^P3ec
print("1 user online\n");+m0p(m ~O^%^#q
} else {Z%~ Z|N]9@
print("$user users online\n");b;u&r(gp&c
}
W-R@5U)\&k7{ ?>:F3vz {s] g;U
[url=http://www.now.cn/vhost/][img]http://b.todayisp.com/bbs/760x90h.gif[/img][/url]
l)V]mAV 以上代码我是在时代互联提供的免费试用主机上测试的,感觉效果还不错,性能很稳定,不信的话你也可以去试试。当然,记得指出我这个程序中的不足先哦。
$Nz#|z i%FXlR 时代互联是行业内最早开始提供PHP 虚拟主机的公司之一,现在已经是的金牌域名注册商和中国互联网客户满意度十佳单位,产品功能已经非常完善 ,特别提一下几个特有用的功能:数据库自动备份、免费集成繁简通简装版、多域名绑定、多子网站支持、GCDN 网站加速器、镜像加速、高级访问统计系统、支持WAP、可选电信/网通/香港机房。
p8Z[?.@M SN 我之前建站的时候选的是他们公司标准商务E型,因为这款机型还特别支持ASP.NET3.5(3.0/2.0/1.1)/ASP。!tp;BSE`#K
当然啦,这两款主机也是相当不错的。
:A Bel WPl 智强商务B型:Unix /Linux 操作系统+独立网页空间1000MB+送1000MB邮局空间+100MB MySQL数据库空间=1869元/年4dq4Wf(q3b
标准商务B型:Unix /Linux 操作系统+独立网页空间500MB+送800MB邮局空间+60MB MySQL数据库空间=1365元/年
A/u6CIi c&~R 提供一下这个公司的联系方式:请见:[url=http://www.now.cn/vhost/]http://www.now.cn/vhost/[/url] yf/|/L1]3f8l)o:W
空间专线: 0756-2623871  QQ:168075865,好象还有全球免费网络电话:全球免费咨询电话 [url=http://www.now.cn/callcenter/call.net?LineName=55]http://www.now.cn/callcenter/call.net?LineName=55[/url])d G#ops'W1](L
自己加QQ去问吧。

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


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