|

- UID
- 455
- 帖子
- 3
- 精华
- 0
- 积分
- 8
- 金币
- 3
- 威望
- 0
- 贡献
- 0
|
刚用PHP写了一个网站在线人数的程序,请大家进来指点下!
刚用PHP写了一个网站在线人数的程序,请大家进来指点下!
$ t2 Y) ^* l+ X. G5 g% a+ c" e% u我是用PHP+MYSQL来写的,原理:网站在线人数的程序代码+后台有MYSQL数据库支持,可以直接统计出网站当前的在线人数。8 i* t8 D, ?% i; f, C4 a5 y
首先我创建MYSQL数据库表。: Z& g. K! c+ }! l B' J8 U
CREATE TABLE tablename (
0 H V& u: r4 {1 afield type(max_length) DEFAULT 'default_value' (NOT) NULL h4 h3 p9 R! o: v
}可以使用的SQL语句。
2 S7 o: P/ G2 t- f6 Y$ p4 m8 cCREATE TABLE useronline (& `2 u& p/ r4 `3 L: a& a3 A$ I
timestamp int(15) DEFAULT '0' NOT NULL,1 E. u- C! c) x' m' k9 D
ip varchar(40) NOT NULL,
$ E1 s9 R4 a( i) ofile varchar(100) NOT NULL,
8 k4 n' K5 Z8 BPRIMARY KEY (timestamp),; S' X$ X& y" [" K7 D/ w
KEY ip (ip),
/ ^" l- D5 r4 ~8 `8 _: P& A: yKEY file (file)
, f9 q' |) Y3 M, [. t);下面我们是PHP脚本,首先我定义MYSQL的信息。
! y. ?& c3 y* d4 |3 O' n. @, ~$server = "localhost"; //你的服务器
# R/ ^3 n& ~/ ?8 T1 w' E! M$db_user = "root"; //你的mysql的用户名% K# e9 V: @9 Q/ D( i4 z+ f0 d
$db_pass = "password"; //你的mysql的密码! {( G3 ?) b) e1 _/ _
$database = "users"; //表的名字设置统计的时间(多少秒内在线人数)
/ v; U! A& f2 Q; `$ K n' U$timeoutseconds = 300;取当前时间。
1 g0 U, F) k) ?. P6 u$timestamp = time();上面的完整代码:
- }, r& r2 K( H) Z6 G4 z<?php2 _7 J3 n( |' l6 R) R, S
$server = "localhost"; //your server. \* x5 u7 s8 x. y' P1 |
$db_user = "root"; //your mysql database username2 o, w! }" |. @- C$ k3 I ~
$db_pass = "password"; //your mysql database password if any
& M7 e* \' n( i9 \# L: K7 z$database = "users"; //the db name4 v6 l6 D. \+ b- Y, C8 Z; F% ?
$timeoutseconds = 300;//timeoutseconds limit
- ~* U; q1 h. K# X; Y//get the current time
1 O- [# Q6 x; U6 f$timestamp = time();+ k3 h F6 m. I
//calculate the lowest timestamp allowed c: s7 F1 E+ ?! D
$timeout = $timestamp-$timeoutseconds;7 @$ m1 S& A) c/ o" W8 v
?>连接mysql
2 z; A3 N( R+ P9 k! Nmysql_connect('localhost', 'username', 'password');也允许使用变量形式。
( F- X7 y' |; n* p" \# m- pmysql_connect($server, $db_user, $db_pass);如果mysql数据库没有密码的话可以使用下面代码连接
7 O4 e, f# Y8 c# f5 ymysql_connect($server, $db_user);查询数据库的代码:/ B4 F8 o2 Z- n4 S
mysql_db_query('database', 'query');我们只要有访客就要增加一条记录。
% I. D' y y, }; Q$insert = mysql_db_query($database, "INSERT INTO useronline VALUES9 h' a& |1 M; t3 ?( f! V) x
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
' z* C8 W3 ]! {' ^1 V: x如果用户用错误信息的话,这样处理。
. {, @; j8 s% r, c; t- _if(!($insert)) {
+ r( p" e$ s3 ], Z+ Yprint "Useronline Insert Failed > ";7 C& _/ ]& v8 x
}然后实现当超过设置的时间就删除该用户记录。
R8 v, d3 s% p' @) d3 b$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");同样给出删除记录出错的处理。1 R4 a4 N- n7 R! g8 z, ~
if(!($delete)) {" { N, ^# H; e! \
print "Useronline Delete Failed > ";2 C' j2 p: S# o7 x, E/ s' i* ~9 }# n
}下面我们解决数据库中不同IP的问题% E6 {9 O3 W5 \( n) u
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");使用* f% U5 M* [; t0 ~
mysql_num_rows(query);来统计用户,代码如下。! H9 q, c7 o/ x" ~3 D
$user = mysql_num_rows($result);最后关闭数据库。/ [4 j( _7 i& E0 W2 h8 m
mysql_close();显示在线的人数。
4 J+ }; b) X( h' v/ ?) Tif($user == 1) {
: r" w& w# t' W- Z9 r7 c+ Nprint("1 user online\n");: O8 I2 s: `9 r
} else {
- ?5 P) ]' ~% F6 wprint("$user users online\n");$ S& I$ x, g6 L! o9 F3 q
}最终把上面代码写成一个PHP文件如下。1 F" q* m1 ^, e6 l6 H
<?php
; s" _# t. m3 q: u0 c' e//Put your basic server info here* Y/ X5 ^% M9 U2 \) P; \
$server = "localhost"; //normally localhost/ ~+ d/ i& o% P9 [1 Z
$db_user = "root"; //your MySQL database username( Y7 n; l* B$ U4 b, I4 }
$db_pass = "password"; //your MySQL database password
4 n* N- F f% M$database = "users";
8 ^) l- v) x e$ a% e0 |! K$timeoutseconds = 300; //it will delete all people which haven't refreshed(so probbably are
% |: \' @4 Q& d5 I, j3 h// offline or inactive) in $timieoutseconds time (so it actually checks the people that are active in the last0 J) a2 a) F# T8 M: L
// $timeoutseconds seconds)
0 T# Y ~0 d5 s2 C- V//this is where PHP gets the time5 D& c: P4 Q3 ?
$timestamp = time();
9 L: P" j- e9 u" ~//counts the timeout, all people which have been seen last online in earlier than this timestamp, will get removed2 b" W2 q, g4 Z% _. y3 h. V; _
$timeout = $timestamp-$timeoutseconds;
3 s; Z2 E! P! X" ^3 F//connect to database
; \2 n7 W3 v& C0 M+ S! P) ^& S$ Smysql_connect($server, $db_user); ^9 z: L8 @* s/ F4 ^" R- _- [
//add the timestamp from the user to the online list
' U2 x9 w- w) X: q* |$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
/ o* @* {* b9 I+ b& @9 ]('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
. ^/ `, G7 l1 X1 }" ?1 tif(!($insert)) {
9 ^5 i9 _( M& t1 b ?1 Nprint "Useronline Insert Failed > ";
9 ?. p- {" K% i7 e, R}
: _# q9 S! x- `* X) Z! g& ]//delete the peoples which haven't been online/active in the last $timeoutseconds seconds.
/ w+ U1 E* X) O$ c1 s$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");3 P/ R2 T# h9 v* _
if(!($delete)) {
) d# H; Z- i" q7 c. lprint "Useronline Delete Failed > ";9 S) A5 |& ]1 i+ u
}0 w5 W8 p; e4 j0 m3 E+ E
//select the amount of people online, all uniques, which are online on THIS page! l) ~ C3 M7 o3 I) y6 Y5 O/ N0 _/ ^
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");
% d( p( f0 o- j; B mif(!($result)) {
) L4 S+ d! J+ O3 }9 X0 P( I1 iprint "Useronline Select Error > ";0 d$ ^$ ^# j. j8 u" ~, I# V8 h8 L$ f
}5 g: `. ? y$ I
//Count the number of rows = the number of people online. \5 l5 d* G5 f
$user = mysql_num_rows($result);' J8 Y5 B! k$ B2 w: A* s; q
//spit out the results
' M% P1 x, N, Y* Wmysql_close();& \- n: M& M% k _7 @2 S
if($user == 1) {( K. ^" E4 \4 [
print("1 user online\n");9 x9 S4 _; [ L, Z( A
} else {8 d( T [- u R; f0 ?
print("$user users online\n");: S# i7 C7 w/ ^- r
}
& }1 Q& Z/ R$ M7 F6 ]3 R?>
; z" A- L# O6 f2 x& P# R: w - |, j" a- Q+ n g! K( x0 g
以上代码我是在时代互联提供的免费试用主机上测试的,感觉效果还不错,性能很稳定,不信的话你也可以去试试。当然,记得指出我这个程序中的不足先哦。. O, X% I: Q" G7 v) d
时代互联是行业内最早开始提供PHP 虚拟主机的公司之一,现在已经是的金牌域名注册商和中国互联网客户满意度十佳单位,产品功能已经非常完善 ,特别提一下几个特有用的功能:数据库自动备份、免费集成繁简通简装版、多域名绑定、多子网站支持、GCDN 网站加速器、镜像加速、高级访问统计系统、支持WAP、可选电信/网通/香港机房。
' r* o6 h7 L0 u6 N6 ^. T6 P8 H我之前建站的时候选的是他们公司标准商务E型,因为这款机型还特别支持ASP.NET3.5(3.0/2.0/1.1)/ASP。9 L& ^5 G/ L& B7 }+ O
当然啦,这两款主机也是相当不错的。
0 v9 S+ Z; q1 }( \8 ^智强商务B型:Unix /Linux 操作系统+独立网页空间1000MB+送1000MB邮局空间+100MB MySQL数据库空间=1869元/年
3 t2 h+ a( Q* c; V5 t' V标准商务B型:Unix /Linux 操作系统+独立网页空间500MB+送800MB邮局空间+60MB MySQL数据库空间=1365元/年
7 L/ @' Z% ~3 j提供一下这个公司的联系方式:请见:http://www.now.cn/vhost/
( Q1 |6 l# k/ F% y空间专线: 0756-2623871 QQ:168075865,好象还有全球免费网络电话:全球免费咨询电话 http://www.now.cn/callcenter/call.net?LineName=555 f- W3 y% T1 o% Q. R( j. E6 Q6 }
自己加QQ去问吧。 |
|