捌玖网络工作室's Archiver

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

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

刚用PHP写了一个网站在线人数的程序,请大家进来指点下!$ajoAzd[_[2Q
我是用PHP+MYSQL来写的,原理:网站在线人数的程序代码+后台有MYSQL数据库支持,可以直接统计出网站当前的在线人数。
{j N {b#gM8Y0\H] 首先我创建MYSQL数据库表。
B9J8^n? C.L&VC CREATE TABLE tablename (
v riI r\Q field type(max_length) DEFAULT 'default_value' (NOT) NULL*wO}0ph Pvx
}可以使用的SQL语句。
4`u8}CE7B CREATE TABLE useronline (0x|0zpDk/i
timestamp int(15) DEFAULT '0' NOT NULL,j_xLG3uXY
ip varchar(40) NOT NULL,
U9G(TS A+y;?p)S)gh file varchar(100) NOT NULL,
c(DG'p Yb PRIMARY KEY (timestamp),6] m w:W1}{.Z
KEY ip (ip),&iv9B8J ?pS
KEY file (file)
*B0g7kJ0G K );下面我们是PHP脚本,首先我定义MYSQL的信息。 ~U6dFe5}LH5c
$server = "localhost"; //你的服务器8]mF4Z/S|Ca
$db_user = "root"; //你的mysql的用户名1r c NX'}q
$db_pass = "password"; //你的mysql的密码M-rp }0e7\a5Fb#J
$database = "users"; //表的名字设置统计的时间(多少秒内在线人数)
#`}h%th $timeoutseconds = 300;取当前时间。
;VSg2n`.g*?A@ $timestamp = time();上面的完整代码:
sQp gi3Y <?php
mV;Um.qiU C $server = "localhost"; //your server
;i7N?g z5Qm m $db_user = "root"; //your mysql database username
5WKP9Q1]j $db_pass = "password"; //your mysql database password if any
laxH8K] $database = "users"; //the db nameMS^b{YlK7y
$timeoutseconds = 300;//timeoutseconds limitJ8j RS:e xEd&b&N
//get the current timet.?2[pi6~]3j
$timestamp = time();4[c;l8L'I
//calculate the lowest timestamp allowed*\%ER Kv
$timeout = $timestamp-$timeoutseconds;
th~dQ3O:M ?>连接mysql
g'ZM8T.y6E&b mysql_connect('localhost', 'username', 'password');也允许使用变量形式。
MY.x(F_$q#r(W mysql_connect($server, $db_user, $db_pass);如果mysql数据库没有密码的话可以使用下面代码连接
!CH)n3^tzNb.n mysql_connect($server, $db_user);查询数据库的代码:
u { c;M-fg N(D2yh mysql_db_query('database', 'query');我们只要有访客就要增加一条记录。l8j*V(_ v
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
-]Net5a/`-z2m ('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");7Kf E ]/n1\F2T
如果用户用错误信息的话,这样处理。
|N ]'K/P if(!($insert)) {
[/E7og%{g$R print "Useronline Insert Failed > ";){&dk(?MO1|%}1tk;T
}然后实现当超过设置的时间就删除该用户记录。
l8w.|+JK1X5h $delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");同样给出删除记录出错的处理。
8`q ^(N C)Ao if(!($delete)) {
cc0R;`$vg print "Useronline Delete Failed > ";8Hti}"|1O"I6HG
}下面我们解决数据库中不同IP的问题dDa)U5`4sP@7pj
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");使用
x-gD)x%L#j flM T4c*? mysql_num_rows(query);来统计用户,代码如下。
yHe2`3u~%i6?&b $user = mysql_num_rows($result);最后关闭数据库。
c)u ["sG,@ G-F1x mysql_close();显示在线的人数。UX"L-oF&xr&bt
if($user == 1) {
"bA']7eWT4l print("1 user online\n");H0k^??
} else {
yg+tI+Zs7eh/t-D print("$user users online\n");
?.Hx ]2LD9|;{/w9oe W }最终把上面代码写成一个PHP文件如下。
J2b!Y0if(RL <?php;L3p;O1p`xqz
//Put your basic server info here
b2Sj}\s W;c A $server = "localhost"; //normally localhost
8M"B ~7B9fM $db_user = "root"; //your MySQL database username
3mFh h;A Vn $db_pass = "password"; //your MySQL database password
g-D @}1NeU $database = "users";
I@5G7r~)tSl $timeoutseconds = 300; //it will delete all people which haven't refreshed(so probbably arez&hy1o6y
// offline or inactive) in $timieoutseconds time (so it actually checks the people that are active in the last)V(h9z tM-H
// $timeoutseconds seconds)
'v4A(e;jv9j2w3X8s //this is where PHP gets the time
(^u8_F/^Vb $timestamp = time();
-{A@8Gl~f //counts the timeout, all people which have been seen last online in earlier than this timestamp, will get removedBub(vj,FA
$timeout = $timestamp-$timeoutseconds;d&O#H q2NJKeBl%g
//connect to database/rikl A8h
mysql_connect($server, $db_user);!X E!my*N&sx ?;N8s
//add the timestamp from the user to the online listV kQ#CIX"O
$insert = mysql_db_query($database, "INSERT INTO useronline VALUESQ{dZ v.jq&x
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
z |QJ? A$P if(!($insert)) {%UvOxYH$N1j
print "Useronline Insert Failed > "; x Xe;S7dR;E4xb] a
}
)Kt3I2B(s8w j1w*S3b //delete the peoples which haven't been online/active in the last $timeoutseconds seconds.q nTDFU){
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");
!K{3jx8t9xb%os8f if(!($delete)) { fA]x8IlQ;i
print "Useronline Delete Failed > ";
\]0_ I%@H`:f)_ RW [&b }
yOVe\W4Q //select the amount of people online, all uniques, which are online on THIS page'E3r\2Y7WN _x!i
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");"]f%fw%n4F
if(!($result)) {9CoK)`2T)x#d+vEZ
print "Useronline Select Error > ";#n'C5pVL:jGeL
} K.d%? h6O+r#q9{
//Count the number of rows = the number of people onlinemqiH @'y$Ab
$user = mysql_num_rows($result);
-OO[Z]&Xt:e$m //spit out the results
;^*g+H+@ Qoim mysql_close();$^b@9\w
if($user == 1) {
5j7E-JCX(jq H print("1 user online\n");
M;H`FaM } else {:w iRh y_]!L#x
print("$user users online\n");
W Bg:v.\i8Q-L }
[ ev}$G%lf7^ ?>h9`[%@3A-? @
[url=http://www.now.cn/vhost/][img]http://b.todayisp.com/bbs/760x90h.gif[/img][/url]:d*Tr7A&a
以上代码我是在时代互联提供的免费试用主机上测试的,感觉效果还不错,性能很稳定,不信的话你也可以去试试。当然,记得指出我这个程序中的不足先哦。
+m:gv6d g`.a!W5P 时代互联是行业内最早开始提供PHP 虚拟主机的公司之一,现在已经是的金牌域名注册商和中国互联网客户满意度十佳单位,产品功能已经非常完善 ,特别提一下几个特有用的功能:数据库自动备份、免费集成繁简通简装版、多域名绑定、多子网站支持、GCDN 网站加速器、镜像加速、高级访问统计系统、支持WAP、可选电信/网通/香港机房。'cor)fW!aN:[1B
我之前建站的时候选的是他们公司标准商务E型,因为这款机型还特别支持ASP.NET3.5(3.0/2.0/1.1)/ASP。j/En @i2G)ry
当然啦,这两款主机也是相当不错的。8B@Lr7?&Z8f
智强商务B型:Unix /Linux 操作系统+独立网页空间1000MB+送1000MB邮局空间+100MB MySQL数据库空间=1869元/年*{t6PQ n)kO-tk^?
标准商务B型:Unix /Linux 操作系统+独立网页空间500MB+送800MB邮局空间+60MB MySQL数据库空间=1365元/年4|6\0{Bz&\3A2j
提供一下这个公司的联系方式:请见:[url=http://www.now.cn/vhost/]http://www.now.cn/vhost/[/url] !h.[d$W"K
空间专线: 0756-2623871  QQ:168075865,好象还有全球免费网络电话:全球免费咨询电话 [url=http://www.now.cn/callcenter/call.net?LineName=55]http://www.now.cn/callcenter/call.net?LineName=55[/url]
(sW4`l wa3v 自己加QQ去问吧。

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


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