返回列表 发帖

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

刚用PHP写了一个网站在线人数的程序,请大家进来指点下!
0 I0 h  _9 @4 O* k' D我是用PHP+MYSQL来写的,原理:网站在线人数的程序代码+后台有MYSQL数据库支持,可以直接统计出网站当前的在线人数。4 P- z0 a! y1 M+ i: }
首先我创建MYSQL数据库表。
4 Q! G* W- B- [, X3 t7 e# Z, lCREATE TABLE tablename (- p. Q% R; {5 n6 g3 N
field type(max_length) DEFAULT 'default_value' (NOT) NULL" M4 M& y" V+ i% n
}可以使用的SQL语句。+ `2 U+ H( U) u4 c2 |
CREATE TABLE useronline () {! a" `" C7 B+ g
timestamp int(15) DEFAULT '0' NOT NULL,
( e. Y8 p8 P" J0 q. x# lip varchar(40) NOT NULL,
2 ^3 H- v6 P7 J# mfile varchar(100) NOT NULL,
( U( o/ G2 f) ePRIMARY KEY (timestamp),7 I3 M( m: P, T9 H! k  I
KEY ip (ip),  u; K8 ?+ C; T) ?
KEY file (file)" o# m3 y' I3 {
);下面我们是PHP脚本,首先我定义MYSQL的信息。/ T6 @4 e' J  q9 L
$server = "localhost"; //你的服务器$ r6 p+ c+ y0 \
$db_user = "root"; //你的mysql的用户名
. T- Y- W& e) j- {0 E& L  }$db_pass = "password"; //你的mysql的密码$ `0 y; a( U1 N$ j
$database = "users"; //表的名字设置统计的时间(多少秒内在线人数)% W! K* m; k1 `& `
$timeoutseconds = 300;取当前时间。
; w8 r# l: C8 K7 q+ Q" w$timestamp = time();上面的完整代码:
* I4 v# D" f0 @1 a# @" H& p5 h<?php  _7 r3 i1 D/ z2 m9 g2 H4 S
$server = "localhost"; //your server6 m  U& x- E# r. W
$db_user = "root"; //your mysql database username
4 f6 A& X& y9 C+ R0 Z% l. d9 V$db_pass = "password"; //your mysql database password if any
: f( `& ]& W5 k5 Z$ ~$database = "users"; //the db name
8 |( T1 D) Y7 ?9 E4 [# k+ t$timeoutseconds = 300;//timeoutseconds limit
4 ?" T# L0 u3 U3 c//get the current time/ b) F) Q/ B/ Q6 G/ w: j1 g
$timestamp = time();3 w! ^6 w( u$ g0 e' h
//calculate the lowest timestamp allowed7 [7 h; M  H5 p) i5 Z
$timeout = $timestamp-$timeoutseconds;
. _  Z. p, j# Z/ z, g: s?>连接mysql
1 L  }4 Q5 c. i. F; w9 k  Bmysql_connect('localhost', 'username', 'password');也允许使用变量形式。% W- p4 K* {: e- M; r2 t- G
mysql_connect($server, $db_user, $db_pass);如果mysql数据库没有密码的话可以使用下面代码连接
* G' V" f' G% g$ Nmysql_connect($server, $db_user);查询数据库的代码:
2 C5 B" N# E3 c* l4 Y" ^6 fmysql_db_query('database', 'query');我们只要有访客就要增加一条记录。2 X8 ?3 j# h* s. t0 P/ z
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES+ O0 _' j# N& S" m4 K" S1 u3 v
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");. a6 f. }# J6 P0 N  M
如果用户用错误信息的话,这样处理。
! l6 D2 I: }/ b6 T4 H6 I0 @- Wif(!($insert)) {
! f2 M' G8 S% o9 N. _  G( B6 P% ~5 Zprint "Useronline Insert Failed > ";
. s! {, E  X7 i" U% ]}然后实现当超过设置的时间就删除该用户记录。
7 W  V& O" T+ E& O5 j; x8 {$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");同样给出删除记录出错的处理。5 {: {. i% r& T' \
if(!($delete)) {
2 V% U0 f8 c, `( ]* o7 `print "Useronline Delete Failed > ";
& b* x) B5 A( N( Y  P5 u}下面我们解决数据库中不同IP的问题* s5 {* n- z$ K
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");使用
4 J% w. }, w! b$ O) t* xmysql_num_rows(query);来统计用户,代码如下。8 J. p5 b: j2 @! Y7 j, V
$user = mysql_num_rows($result);最后关闭数据库。3 }5 @9 R) o" q6 U4 G& g+ p
mysql_close();显示在线的人数。! P: |+ s+ X% ?4 U: H$ d
if($user == 1) {, i9 s' q% x* ?/ D" E$ ~
print("1 user online\n");, ~2 \1 q0 k. Q9 s
} else {
7 s* s$ _7 d* Fprint("$user users online\n");# X; O; Z  [: M2 v
}最终把上面代码写成一个PHP文件如下。; O) V1 J7 ?$ p  M) N
<?php5 ^3 U- _0 A* G" F& r) Y
//Put your basic server info here/ |1 B8 |1 r4 L& \$ k
$server = "localhost"; //normally localhost
9 U  w; m0 x5 f9 e$db_user = "root"; //your MySQL database username
+ R$ {' G8 H4 @4 V% f$db_pass = "password"; //your MySQL database password
: c+ d. c. E4 J2 D& g5 e0 l$database = "users";3 G" e& q9 p! \* ]  G
$timeoutseconds = 300; //it will delete all people which haven't refreshed(so probbably are
: I7 n9 n$ S) N6 R2 P5 s& `// offline or inactive) in $timieoutseconds time (so it actually checks the people that are active in the last
3 X; h% [. ?- I// $timeoutseconds seconds)
% Y9 _$ }/ z9 N" _4 v//this is where PHP gets the time
- o$ v' F) A+ E$ @% u& e) a. Z/ x$timestamp = time();. o4 f5 S9 ]" A# u
//counts the timeout, all people which have been seen last online in earlier than this timestamp, will get removed. u( v. y$ q0 z: E7 E7 `
$timeout = $timestamp-$timeoutseconds;. o; f; P4 @- Y% S3 @: G7 a) b1 q
//connect to database. G; O. U9 x* P' p; j
mysql_connect($server, $db_user);
7 h0 o+ a7 g5 n, [: |//add the timestamp from the user to the online list
( b# k( @% R- ~2 e; d/ L$insert = mysql_db_query($database, "INSERT INTO useronline VALUES. S; f# Y: h1 @
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
4 k: r" v3 Q  \; m8 u7 ]if(!($insert)) {
; v/ X" f2 `+ P* I, h1 @print "Useronline Insert Failed > ";
& K/ c: d) ]; W+ i! W7 c. {& X}: R1 S( h' F; Z' D% @3 J
//delete the peoples which haven't been online/active in the last $timeoutseconds seconds.' f2 Y- I& }& Q. p7 O: i
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");$ Z% h$ U3 M  ]& B+ j4 _/ ]0 y% t
if(!($delete)) {4 ?5 t- }* }0 ^0 {, s
print "Useronline Delete Failed > ";$ T# T: H' T3 H+ o# s
}, M8 o' ~/ D, m3 l- r4 N: Q
//select the amount of people online, all uniques, which are online on THIS page
% U  t; [5 K: @* ~) H$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");
. c$ m0 ~2 {- zif(!($result)) {& f- x! U) T1 O, K9 _$ G
print "Useronline Select Error > ";
" Z: I6 v+ E; ~" ~3 t6 b0 V}
* Z% T- I- j8 t1 c# s2 u//Count the number of rows = the number of people online
. A9 f; l) z+ S6 w& S$user = mysql_num_rows($result);5 i) h& Z; \7 B1 M+ O
//spit out the results
' |. o" e2 I' T0 d$ v5 t0 C) B/ lmysql_close();
; m  m& U  Y+ T5 A/ R& Zif($user == 1) {
. b: E1 y0 a# q. Hprint("1 user online\n");
0 p4 x# `! l) f; E} else {
$ ]% o  M( X  R  ?8 oprint("$user users online\n");
, q* [1 ?, W  p7 V}
2 H' a# w9 v( x, y' \?>
, e* X2 T( `, X% }; z$ J: {
: L& ~# k: C6 ?8 Q以上代码我是在时代互联提供的免费试用主机上测试的,感觉效果还不错,性能很稳定,不信的话你也可以去试试。当然,记得指出我这个程序中的不足先哦。/ G- N( @! z/ L: x% ^: ~+ e& b4 F' K6 F
时代互联是行业内最早开始提供PHP 虚拟主机的公司之一,现在已经是的金牌域名注册商和中国互联网客户满意度十佳单位,产品功能已经非常完善 ,特别提一下几个特有用的功能:数据库自动备份、免费集成繁简通简装版、多域名绑定、多子网站支持、GCDN 网站加速器、镜像加速、高级访问统计系统、支持WAP、可选电信/网通/香港机房。" }  y& K( S* r" E) E
我之前建站的时候选的是他们公司标准商务E型,因为这款机型还特别支持ASP.NET3.5(3.0/2.0/1.1)/ASP。: `6 T, S' [5 a! E
当然啦,这两款主机也是相当不错的。
% |, \% S* h) v* U& t$ ^智强商务B型:Unix /Linux 操作系统+独立网页空间1000MB+送1000MB邮局空间+100MB MySQL数据库空间=1869元/年4 E7 J0 ?* I6 U4 n" p
标准商务B型:Unix /Linux 操作系统+独立网页空间500MB+送800MB邮局空间+60MB MySQL数据库空间=1365元/年' z+ m' O3 W2 S) N1 N
提供一下这个公司的联系方式:请见:http://www.now.cn/vhost/ : E: d! O! p5 y6 p8 \% t
空间专线: 0756-2623871  QQ:168075865,好象还有全球免费网络电话:全球免费咨询电话 http://www.now.cn/callcenter/call.net?LineName=551 Z( l2 }6 k- U" z# U1 V7 i3 N/ Z
自己加QQ去问吧。

返回列表
【捌玖网络】已经运行: