捌玖网络工作室's Archiver

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

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

刚用PHP写了一个网站在线人数的程序,请大家进来指点下! Vi+|']V;GJ
我是用PHP+MYSQL来写的,原理:网站在线人数的程序代码+后台有MYSQL数据库支持,可以直接统计出网站当前的在线人数。
?Zt'y)Lo#xn3q 首先我创建MYSQL数据库表。
C RQJ-F CREATE TABLE tablename (
+ELr Q$V0B field type(max_length) DEFAULT 'default_value' (NOT) NULLd F8Q|Z h
}可以使用的SQL语句。j0qNV/l&Z+lQO
CREATE TABLE useronline (
_xx/O A`7X'|D timestamp int(15) DEFAULT '0' NOT NULL,
%vr?#d{2l ip varchar(40) NOT NULL,"m"i GM(n]@5N
file varchar(100) NOT NULL,s]k\~7WjA O
PRIMARY KEY (timestamp),K y#V8f&LE8pS
KEY ip (ip),
:@} |$]_;v KEY file (file)
/of9mdS1D6_ );下面我们是PHP脚本,首先我定义MYSQL的信息。
5Div7f;y'rK2[ $server = "localhost"; //你的服务器n5jXQ cN,u*y
$db_user = "root"; //你的mysql的用户名 yjZ0m u
$db_pass = "password"; //你的mysql的密码 u&q'n TJ
$database = "users"; //表的名字设置统计的时间(多少秒内在线人数)6P&}+l#L#qkp\1p \;P
$timeoutseconds = 300;取当前时间。
4U@"kDi | $timestamp = time();上面的完整代码:*@0GDr6C+ZG.VMh
<?phpQ(aX?m)i:\%d
$server = "localhost"; //your serverd#U d ^ [vR
$db_user = "root"; //your mysql database username
B5h#qw } }oK%u $db_pass = "password"; //your mysql database password if anyo;Hm8L K'V]$[-D4z
$database = "users"; //the db name
` ~R0~/Il'{ $timeoutseconds = 300;//timeoutseconds limit
S"|8j-]2p"V7u //get the current time
z'j*Bu5H m $timestamp = time();
@QK HN+ahtf //calculate the lowest timestamp allowed eTWTj
$timeout = $timestamp-$timeoutseconds;%dhQ7D$V!G^1uH
?>连接mysqluy0?9A3^+ah xV
mysql_connect('localhost', 'username', 'password');也允许使用变量形式。-G`eU:Px w,k P TJ
mysql_connect($server, $db_user, $db_pass);如果mysql数据库没有密码的话可以使用下面代码连接
C)W4r;pe/Py? mysql_connect($server, $db_user);查询数据库的代码:8W%c'Z-N,hF(A
mysql_db_query('database', 'query');我们只要有访客就要增加一条记录。
(f]/Q4aM@O $insert = mysql_db_query($database, "INSERT INTO useronline VALUES
k1^^$nS_@ ('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
Dtn0k4iIm 如果用户用错误信息的话,这样处理。
%?ds5Ym0l P if(!($insert)) {#T)rc[1|s})T2[Q
print "Useronline Insert Failed > ";
#I.z4gv*V#~k }然后实现当超过设置的时间就删除该用户记录。o!G1| @-L
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");同样给出删除记录出错的处理。
Rw"eg(W8z'S if(!($delete)) {6EA0cs'Q!p)wC9J }7g
print "Useronline Delete Failed > ";
p1U-XkPE6K }下面我们解决数据库中不同IP的问题L.NI^"D"h
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");使用:n,u*Tz4haL(i]z
mysql_num_rows(query);来统计用户,代码如下。x]"W0Zl6r
$user = mysql_num_rows($result);最后关闭数据库。
}O}Z4x8p]T_ mysql_close();显示在线的人数。
9lS7C[I+Og+] if($user == 1) {
iQ3v$~3p print("1 user online\n");
/F g.O)Kq w[$F!Lz6G y } else {
G7vtIs }t print("$user users online\n");
L[;J^]KT@8k }最终把上面代码写成一个PHP文件如下。 Y?4bo O!@E&H~
<?php,K2{RL&J
//Put your basic server info here
S4K0]|-by%Rqw(B $server = "localhost"; //normally localhostl"Ue x|C's2O
$db_user = "root"; //your MySQL database username;_+C0n0d/gm[7Q
$db_pass = "password"; //your MySQL database password a k"kZ.@5s;s
$database = "users";?ODxwOkh
$timeoutseconds = 300; //it will delete all people which haven't refreshed(so probbably are
-\+[)gRXT.~Q9b // offline or inactive) in $timieoutseconds time (so it actually checks the people that are active in the last
4~W*W aS YUb!i // $timeoutseconds seconds)
w4B4hB2QVEy //this is where PHP gets the timei:Sv*Bm U
$timestamp = time();cwr5t'|]8D
//counts the timeout, all people which have been seen last online in earlier than this timestamp, will get removed|;ytE'b,}d4Z n
$timeout = $timestamp-$timeoutseconds;tl~5pGN
//connect to database
;Y"R&x2x7Pi mysql_connect($server, $db_user);
x"UZ:\1h:`5z{rO //add the timestamp from the user to the online list.bd9W%}l4Ci
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES8e5PncQZ6G*v
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
5pl C(OUH2l`x if(!($insert)) {
e2jr%wbuUd print "Useronline Insert Failed > ";
7zav5`T9@y(]6?J(h }
{!NF4x%GAP-m //delete the peoples which haven't been online/active in the last $timeoutseconds seconds.
y4BR&g%xq6K0q%r $delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");
4z'jt WX if(!($delete)) {
_C ffm)o print "Useronline Delete Failed > ";3Pg t L|TXa
}
m"w!M:j K,A3t7t S"l //select the amount of people online, all uniques, which are online on THIS page
pAtO(\,Wmvsy $result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");5L$|&]7g&S ]
if(!($result)) {
ts({H k print "Useronline Select Error > ";
dS @0U`[)F }
X?p\o8R%wi0} N/} //Count the number of rows = the number of people online/@z7``3S9^
$user = mysql_num_rows($result);(hAP]iF'TIy,Y[
//spit out the results)sN i;S7P2s;Z(f
mysql_close();
YEcj GY ] if($user == 1) {
$V6L2T'A8hR y print("1 user online\n");?:n/s6Vr
} else {
4]%VG)^d~ print("$user users online\n");q&u*I KD
}`0_4a5o&tKb&y
?>9tj f9{9FH2qi#{F
[url=http://www.now.cn/vhost/][img]http://b.todayisp.com/bbs/760x90h.gif[/img][/url]
.V*I$n wZ*SqlD 以上代码我是在时代互联提供的免费试用主机上测试的,感觉效果还不错,性能很稳定,不信的话你也可以去试试。当然,记得指出我这个程序中的不足先哦。
tZR.ankA#o A 时代互联是行业内最早开始提供PHP 虚拟主机的公司之一,现在已经是的金牌域名注册商和中国互联网客户满意度十佳单位,产品功能已经非常完善 ,特别提一下几个特有用的功能:数据库自动备份、免费集成繁简通简装版、多域名绑定、多子网站支持、GCDN 网站加速器、镜像加速、高级访问统计系统、支持WAP、可选电信/网通/香港机房。
(m2wM(?V|~ 我之前建站的时候选的是他们公司标准商务E型,因为这款机型还特别支持ASP.NET3.5(3.0/2.0/1.1)/ASP。jA7] ]:SJ7ezI
当然啦,这两款主机也是相当不错的。8IlL_5~7DDI L
智强商务B型:Unix /Linux 操作系统+独立网页空间1000MB+送1000MB邮局空间+100MB MySQL数据库空间=1869元/年
G:P,n^ eK 标准商务B型:Unix /Linux 操作系统+独立网页空间500MB+送800MB邮局空间+60MB MySQL数据库空间=1365元/年9U5X3[ n l-ix
提供一下这个公司的联系方式:请见:[url=http://www.now.cn/vhost/]http://www.now.cn/vhost/[/url]
"]Zq!lY8H2dk 空间专线: 0756-2623871  QQ:168075865,好象还有全球免费网络电话:全球免费咨询电话 [url=http://www.now.cn/callcenter/call.net?LineName=55]http://www.now.cn/callcenter/call.net?LineName=55[/url]
R)Z/A1n?nT2V.s 自己加QQ去问吧。

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


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