返回列表 发帖

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

刚用PHP写了一个网站在线人数的程序,请大家进来指点下!
. ]# u$ I7 o1 P; m- N' b/ _8 K& B我是用PHP+MYSQL来写的,原理:网站在线人数的程序代码+后台有MYSQL数据库支持,可以直接统计出网站当前的在线人数。
+ y$ w% @: F5 a5 D首先我创建MYSQL数据库表。
, `3 E, d- c7 c0 W) hCREATE TABLE tablename (
3 ~! _0 s& T8 i7 l+ I% u8 M0 rfield type(max_length) DEFAULT 'default_value' (NOT) NULL& ]+ p0 ?7 s% n' J4 W
}可以使用的SQL语句。, b& E9 p- c7 _' K8 ]7 `" Q6 l1 d
CREATE TABLE useronline (
2 c# S) M1 R0 z* |timestamp int(15) DEFAULT '0' NOT NULL,
9 E6 o. I( H0 Y' I3 e$ u( `ip varchar(40) NOT NULL,' A. ~/ n- C  [1 }
file varchar(100) NOT NULL,
& L  Y* ~" t) q. JPRIMARY KEY (timestamp),/ J# \+ R+ G: m# U9 w
KEY ip (ip),
2 I& @( G5 V- z# t, ?5 }KEY file (file)8 M; b7 w  G8 t0 G
);下面我们是PHP脚本,首先我定义MYSQL的信息。; T- A2 e% b) w- K1 A% a
$server = "localhost"; //你的服务器: X: B5 t0 ~/ O
$db_user = "root"; //你的mysql的用户名: a0 O. J! N# c8 O3 Z) a- }
$db_pass = "password"; //你的mysql的密码
) I2 Y8 A/ _- L$database = "users"; //表的名字设置统计的时间(多少秒内在线人数)5 [4 G* g5 a2 m& U; e
$timeoutseconds = 300;取当前时间。: X; t6 r, _1 b( a4 |$ ?
$timestamp = time();上面的完整代码:7 b2 _& P4 f! U  X" z/ N1 `
<?php9 ?5 v) M$ w8 O
$server = "localhost"; //your server
; |1 @8 s9 L) o# b' \. `/ w$db_user = "root"; //your mysql database username% y  M9 a2 L" V! _2 P' A; j" r
$db_pass = "password"; //your mysql database password if any
9 q0 l% Y, z' b; Z5 V$database = "users"; //the db name9 n4 N8 R$ e& s! Y
$timeoutseconds = 300;//timeoutseconds limit
# ~$ O- ]/ S/ v( G! j% b//get the current time
  {0 C+ c6 r0 v) b$ X' r, W! q$timestamp = time();
, S8 [3 Z; d9 Q2 Q% O//calculate the lowest timestamp allowed
1 y3 V0 d. D9 M; a$timeout = $timestamp-$timeoutseconds;( [3 \8 R9 i0 M$ E
?>连接mysql, j/ c9 L0 [' c" |9 c: G
mysql_connect('localhost', 'username', 'password');也允许使用变量形式。
8 l8 n9 L8 S0 ]mysql_connect($server, $db_user, $db_pass);如果mysql数据库没有密码的话可以使用下面代码连接
: i$ }2 L1 C/ mmysql_connect($server, $db_user);查询数据库的代码:! o1 r% O7 K4 B9 T4 M) v
mysql_db_query('database', 'query');我们只要有访客就要增加一条记录。: Q8 R1 D1 ?! B
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
! X1 F, n. q% F4 G+ C('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
+ k  _  ?4 y  D# `! D  X8 @如果用户用错误信息的话,这样处理。) R! Z3 d! Y6 f' X
if(!($insert)) {
! W. A& t9 M/ ]0 Q/ tprint "Useronline Insert Failed > ";, Y5 D7 F7 |# C9 ~
}然后实现当超过设置的时间就删除该用户记录。
7 ?7 R7 e: R6 h3 s. `$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");同样给出删除记录出错的处理。1 l# u; }* Z  {
if(!($delete)) {
+ ?3 e. M/ m$ b) b. i" m$ zprint "Useronline Delete Failed > ";
( z! g( }( b/ e2 i8 s( h4 J}下面我们解决数据库中不同IP的问题! L! I3 U# j9 w7 e8 O
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");使用
- y* T. p" q, N. K  Smysql_num_rows(query);来统计用户,代码如下。
1 T  K$ ^! U' e$ b) [' J( S. E. h$user = mysql_num_rows($result);最后关闭数据库。
1 N& W% p* P3 Smysql_close();显示在线的人数。. Z' v9 @$ V& T1 f+ _5 f
if($user == 1) {
& B# m) N+ m0 C( Zprint("1 user online\n");( s5 H/ @" \# N" k  h6 B% W
} else {" C+ {8 h" r$ O
print("$user users online\n");
( l+ y) z& @  ^}最终把上面代码写成一个PHP文件如下。4 o7 Y9 X9 S. z
<?php
+ f1 s& G/ H8 t+ g6 f//Put your basic server info here
) [( `- F" L  {  O$server = "localhost"; //normally localhost
5 `/ F0 J# Z6 s6 _$db_user = "root"; //your MySQL database username
+ T8 [3 g9 X' W+ J& V7 J+ G$db_pass = "password"; //your MySQL database password
% b9 a9 O& d; G$database = "users";7 p8 ~: c7 @% l! }( Z! ~
$timeoutseconds = 300; //it will delete all people which haven't refreshed(so probbably are
3 i2 n0 w5 R2 M7 X// offline or inactive) in $timieoutseconds time (so it actually checks the people that are active in the last: Z/ B* B+ T0 g1 O" ^3 Z; y% w
// $timeoutseconds seconds)# u+ u! Y# D# v# l! c0 }
//this is where PHP gets the time2 e1 P$ W2 V0 j& g5 v9 l& E
$timestamp = time();
/ C0 k5 x% f9 O8 p$ C//counts the timeout, all people which have been seen last online in earlier than this timestamp, will get removed; v, }  o- {2 o: N7 g9 f7 b0 a9 b! i- {
$timeout = $timestamp-$timeoutseconds;: _6 O' n& }9 n2 h6 H
//connect to database
; P. x: \( Q: C6 h, m  c# ~mysql_connect($server, $db_user);% I9 o0 m: A& G: i
//add the timestamp from the user to the online list/ Z! f2 S5 G+ I% V4 Y& w7 Y" O4 g! K
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES% N9 b3 L  x  c9 F: g) n
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");; l/ H9 ]- e1 q, F$ z2 Y
if(!($insert)) {
+ r6 t" I  i" X" Iprint "Useronline Insert Failed > ";
( R. X' Q# X  z8 z}9 k; w( E- `( v
//delete the peoples which haven't been online/active in the last $timeoutseconds seconds.
3 u; q* d4 }4 b& L, r% i$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");
. ~$ T$ @# _( b" _& p5 eif(!($delete)) {
! f3 ~& j9 g" I$ nprint "Useronline Delete Failed > ";: c/ Y! x) K; Z( }' N0 D
}- |  Y7 O: E: ~7 I" r! W5 K6 f
//select the amount of people online, all uniques, which are online on THIS page
% I' d6 D& ]' u2 \1 U9 J/ ]$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");
, i. X$ [- ^8 z5 w! wif(!($result)) {
8 R9 M6 f, e' F& }print "Useronline Select Error > ";
& V, t0 ~  @8 S% a8 B}/ A& a0 |0 n" `
//Count the number of rows = the number of people online3 o+ A6 H$ X- ?6 J
$user = mysql_num_rows($result);. g" n3 X: `6 ]4 s2 k/ l; K& T# ^
//spit out the results- n0 C7 Q8 N0 }8 D* M2 y
mysql_close();' X. J* I! G- H5 X
if($user == 1) {
6 t/ i' K2 m. \1 d8 x! D( oprint("1 user online\n");: ^  M7 h6 O; l' B* x' u" ~
} else {
! M* o5 [; @7 t8 I; Eprint("$user users online\n");/ l1 T  S- }" s5 f4 M
}
0 G7 c. q" L2 d4 W8 m?>8 g, Z. g, b* J2 o
% O* Y; ~1 g5 P) D
以上代码我是在时代互联提供的免费试用主机上测试的,感觉效果还不错,性能很稳定,不信的话你也可以去试试。当然,记得指出我这个程序中的不足先哦。
& n5 J3 |2 B- H时代互联是行业内最早开始提供PHP 虚拟主机的公司之一,现在已经是的金牌域名注册商和中国互联网客户满意度十佳单位,产品功能已经非常完善 ,特别提一下几个特有用的功能:数据库自动备份、免费集成繁简通简装版、多域名绑定、多子网站支持、GCDN 网站加速器、镜像加速、高级访问统计系统、支持WAP、可选电信/网通/香港机房。
! I3 Z, L3 c" p" G2 w" l+ w我之前建站的时候选的是他们公司标准商务E型,因为这款机型还特别支持ASP.NET3.5(3.0/2.0/1.1)/ASP。
- s5 V' b% w9 k2 [5 _. n当然啦,这两款主机也是相当不错的。6 {7 a. ~1 l: w9 E3 M3 j+ v
智强商务B型:Unix /Linux 操作系统+独立网页空间1000MB+送1000MB邮局空间+100MB MySQL数据库空间=1869元/年
6 c0 T+ C$ \1 W7 u2 L% x, P标准商务B型:Unix /Linux 操作系统+独立网页空间500MB+送800MB邮局空间+60MB MySQL数据库空间=1365元/年
" u# C5 Z) P- C' }0 e& J; `提供一下这个公司的联系方式:请见:http://www.now.cn/vhost/ * [- ?& W, v6 c7 `. g6 y
空间专线: 0756-2623871  QQ:168075865,好象还有全球免费网络电话:全球免费咨询电话 http://www.now.cn/callcenter/call.net?LineName=55
7 @; m% i) f4 L9 t+ b7 I! K9 y  [自己加QQ去问吧。

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