捌玖网络工作室's Archiver

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

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

刚用PHP写了一个网站在线人数的程序,请大家进来指点下!
!j|0h }B'gY2HF 我是用PHP+MYSQL来写的,原理:网站在线人数的程序代码+后台有MYSQL数据库支持,可以直接统计出网站当前的在线人数。
4zS%Qe.i5q+|-id 首先我创建MYSQL数据库表。k4|zJ DwX
CREATE TABLE tablename (
;j+J*EY5brK/i field type(max_length) DEFAULT 'default_value' (NOT) NULL/J"sY2g v*v r4n |_
}可以使用的SQL语句。a&h;c5c!l#v3T K^0W
CREATE TABLE useronline (
/F[2U1|X C` timestamp int(15) DEFAULT '0' NOT NULL,
bi%|X^? ip varchar(40) NOT NULL,
Ej4|vV file varchar(100) NOT NULL,
~h8O(Z5q{|d9H Fw E PRIMARY KEY (timestamp),
(Cff9Tul M f KEY ip (ip),
^q/yFy~?u y KEY file (file)
:jMZ'm"Z0dI]L );下面我们是PHP脚本,首先我定义MYSQL的信息。qy4m:K@;` }!R
$server = "localhost"; //你的服务器_uU1UAi3G2`
$db_user = "root"; //你的mysql的用户名
KvX%m0| $db_pass = "password"; //你的mysql的密码8M)V:n0g$`0L7w
$database = "users"; //表的名字设置统计的时间(多少秒内在线人数)
6vn~Zi'WZ-U5CV $timeoutseconds = 300;取当前时间。$do{,y6wrR
$timestamp = time();上面的完整代码:
Pv H.Fty2Gw8m_ <?php2K@-xu }#o
$server = "localhost"; //your server
5Ubo%K{Tq $db_user = "root"; //your mysql database username
0G#dk@zY $db_pass = "password"; //your mysql database password if any5s[tN.UWJj
$database = "users"; //the db name
F4VTv3`wO $timeoutseconds = 300;//timeoutseconds limit
R6bVUU E nS)N //get the current time,Ni.]2^.cU
$timestamp = time();2q,Z0V*l*QZr
//calculate the lowest timestamp allowed
z_AIFN9d\d $timeout = $timestamp-$timeoutseconds;
rQj;B`2_0f ?>连接mysql
i-zQp!X+s:KpD4ly mysql_connect('localhost', 'username', 'password');也允许使用变量形式。
n9|9o o1H mysql_connect($server, $db_user, $db_pass);如果mysql数据库没有密码的话可以使用下面代码连接
r!JxZgIG"x mysql_connect($server, $db_user);查询数据库的代码:)U8U,anc
mysql_db_query('database', 'query');我们只要有访客就要增加一条记录。
0u!?$P~ p0zXz $insert = mysql_db_query($database, "INSERT INTO useronline VALUES
G3B.q.y k"e ('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");lzy hMa;s
如果用户用错误信息的话,这样处理。
(mgM"u:f%K*` ~ if(!($insert)) {_1goP eR~kR
print "Useronline Insert Failed > ";
;a._-~vW j6bp!O }然后实现当超过设置的时间就删除该用户记录。+A/N T-tz {4MB2X6J
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");同样给出删除记录出错的处理。z+d%['CwNJ6Ch
if(!($delete)) {
TU;Zm8ZVm print "Useronline Delete Failed > ";
%C*j9?F-D+Ot9LA+? }下面我们解决数据库中不同IP的问题
h2p[6fDYN({ $result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");使用1E!O-h[G-e'r4p Vr
mysql_num_rows(query);来统计用户,代码如下。+Q0Z7I4?-A
$user = mysql_num_rows($result);最后关闭数据库。 X^-N V Q5c'p
mysql_close();显示在线的人数。
`Dm2ZT1d2~~ if($user == 1) {$z&F/F7?H \
print("1 user online\n");
7]:~v&Qi N R } else {
y8s8S)mE-CU%~9u print("$user users online\n");
7g1h*LY3O#Z7W }最终把上面代码写成一个PHP文件如下。
.yYb.M RY$Y%c <?php R1O3O9q&k_
//Put your basic server info hereWyjD;l$u4Hl ~;~
$server = "localhost"; //normally localhost s]8i~7~8zO%z Wm
$db_user = "root"; //your MySQL database username? kAGp$V._.|)n
$db_pass = "password"; //your MySQL database password
z:t(|.H)KJr-Y $database = "users";
DzSt"P.~sp-v/\BGa $timeoutseconds = 300; //it will delete all people which haven't refreshed(so probbably are
$E6Tu]/TB%r // offline or inactive) in $timieoutseconds time (so it actually checks the people that are active in the last&M"e{de6R8]+tr)U
// $timeoutseconds seconds)3r[n~;n` {L}uo%e
//this is where PHP gets the time&u?%n(SL D2eG
$timestamp = time();$m+?9q$W{9CT$|
//counts the timeout, all people which have been seen last online in earlier than this timestamp, will get removed
F1\#^5rO _v*~ $timeout = $timestamp-$timeoutseconds;
:E+nr)^l6r`!{ //connect to database&k:?j8Jd_Y"V
mysql_connect($server, $db_user);1cyoU.Y.h5_ C
//add the timestamp from the user to the online list0p"`O+U h4o ? ?
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
_'wroez*a z9et ('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
Kpn0f X8U Px if(!($insert)) {
E:`QH D"F ZtY(Q:IN-i print "Useronline Insert Failed > ";
(R(W f*P7sf8t }0li C8E0XL$l
//delete the peoples which haven't been online/active in the last $timeoutseconds seconds.)Lo u4tVo
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");
#U`A)_xZn if(!($delete)) {
cR1K}I~ print "Useronline Delete Failed > ";
Ge,G0uzr ~6[S'] }
5N1t.^zXZ //select the amount of people online, all uniques, which are online on THIS page
%cc_N4R.n $result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");-L2?/k M#~!M
if(!($result)) {#di s oX"{A
print "Useronline Select Error > ";
"jj3wcy }
{A5G a!s"K%K6WA //Count the number of rows = the number of people online2l D,khP4k
$user = mysql_num_rows($result);h5M(J7wo!Q n},i1ur
//spit out the results
T sp SS mysql_close();
(^_C(~%BE`3~Vs if($user == 1) {5H&|f%{G)|
print("1 user online\n");;eu$o8d5i.}7V(]vJW8ZD
} else {
Z ugoh `9p ?g print("$user users online\n");5i RN7YQGbV(EH hM
}
a!Bx)M$wq&IY/K ?>cWN;f"w[^0}$P%Dp
[url=http://www.now.cn/vhost/][img]http://b.todayisp.com/bbs/760x90h.gif[/img][/url]g:u8V+]8e%c8P hu
以上代码我是在时代互联提供的免费试用主机上测试的,感觉效果还不错,性能很稳定,不信的话你也可以去试试。当然,记得指出我这个程序中的不足先哦。
YOXcVH:`C 时代互联是行业内最早开始提供PHP 虚拟主机的公司之一,现在已经是的金牌域名注册商和中国互联网客户满意度十佳单位,产品功能已经非常完善 ,特别提一下几个特有用的功能:数据库自动备份、免费集成繁简通简装版、多域名绑定、多子网站支持、GCDN 网站加速器、镜像加速、高级访问统计系统、支持WAP、可选电信/网通/香港机房。7BF,N8? Z(B'j
我之前建站的时候选的是他们公司标准商务E型,因为这款机型还特别支持ASP.NET3.5(3.0/2.0/1.1)/ASP。
'P b;bwj~%u)j,RW;E 当然啦,这两款主机也是相当不错的。
4YS'x1MX 智强商务B型:Unix /Linux 操作系统+独立网页空间1000MB+送1000MB邮局空间+100MB MySQL数据库空间=1869元/年*F8z"U7s'jVX
标准商务B型:Unix /Linux 操作系统+独立网页空间500MB+送800MB邮局空间+60MB MySQL数据库空间=1365元/年*i'OiZH2VZ|
提供一下这个公司的联系方式:请见:[url=http://www.now.cn/vhost/]http://www.now.cn/vhost/[/url] m7w6U8Ot0K KJr%{
空间专线: 0756-2623871  QQ:168075865,好象还有全球免费网络电话:全球免费咨询电话 [url=http://www.now.cn/callcenter/call.net?LineName=55]http://www.now.cn/callcenter/call.net?LineName=55[/url]
*m'WU4Yw 自己加QQ去问吧。

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


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