|

- UID
- 455
- 帖子
- 3
- 精华
- 0
- 积分
- 8
- 金币
- 3
- 威望
- 0
- 贡献
- 0
|
刚用PHP写了一个网站在线人数的程序,请大家进来指点下!
刚用PHP写了一个网站在线人数的程序,请大家进来指点下!
: I6 t3 b8 E# L+ G- Z我是用PHP+MYSQL来写的,原理:网站在线人数的程序代码+后台有MYSQL数据库支持,可以直接统计出网站当前的在线人数。
" G. w. V% I9 I首先我创建MYSQL数据库表。0 z- x- {( b6 P( f' t4 n
CREATE TABLE tablename (
2 K0 m6 Q. b8 E6 Q' _2 P7 D6 o; t; ?) tfield type(max_length) DEFAULT 'default_value' (NOT) NULL' C+ n4 F4 z8 E3 a
}可以使用的SQL语句。# ^- b+ T7 ^+ f1 ^# ]
CREATE TABLE useronline ($ V5 k$ h! w* W8 l. V5 x/ O
timestamp int(15) DEFAULT '0' NOT NULL,
, w% A7 n1 H( kip varchar(40) NOT NULL,0 O, H! B2 T4 `% N! y1 y
file varchar(100) NOT NULL,
2 G2 V' c) @& C' I+ _! ePRIMARY KEY (timestamp),
; Y3 X+ y* k9 \9 }7 N3 H' d5 IKEY ip (ip),
) E. @5 `$ S6 s, r- q3 fKEY file (file)6 P1 n! j' o$ W; w- h1 J# S
);下面我们是PHP脚本,首先我定义MYSQL的信息。
* v3 ~/ `7 `4 u8 q* a$server = "localhost"; //你的服务器. R0 E% ^! d( e! i/ W5 O7 J
$db_user = "root"; //你的mysql的用户名; v8 }$ K1 H/ C. t x& ?7 d% l
$db_pass = "password"; //你的mysql的密码
1 Q/ n# g, z6 K. K1 L3 s, c' u$database = "users"; //表的名字设置统计的时间(多少秒内在线人数)1 v. ?3 |5 k8 F
$timeoutseconds = 300;取当前时间。
5 T0 W1 v* {: Y6 }$timestamp = time();上面的完整代码:
: ]! T8 w t) s+ l+ k# M<?php
" B7 E- |: h4 }/ {3 [8 ^! o" \$server = "localhost"; //your server
2 c( K& F1 Z k- V* T8 u7 G. k$db_user = "root"; //your mysql database username
" u# g# `8 J; a. M$db_pass = "password"; //your mysql database password if any
$ d% Z: b. R e5 m% M7 j% H# v$database = "users"; //the db name4 e d) B4 x W. K8 |& E
$timeoutseconds = 300;//timeoutseconds limit( Q/ R$ [9 _& h' o% s3 H
//get the current time
$ Y# }/ I% m2 T$timestamp = time();
* C! v/ y, S6 z9 B2 U& j//calculate the lowest timestamp allowed
$ O. V2 F5 K' |$ ~/ k, j$timeout = $timestamp-$timeoutseconds;+ T: ?0 m5 S. @+ O9 M, K! U5 S0 B) U
?>连接mysql, n( z6 N/ d8 s* H1 T
mysql_connect('localhost', 'username', 'password');也允许使用变量形式。
! |7 @9 w( D ^! d. tmysql_connect($server, $db_user, $db_pass);如果mysql数据库没有密码的话可以使用下面代码连接% w/ b/ e. T9 L4 V/ E1 u
mysql_connect($server, $db_user);查询数据库的代码:
6 _' r! m6 I9 \2 J9 u, @: Wmysql_db_query('database', 'query');我们只要有访客就要增加一条记录。
) @" [' x1 t, q6 I& g$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
) B9 x) A/ T0 Y('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
' r5 S, X7 ~, Q' t& o! M$ c! C如果用户用错误信息的话,这样处理。
0 g/ V2 ~3 F; y0 |/ Y- f/ |3 T) V o* hif(!($insert)) {
. t) z" d" m/ b0 Fprint "Useronline Insert Failed > ";- e h# I/ W0 H: S
}然后实现当超过设置的时间就删除该用户记录。8 \) D v; h" w
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");同样给出删除记录出错的处理。
9 v' S0 H/ @( b1 ^0 C. ~if(!($delete)) {
i3 j; r" ?2 @" y% c1 O' \print "Useronline Delete Failed > ";* I' q- u$ t: G5 L# m
}下面我们解决数据库中不同IP的问题
' f. r+ f6 _5 r( l& l' t$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");使用# |/ V h+ k; b- L5 K
mysql_num_rows(query);来统计用户,代码如下。
" X+ \4 H7 d, X: E: c$user = mysql_num_rows($result);最后关闭数据库。
. z: W; c: `. ?* K) I8 Kmysql_close();显示在线的人数。, i6 }) ^+ x$ a9 ]/ \* V
if($user == 1) { x( T" K4 q* C! U. @7 ]( N/ f
print("1 user online\n");
0 T0 u$ t& k) ^% A J# F& q9 B} else {
# K" A8 q6 E0 t' L J2 Z) D- [print("$user users online\n");
0 G |+ p; J' v}最终把上面代码写成一个PHP文件如下。
! W! Z! l9 |5 \- b+ ~, P9 v<?php
& A9 W3 ]2 y( E( @. K! c# ~ i; m//Put your basic server info here
3 A8 E" S: G5 E9 e* a3 o$server = "localhost"; //normally localhost
' ^% t7 a; o: _, ^* V) G& [$db_user = "root"; //your MySQL database username9 ]: x5 r% G/ q/ g) ?; l
$db_pass = "password"; //your MySQL database password
& k; ]- `1 v- D( H0 B$database = "users";) Y8 B" \3 z Y7 f, ^; V1 ^
$timeoutseconds = 300; //it will delete all people which haven't refreshed(so probbably are
) { l+ q8 V$ o: {; d1 z// offline or inactive) in $timieoutseconds time (so it actually checks the people that are active in the last1 [9 K- g$ l7 i$ s
// $timeoutseconds seconds)
+ O2 c5 Q# ?3 |9 d0 k; ^; _$ ~/ K8 ^//this is where PHP gets the time
- P" g2 k/ ?/ n/ d; f8 p7 S4 [% r$timestamp = time();) t+ o" a( M' h& ?8 F
//counts the timeout, all people which have been seen last online in earlier than this timestamp, will get removed. e8 g @% E% G% Q7 ?. b
$timeout = $timestamp-$timeoutseconds;/ V2 f3 @2 y% y1 q; }/ l
//connect to database2 E& V* m) ?" w8 w& g0 t
mysql_connect($server, $db_user);9 X, {& j/ S2 s& E" U# T; n, e0 a
//add the timestamp from the user to the online list
" c: V6 O/ a$ B7 }4 @0 P$ \$insert = mysql_db_query($database, "INSERT INTO useronline VALUES2 M* Y9 ^7 d/ m3 w" s
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");1 G6 J3 c( c2 k: z
if(!($insert)) {
( c0 u1 w! p K0 L' v$ |: M4 g! j! Cprint "Useronline Insert Failed > ";' A9 Z# H4 E4 ?4 {: S% D# s
}
6 f% P T7 d* q O7 b4 G//delete the peoples which haven't been online/active in the last $timeoutseconds seconds.( ~6 q, y+ }& @& |0 P& J
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");
4 p9 F& y: I9 _3 j. q& U3 K6 P2 Qif(!($delete)) {
: B |" d: s- J% z, i; N- l. J- kprint "Useronline Delete Failed > ";
1 V0 F: n# ?. U& T3 Y7 v1 M1 p}
1 m$ a1 W2 B9 |& Y; l. o//select the amount of people online, all uniques, which are online on THIS page
/ U _5 n8 R3 G9 k/ [* u* S6 g. c$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");
6 B! O; B* S5 G; j* a+ w; xif(!($result)) {
) m& o$ e$ K5 T: ~print "Useronline Select Error > ";
- u( m+ x% K* G; G; ^}5 ^. E: y! v. G" Y; k% ~3 w. H
//Count the number of rows = the number of people online
- {! c9 Z1 D% \0 x0 Z$user = mysql_num_rows($result);" @# G2 k, L5 L1 `5 D
//spit out the results
7 `) _/ Y7 {) J/ Gmysql_close();
, o# B1 }. l8 ~- Hif($user == 1) {, S) _9 ?) k- S! u, y4 V' J
print("1 user online\n");
4 g4 z* e) [6 @; b} else {
% d6 a9 v1 F# K3 E0 j" M' k$ |print("$user users online\n");
: ~& @' ^, G- h) A}; y" r8 }/ V7 X6 ]2 _! t+ ?/ W
?>. U- ~# Q! T3 U- z
) u3 B% ~% p! W8 g ^
以上代码我是在时代互联提供的免费试用主机上测试的,感觉效果还不错,性能很稳定,不信的话你也可以去试试。当然,记得指出我这个程序中的不足先哦。
$ t' I- I. S' s! k& Y3 k时代互联是行业内最早开始提供PHP 虚拟主机的公司之一,现在已经是的金牌域名注册商和中国互联网客户满意度十佳单位,产品功能已经非常完善 ,特别提一下几个特有用的功能:数据库自动备份、免费集成繁简通简装版、多域名绑定、多子网站支持、GCDN 网站加速器、镜像加速、高级访问统计系统、支持WAP、可选电信/网通/香港机房。
5 L* ` X: t2 Z' n我之前建站的时候选的是他们公司标准商务E型,因为这款机型还特别支持ASP.NET3.5(3.0/2.0/1.1)/ASP。/ v/ P1 I# y# U7 Z; Z6 w; s0 T
当然啦,这两款主机也是相当不错的。& S1 Z) j a9 t) b6 {# Z, L7 e, y* O
智强商务B型:Unix /Linux 操作系统+独立网页空间1000MB+送1000MB邮局空间+100MB MySQL数据库空间=1869元/年
. \& C& J, x, q' J( [" m标准商务B型:Unix /Linux 操作系统+独立网页空间500MB+送800MB邮局空间+60MB MySQL数据库空间=1365元/年
* o% S2 f$ \; l; d; C' G, c7 r提供一下这个公司的联系方式:请见:http://www.now.cn/vhost/ 0 c! l7 S& p J8 A
空间专线: 0756-2623871 QQ:168075865,好象还有全球免费网络电话:全球免费咨询电话 http://www.now.cn/callcenter/call.net?LineName=55
$ V1 ~+ F. Y2 i自己加QQ去问吧。 |
|