
- UID
- 455
- 帖子
- 3
- 精华
- 0
- 积分
- 8
- 金币
- 3
- 威望
- 0
- 贡献
- 0
|
刚用PHP写了一个网站在线人数的程序,请大家进来指点下!
刚用PHP写了一个网站在线人数的程序,请大家进来指点下!& K& _) ~/ M7 D% O" C
我是用PHP+MYSQL来写的,原理:网站在线人数的程序代码+后台有MYSQL数据库支持,可以直接统计出网站当前的在线人数。/ X6 P! D/ {$ n( z8 n
首先我创建MYSQL数据库表。. X" v- ]. P, I$ j# `7 z& g
CREATE TABLE tablename (
& y% y7 ]% N' `4 d* W- r" u q* vfield type(max_length) DEFAULT 'default_value' (NOT) NULL
) l; G& Y" X4 u}可以使用的SQL语句。& W7 [. n- s; ?! g1 ?3 w( H, ]
CREATE TABLE useronline (. k- ~% L7 ?5 Y' Y% D- }$ D8 d
timestamp int(15) DEFAULT '0' NOT NULL,; \ [# g5 Y. Z1 Z
ip varchar(40) NOT NULL,2 M2 L, E# ~: o; V3 k) D& c* ]: f9 S
file varchar(100) NOT NULL,
) d& `' i! P7 UPRIMARY KEY (timestamp),
0 P F$ M" E9 X& l& D: T( |KEY ip (ip),
! K# m& ^5 U* d, Y4 |KEY file (file)
! _( Z& G7 d; O6 n; K4 O);下面我们是PHP脚本,首先我定义MYSQL的信息。' V& l2 v% n0 m3 u" o8 X3 E4 V
$server = "localhost"; //你的服务器
, B' k" ^% e: |; n- U2 d$db_user = "root"; //你的mysql的用户名
* O; k' c; E) V$db_pass = "password"; //你的mysql的密码
1 f' [! ~9 U5 D* [, x& ~) N$database = "users"; //表的名字设置统计的时间(多少秒内在线人数)# }. A3 |2 j9 ]' X
$timeoutseconds = 300;取当前时间。3 m# R6 u, B9 m& u( S
$timestamp = time();上面的完整代码:
6 p* e& e6 R7 N<?php
Q( ~3 n& m7 T$server = "localhost"; //your server
3 n5 `# A; u) f8 s$ Q$db_user = "root"; //your mysql database username% H5 q+ i( w" e" D. L+ h
$db_pass = "password"; //your mysql database password if any
1 s K2 v; `# w! H& _: e$ y$database = "users"; //the db name4 G4 |" \; d6 ?7 J5 ^2 a% ~1 n( e
$timeoutseconds = 300;//timeoutseconds limit
/ p. w& G: y- w7 D//get the current time( z3 _! i5 Z' E { H
$timestamp = time();5 K6 x8 o% D4 f% U7 x& ]
//calculate the lowest timestamp allowed: w$ }% y1 H4 e. M y
$timeout = $timestamp-$timeoutseconds;
; i3 m- s, \3 E, v7 r?>连接mysql7 b7 M1 D: ?; f1 M6 V* R7 e# i$ ^9 x
mysql_connect('localhost', 'username', 'password');也允许使用变量形式。; _8 M# ^( ^& e% b ~5 k3 f
mysql_connect($server, $db_user, $db_pass);如果mysql数据库没有密码的话可以使用下面代码连接( H! m& Z* h; ~7 O+ ?
mysql_connect($server, $db_user);查询数据库的代码:
) P: G8 B) i9 y/ m) G2 K7 \mysql_db_query('database', 'query');我们只要有访客就要增加一条记录。( \- s3 z1 `1 C6 p$ R* k
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES8 x3 r) O- V) J$ T$ J( |
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
! d# x! j. O/ N6 B. t如果用户用错误信息的话,这样处理。
" S& H" }' G; fif(!($insert)) {; j3 W' V- h) ]
print "Useronline Insert Failed > ";
5 \, b% [ S# B" R}然后实现当超过设置的时间就删除该用户记录。) J& f9 u! N; W7 c# s5 u
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");同样给出删除记录出错的处理。& A$ z t- Q' |1 W2 d
if(!($delete)) {+ V) s* K; @) k" f# k4 r
print "Useronline Delete Failed > ";
: v( }/ J' X J; Z}下面我们解决数据库中不同IP的问题
" J! {3 Q" H% f7 U$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");使用* ~8 d$ s1 h8 M" E+ v c/ x7 n; u( ^' i. h
mysql_num_rows(query);来统计用户,代码如下。
! E# |& X) {9 h* l0 j$user = mysql_num_rows($result);最后关闭数据库。
& J8 H: x) _0 }mysql_close();显示在线的人数。
2 a; L8 K5 q3 G# {if($user == 1) {- Z- R+ Y% r* M; N$ w8 A8 ]
print("1 user online\n");
) ~3 i9 g* q9 C2 _* I( Q# B V' l} else {' k7 g+ A) b8 x8 N7 b4 J* l
print("$user users online\n");
* }7 Y, z2 F/ A. @( h2 T}最终把上面代码写成一个PHP文件如下。2 @2 t4 t4 z5 ?: G7 W8 ]( y
<?php
, |9 g: z, C1 m& f6 J. Y3 |//Put your basic server info here! V) t8 `7 s: |
$server = "localhost"; //normally localhost5 C& P1 I1 _' T2 _
$db_user = "root"; //your MySQL database username0 w% N h6 n8 q/ `9 T
$db_pass = "password"; //your MySQL database password' D* |6 @1 k/ g0 J: ?# F7 N
$database = "users";
5 d" n) Q6 v& t# M* s9 [$timeoutseconds = 300; //it will delete all people which haven't refreshed(so probbably are
: p5 s j- V" c$ L. k5 L// offline or inactive) in $timieoutseconds time (so it actually checks the people that are active in the last2 E$ O& z1 ^$ j6 q+ ^; k, r& a) X1 c
// $timeoutseconds seconds)
" P0 m) J/ i6 ~1 J//this is where PHP gets the time
( i# z0 h, d; w- u7 ?" B) q* \1 h' ^0 u$timestamp = time();
2 o# ^6 @- Q2 l5 {//counts the timeout, all people which have been seen last online in earlier than this timestamp, will get removed
! J. v; G$ |. y0 G* F* R, M$timeout = $timestamp-$timeoutseconds;% W" n/ N' G. G
//connect to database
3 E+ s. Y, h$ w* @7 t, p5 t3 Rmysql_connect($server, $db_user);
2 O. n6 q; H/ B7 P2 }//add the timestamp from the user to the online list& c0 A$ d6 g- N) O( k+ Z% }$ k& p
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
/ H6 Q1 U7 n; Y1 G% `('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");/ _# R+ y6 \' k
if(!($insert)) {! D& b; n8 \$ j' f( Z8 Z6 z4 z5 F
print "Useronline Insert Failed > ";$ R. W$ r* L' J! p. L2 A, w
}( ]+ r, h( O2 ?- g$ ]% a, ~& l
//delete the peoples which haven't been online/active in the last $timeoutseconds seconds.
" ?5 v8 S4 A& t9 N) |$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");$ n( g# K. `( v- n
if(!($delete)) {
$ U, |/ Q% Y4 W3 B/ S) ~3 A8 g8 _print "Useronline Delete Failed > ";
; b* N* ?- c' O' s3 }}, k# x1 X9 z0 n) V1 b, y
//select the amount of people online, all uniques, which are online on THIS page
! g4 i2 F, L h# D6 o$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");
) z# ?$ q' f) ^if(!($result)) {: H, Y$ N& \* C' k
print "Useronline Select Error > ";
9 d& [# d4 z( o' U3 Y8 P$ k}
" F4 i* e+ [* b7 j' o//Count the number of rows = the number of people online( {. v5 `$ p5 M7 O M
$user = mysql_num_rows($result);
& C, Z$ W' w8 V5 E$ J! n% ?3 p9 `//spit out the results( U8 Z! h4 ^& Q3 t" \9 F
mysql_close();
- I! _) I0 C9 ~if($user == 1) {% V5 n; W3 E6 i( S+ G8 v4 q
print("1 user online\n");
4 t3 |2 B* w: h$ D2 S$ |} else {
; p- ]- Z: \) P# W6 tprint("$user users online\n");
3 w/ }' a k. ]& |8 d5 Z5 H}( R1 _6 o- U6 [: p
?>5 `/ t" r/ L; Q3 C% [

- Z5 D, A: K) i1 I# {8 n以上代码我是在时代互联提供的免费试用主机上测试的,感觉效果还不错,性能很稳定,不信的话你也可以去试试。当然,记得指出我这个程序中的不足先哦。) u' A; V; Q" G: [6 ]. y) k. `
时代互联是行业内最早开始提供PHP 虚拟主机的公司之一,现在已经是的金牌域名注册商和中国互联网客户满意度十佳单位,产品功能已经非常完善 ,特别提一下几个特有用的功能:数据库自动备份、免费集成繁简通简装版、多域名绑定、多子网站支持、GCDN 网站加速器、镜像加速、高级访问统计系统、支持WAP、可选电信/网通/香港机房。
0 e* I4 t7 Z; F' T我之前建站的时候选的是他们公司标准商务E型,因为这款机型还特别支持ASP.NET3.5(3.0/2.0/1.1)/ASP。
. }$ V7 E3 ^ G# W a当然啦,这两款主机也是相当不错的。
/ h; o6 D% N, u* k/ D智强商务B型:Unix /Linux 操作系统+独立网页空间1000MB+送1000MB邮局空间+100MB MySQL数据库空间=1869元/年
% s9 g% I, w7 Y/ q1 E标准商务B型:Unix /Linux 操作系统+独立网页空间500MB+送800MB邮局空间+60MB MySQL数据库空间=1365元/年! l+ n- F' f4 S. N* p& C; f
提供一下这个公司的联系方式:请见:http://www.now.cn/vhost/
; }# g. _! O. `空间专线: 0756-2623871 QQ:168075865,好象还有全球免费网络电话:全球免费咨询电话 http://www.now.cn/callcenter/call.net?LineName=55% C# z z$ B5 P0 ]. @. k5 F7 [
自己加QQ去问吧。 |
|