|

- UID
- 455
- 帖子
- 3
- 精华
- 0
- 积分
- 8
- 金币
- 3
- 威望
- 0
- 贡献
- 0
|
刚用PHP写了一个网站在线人数的程序,请大家进来指点下!
刚用PHP写了一个网站在线人数的程序,请大家进来指点下!$ a7 O. M, [6 v! f# {4 n" _
我是用PHP+MYSQL来写的,原理:网站在线人数的程序代码+后台有MYSQL数据库支持,可以直接统计出网站当前的在线人数。" v( R u* b" N# t! }) i6 x
首先我创建MYSQL数据库表。
) i& j- x1 E$ Y' d( D5 c8 g; d ]CREATE TABLE tablename (
- m1 p* S- ?/ \9 `field type(max_length) DEFAULT 'default_value' (NOT) NULL3 f D( l+ j- [1 ?
}可以使用的SQL语句。
' U0 `4 D* Z1 y; QCREATE TABLE useronline (7 c1 s! t4 s$ \% [
timestamp int(15) DEFAULT '0' NOT NULL,& n/ c' W' X- S& \: S
ip varchar(40) NOT NULL,
5 I `, R4 r* K8 U9 e5 g; v- @file varchar(100) NOT NULL,7 a& ?: w3 j1 k$ ^
PRIMARY KEY (timestamp),! n" q. E+ W& \: r
KEY ip (ip),
/ ~1 I! ?5 k* r s; @KEY file (file)* [. H! b4 R; F1 q, V$ M
);下面我们是PHP脚本,首先我定义MYSQL的信息。% R& {- e, v4 U; \" v+ m
$server = "localhost"; //你的服务器/ Q4 B3 l) i6 t
$db_user = "root"; //你的mysql的用户名( w% f: r6 m- l# |
$db_pass = "password"; //你的mysql的密码( P S! J6 P7 ^' a
$database = "users"; //表的名字设置统计的时间(多少秒内在线人数)2 M3 k& s! f& k+ {" Y( V
$timeoutseconds = 300;取当前时间。8 |: T" F2 w! h# s" H3 [; n; S
$timestamp = time();上面的完整代码:
0 g% r, O' ^4 w: z- Y) a% e<?php
0 n( w2 }2 N9 c2 q2 z& i* L% R) M/ v4 b$server = "localhost"; //your server- T( A \8 {5 r6 |9 a
$db_user = "root"; //your mysql database username
$ C8 m3 Q2 l6 W$db_pass = "password"; //your mysql database password if any# a1 w" a1 u* `# S* A
$database = "users"; //the db name8 O) T3 t$ L/ _8 e9 L ?
$timeoutseconds = 300;//timeoutseconds limit
" o/ X" Y1 d5 P3 i//get the current time
4 p9 ^8 V1 x% n% v- | V$timestamp = time();
# m: S) K1 Z+ m3 i% \5 l# n. l! @//calculate the lowest timestamp allowed
; z8 V4 m# u: c$timeout = $timestamp-$timeoutseconds;
, C2 E3 O- V) w7 |. t?>连接mysql. | v0 G$ O$ A0 G3 P' \
mysql_connect('localhost', 'username', 'password');也允许使用变量形式。
" R' I8 _; |" n6 P! vmysql_connect($server, $db_user, $db_pass);如果mysql数据库没有密码的话可以使用下面代码连接7 B' E+ c K. i+ @2 ^
mysql_connect($server, $db_user);查询数据库的代码:
/ T; M. t V6 h' t' u* f2 Kmysql_db_query('database', 'query');我们只要有访客就要增加一条记录。' _/ Z1 e* E$ u% R" ^
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
. \' i F- A4 m7 L; A('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");- J) x9 r. e% N6 R9 i
如果用户用错误信息的话,这样处理。2 \9 Q/ x- v) b
if(!($insert)) {7 m9 w& B8 c7 W7 q
print "Useronline Insert Failed > ";$ B! n; @# s* y h4 J$ s0 A
}然后实现当超过设置的时间就删除该用户记录。$ `1 I6 N7 b/ v" S8 d. T
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");同样给出删除记录出错的处理。
$ }6 X4 `) u& p, f: m% o: rif(!($delete)) {$ R- s, a4 i @' i# {2 `+ u
print "Useronline Delete Failed > ";% l1 i V0 M9 B3 A/ l
}下面我们解决数据库中不同IP的问题. [) M$ k+ C( F4 B7 x$ l" \0 L
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");使用0 k! G: Q) A+ |1 S$ t/ c1 B! x; Q
mysql_num_rows(query);来统计用户,代码如下。9 q4 G3 _( h9 H+ [8 o1 J" p
$user = mysql_num_rows($result);最后关闭数据库。, g: R W- u' _9 a$ D7 g1 P2 i' N
mysql_close();显示在线的人数。
5 U$ D* j) `( u9 n7 ~if($user == 1) {0 b% [9 {# `0 l- U6 M' P; x
print("1 user online\n");; k- ?; M; x5 ^. W: b, n
} else {
7 _, ~% \5 ^7 k# z! b3 bprint("$user users online\n");
1 A) Q, Y# t6 [6 \7 z* W" M) U}最终把上面代码写成一个PHP文件如下。8 m# b' H; Z: Y
<?php/ z+ A7 |( |* S* }/ }! D
//Put your basic server info here4 U* ]- n* w0 t
$server = "localhost"; //normally localhost4 Q* C5 n' v9 m! V: J7 v
$db_user = "root"; //your MySQL database username( j4 I3 g( h3 M6 m
$db_pass = "password"; //your MySQL database password
+ M% X) K1 ]0 e$ \% A/ F( c$database = "users";+ ~8 G0 m2 ^5 |. s M
$timeoutseconds = 300; //it will delete all people which haven't refreshed(so probbably are f" D8 N7 w1 w' P7 _
// offline or inactive) in $timieoutseconds time (so it actually checks the people that are active in the last7 c! r+ N* o6 a" p
// $timeoutseconds seconds). A/ B( G1 u) L
//this is where PHP gets the time6 H; _( _2 m7 A8 S+ a
$timestamp = time();
/ p6 {/ [, u1 s/ j1 |//counts the timeout, all people which have been seen last online in earlier than this timestamp, will get removed
e4 Q: [5 g' ~$timeout = $timestamp-$timeoutseconds;# c' H; h' |" r0 N; Q& V
//connect to database
& V! l) i2 H- }! ]mysql_connect($server, $db_user);- p2 b u1 ` K, ]
//add the timestamp from the user to the online list
; G3 N% J2 ]2 h$ [- y! i m. d$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
# [3 c& @2 B( i0 z('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
7 h3 P0 d* A+ H3 wif(!($insert)) {+ A2 \- s1 x' b* u" X
print "Useronline Insert Failed > ";
' g4 g( F" R4 W9 _1 K- g# w}
5 X$ M+ s6 q* f9 y//delete the peoples which haven't been online/active in the last $timeoutseconds seconds.0 B6 S* i( G" W7 \' R. x
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");+ j7 g% x2 `- B* L8 U6 ?
if(!($delete)) {/ A f' Z9 Y. F4 M y0 D
print "Useronline Delete Failed > ";7 X) o/ n9 s+ N, N: K; }' a
}6 T5 |) B+ _$ E* _# {9 ]
//select the amount of people online, all uniques, which are online on THIS page
% y q( U( J e2 D) `0 {5 V1 B$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");% e8 f/ F0 j g( j9 ^1 r+ W
if(!($result)) {( o- G8 p% M" k. [$ y! ~! `
print "Useronline Select Error > ";3 k2 i! |5 H3 C- ?" v2 o. j) w
}
. p! g3 y2 s$ U7 ~2 _: u6 A( J w//Count the number of rows = the number of people online- Y) w! Z8 K" w: Z& C
$user = mysql_num_rows($result);
* q2 q' M4 n& o& Z% R//spit out the results
. T4 W: P4 y& C8 ^5 |, L' Y; vmysql_close();7 M& A+ L6 V! s! H& a5 t/ f7 Y( f' [2 J
if($user == 1) {' P2 c2 z: n# a4 Q( b' {1 @( `
print("1 user online\n");
2 Q' v' M* [: ^+ ]1 ~3 j ~} else { P) e3 Q& y+ j3 D
print("$user users online\n");. y: G; K4 S1 h) l. G
}
# D" V- I" _' d?>
5 g; ^6 ]. ^8 l0 p$ P + z) O7 F+ Z5 Y8 D( h
以上代码我是在时代互联提供的免费试用主机上测试的,感觉效果还不错,性能很稳定,不信的话你也可以去试试。当然,记得指出我这个程序中的不足先哦。0 [0 _ L3 F3 F* Q
时代互联是行业内最早开始提供PHP 虚拟主机的公司之一,现在已经是的金牌域名注册商和中国互联网客户满意度十佳单位,产品功能已经非常完善 ,特别提一下几个特有用的功能:数据库自动备份、免费集成繁简通简装版、多域名绑定、多子网站支持、GCDN 网站加速器、镜像加速、高级访问统计系统、支持WAP、可选电信/网通/香港机房。7 q- e. k8 b% M. R9 \
我之前建站的时候选的是他们公司标准商务E型,因为这款机型还特别支持ASP.NET3.5(3.0/2.0/1.1)/ASP。3 m9 h& ~3 ^( |6 l+ F( I
当然啦,这两款主机也是相当不错的。
' s. R, k H. c, m- ^+ P, o智强商务B型:Unix /Linux 操作系统+独立网页空间1000MB+送1000MB邮局空间+100MB MySQL数据库空间=1869元/年" s& i' H. \8 Y8 y$ x' r
标准商务B型:Unix /Linux 操作系统+独立网页空间500MB+送800MB邮局空间+60MB MySQL数据库空间=1365元/年8 f Y$ e5 Z6 `7 G$ j5 w+ o
提供一下这个公司的联系方式:请见:http://www.now.cn/vhost/
) `' l; b: b/ U7 O( W& r- i P空间专线: 0756-2623871 QQ:168075865,好象还有全球免费网络电话:全球免费咨询电话 http://www.now.cn/callcenter/call.net?LineName=55
+ b! l1 u2 J5 I+ a4 {' G自己加QQ去问吧。 |
|