|

- UID
- 455
- 帖子
- 3
- 精华
- 0
- 积分
- 8
- 金币
- 3
- 威望
- 0
- 贡献
- 0
|
刚用PHP写了一个网站在线人数的程序,请大家进来指点下!
刚用PHP写了一个网站在线人数的程序,请大家进来指点下!6 C8 A/ Y& x, V$ b, s8 f# @; ]& E
我是用PHP+MYSQL来写的,原理:网站在线人数的程序代码+后台有MYSQL数据库支持,可以直接统计出网站当前的在线人数。, U( h' N% o" P: O# M( a
首先我创建MYSQL数据库表。! [5 |$ d5 J. \# ]. q" d% V) g
CREATE TABLE tablename (% I* ]9 {; H$ q
field type(max_length) DEFAULT 'default_value' (NOT) NULL
) R; z. t) _3 e# ]}可以使用的SQL语句。
9 O8 {7 p1 S9 Z0 cCREATE TABLE useronline (
) r6 Q7 h9 c$ k1 atimestamp int(15) DEFAULT '0' NOT NULL,
! k4 I, s% m e- D: W5 f0 Oip varchar(40) NOT NULL,6 c) @- }) y7 @4 i
file varchar(100) NOT NULL,9 f0 p. b J, X& y9 p2 y
PRIMARY KEY (timestamp),
9 k l" W+ L9 C- H% l; _KEY ip (ip),/ G, _ O$ d6 Y1 v$ c7 k% \
KEY file (file). D+ x5 c) H* @/ {% r2 A4 M
);下面我们是PHP脚本,首先我定义MYSQL的信息。
k. ^, |8 R3 m0 p. O& \; I+ k# O$server = "localhost"; //你的服务器
& T" Y% b+ a! G3 A$ u# o# c$db_user = "root"; //你的mysql的用户名) u- L# _/ [9 C# ?# Y) j
$db_pass = "password"; //你的mysql的密码
' w) F$ r# m, k$database = "users"; //表的名字设置统计的时间(多少秒内在线人数)
5 s) ~. M2 m( d" I! ^# C f' b$timeoutseconds = 300;取当前时间。- E. B2 d* t) h8 U. E
$timestamp = time();上面的完整代码:
* `9 o" Y( q- X/ m4 q( V/ l/ }4 j<?php( _* x, a M% J# k
$server = "localhost"; //your server3 e1 {' k8 z. A# y u8 m) Q2 K
$db_user = "root"; //your mysql database username
6 u. `# k9 s" l5 b- N; H1 z. |$db_pass = "password"; //your mysql database password if any* t* w! r4 ~8 g! Z. A" V z8 f
$database = "users"; //the db name" Q! x! J! ]" _# F8 l& d# b/ F
$timeoutseconds = 300;//timeoutseconds limit
& ~5 ]6 S* m" h" T3 H//get the current time
) j7 T/ v2 n/ H/ |4 W$ G% j$timestamp = time();
" G/ C' _7 _* W5 o- s! h4 z8 K//calculate the lowest timestamp allowed
/ E$ L( ^& ~' |8 {' x- Q$timeout = $timestamp-$timeoutseconds;
+ [, y4 M4 G8 R8 I?>连接mysql
6 r. J C8 X. H+ Y6 |mysql_connect('localhost', 'username', 'password');也允许使用变量形式。9 |4 R% M% ~1 m) D& s6 P: c- @
mysql_connect($server, $db_user, $db_pass);如果mysql数据库没有密码的话可以使用下面代码连接
3 q4 A: W, j( o3 Y4 [4 X {mysql_connect($server, $db_user);查询数据库的代码:
! F. C) @1 Y6 k3 [9 I0 E* Rmysql_db_query('database', 'query');我们只要有访客就要增加一条记录。
: Q' o8 }1 A" e$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
2 a$ a6 \+ J/ P ~/ P$ R* N('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");! v2 }+ W0 Q u! B
如果用户用错误信息的话,这样处理。
' V6 q0 E }1 y1 p" ]if(!($insert)) {) k4 ^1 I X) [5 a
print "Useronline Insert Failed > ";* w% Q8 o; Q' W. y* J2 y
}然后实现当超过设置的时间就删除该用户记录。
7 g& R$ @2 d; J8 \$ L0 U9 w$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");同样给出删除记录出错的处理。
0 l3 V, U3 @& J6 Nif(!($delete)) {( ]0 o' ~: A0 k- i+ u, H% V2 O
print "Useronline Delete Failed > ";
$ H& M1 T1 `. i/ @}下面我们解决数据库中不同IP的问题. b& \( H7 ?; _8 e2 `
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");使用, k! Q/ V4 I$ B8 E n% p) S
mysql_num_rows(query);来统计用户,代码如下。6 i# \$ j& \5 `, L
$user = mysql_num_rows($result);最后关闭数据库。
$ O% q5 h8 U& R% h% mmysql_close();显示在线的人数。
x1 L; Q$ `9 h& Q6 ^# Eif($user == 1) {4 ?9 T6 ?6 G' l, N8 p: z5 U
print("1 user online\n");+ S6 l: L- v! P% s0 G& Z
} else {; q; r% }) q5 X7 {# ^' ^
print("$user users online\n");2 A+ X# V3 o9 T3 S
}最终把上面代码写成一个PHP文件如下。
6 n8 P: u/ z. [3 T. }5 F<?php
+ u" j5 K) @5 }) [' ~* C7 Z//Put your basic server info here
) \7 h+ P, J6 p# h$ A) }* j V$server = "localhost"; //normally localhost/ Y+ r4 T. ~6 O) B; n& w
$db_user = "root"; //your MySQL database username" S% r& b* O; S
$db_pass = "password"; //your MySQL database password- \0 J( _/ a$ k4 O# @
$database = "users";' z2 h6 ]. Q3 r# X) J( M% j( b! [
$timeoutseconds = 300; //it will delete all people which haven't refreshed(so probbably are
: D! `& C1 w" J// offline or inactive) in $timieoutseconds time (so it actually checks the people that are active in the last
1 M# @9 l* I- H: G// $timeoutseconds seconds)9 ? C) ~% P6 T+ Z. I; y j/ b
//this is where PHP gets the time1 F' v( w' Q, ?+ D2 {5 b
$timestamp = time(); `0 R$ g) f+ S" I7 J2 J
//counts the timeout, all people which have been seen last online in earlier than this timestamp, will get removed
9 O" K) N* g3 t, W6 m$timeout = $timestamp-$timeoutseconds;
5 N3 Z/ ?8 f/ H0 r) v$ Z//connect to database1 {4 ], f) r/ Q: U4 g
mysql_connect($server, $db_user);- d' W# p& M. G& Y( q
//add the timestamp from the user to the online list8 w; m& j; Y3 G* I0 y' _5 z
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES* u4 f$ ~) y) f. Y
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");3 q, @; u& _4 x9 \
if(!($insert)) {' E6 A& N9 o( Z' M6 e3 {
print "Useronline Insert Failed > ";& k" V* H" X" j. T/ A
}
0 l) f, S. n- b" [//delete the peoples which haven't been online/active in the last $timeoutseconds seconds.) O4 R% A" W, q* J' o
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");
/ ]1 }: h' h, E4 hif(!($delete)) {2 c( x9 C/ {) J4 B: m
print "Useronline Delete Failed > ";/ P* ~8 T8 R* V/ H" L' _. t' b6 j
}; _4 u! p/ I# n$ s$ A9 u. W, Z9 k
//select the amount of people online, all uniques, which are online on THIS page
$ `7 A6 U9 U. _1 |6 z$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");7 S5 F+ t2 p7 v* b' x
if(!($result)) { y3 r- E$ z7 @% r3 f' ]: G- L
print "Useronline Select Error > ";6 {2 C! @5 u" P9 S0 b9 Z) T+ ~
}
6 z3 u& }8 w/ N* K//Count the number of rows = the number of people online
9 f+ K$ c. g/ ]) a$user = mysql_num_rows($result);
% i0 g$ x, Q& i//spit out the results q# V: u" Y, ^ O# t% M
mysql_close();2 V. w+ B/ _0 z; l! t y4 w U
if($user == 1) {/ Z" r ?% r3 C8 V
print("1 user online\n");
7 M* a+ i) \+ b2 ^) q0 q} else {* k1 s0 O9 U- j7 z! ~
print("$user users online\n");
+ V5 V0 v# l) t; d( G* S N2 \}
0 [: l9 [9 H1 o6 Q4 I" D?>/ y; t6 O$ x4 q

5 b$ k9 g2 k1 K7 ]1 Q以上代码我是在时代互联提供的免费试用主机上测试的,感觉效果还不错,性能很稳定,不信的话你也可以去试试。当然,记得指出我这个程序中的不足先哦。
- ^" n- e4 I8 T+ g0 e2 D时代互联是行业内最早开始提供PHP 虚拟主机的公司之一,现在已经是的金牌域名注册商和中国互联网客户满意度十佳单位,产品功能已经非常完善 ,特别提一下几个特有用的功能:数据库自动备份、免费集成繁简通简装版、多域名绑定、多子网站支持、GCDN 网站加速器、镜像加速、高级访问统计系统、支持WAP、可选电信/网通/香港机房。" t/ m. y" i/ Z a6 W
我之前建站的时候选的是他们公司标准商务E型,因为这款机型还特别支持ASP.NET3.5(3.0/2.0/1.1)/ASP。3 f+ ^$ v* i O/ N# u) x
当然啦,这两款主机也是相当不错的。
/ A, g6 V l: q( W: M智强商务B型:Unix /Linux 操作系统+独立网页空间1000MB+送1000MB邮局空间+100MB MySQL数据库空间=1869元/年
7 m1 }/ J8 \0 I+ C3 Y$ X @$ s标准商务B型:Unix /Linux 操作系统+独立网页空间500MB+送800MB邮局空间+60MB MySQL数据库空间=1365元/年1 f; x; _9 K) Y) W9 {) X j) c
提供一下这个公司的联系方式:请见:http://www.now.cn/vhost/ 5 z( `+ C& O6 @# H
空间专线: 0756-2623871 QQ:168075865,好象还有全球免费网络电话:全球免费咨询电话 http://www.now.cn/callcenter/call.net?LineName=55
2 j+ c& w' u1 q+ `4 g H自己加QQ去问吧。 |
|