返回列表 发帖

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

刚用PHP写了一个网站在线人数的程序,请大家进来指点下!7 L' b+ ~! j$ o1 o7 p
我是用PHP+MYSQL来写的,原理:网站在线人数的程序代码+后台有MYSQL数据库支持,可以直接统计出网站当前的在线人数。( U" _& ~1 J* o
首先我创建MYSQL数据库表。; Q& {* O6 f1 ^
CREATE TABLE tablename (# q) [/ C- s8 O  k) r
field type(max_length) DEFAULT 'default_value' (NOT) NULL
0 l( D! p; [  r* y7 w}可以使用的SQL语句。* q  L7 [9 @7 x. X
CREATE TABLE useronline (" ?- y- A: h/ U: b) I! z
timestamp int(15) DEFAULT '0' NOT NULL,( w9 X2 G; V9 y/ |1 R
ip varchar(40) NOT NULL,! y7 U2 u6 B9 V9 x- u8 V+ ^
file varchar(100) NOT NULL,7 ^/ \3 y0 {) Z7 T% p/ A
PRIMARY KEY (timestamp),
. i% F' E3 i& _2 J1 {KEY ip (ip),
2 c" B0 B+ H1 L& Z% CKEY file (file)
* z8 k8 S# ?5 |) j& z# D);下面我们是PHP脚本,首先我定义MYSQL的信息。8 u7 S; R/ U4 i/ z: q
$server = "localhost"; //你的服务器1 h" d/ u2 [5 x" b/ d. y7 y+ n
$db_user = "root"; //你的mysql的用户名' O" \8 j+ R4 H# D
$db_pass = "password"; //你的mysql的密码
- }% A  k  {# n) }) {2 K$database = "users"; //表的名字设置统计的时间(多少秒内在线人数)
0 B" d% `- F( F/ c5 Y# `$timeoutseconds = 300;取当前时间。2 ?4 O/ U( f. X0 G' L0 M
$timestamp = time();上面的完整代码:
3 T- H; ^" n8 y* W<?php
3 k# b$ Y: H) i$server = "localhost"; //your server
. O8 e4 r9 _6 n4 _$db_user = "root"; //your mysql database username2 M' X1 ?& U7 w! G/ D3 X
$db_pass = "password"; //your mysql database password if any
1 U1 K9 v) z% {. A4 H$database = "users"; //the db name# H8 `; ~* J$ }% D0 E
$timeoutseconds = 300;//timeoutseconds limit7 j0 a5 j% _- C& d- K& R' S8 E
//get the current time% |# E8 x5 J4 U2 ?4 b) U2 t
$timestamp = time();# K, U9 k" U  b4 L9 k
//calculate the lowest timestamp allowed
, D0 B. c( i: y# b* g8 O! V' a- j$timeout = $timestamp-$timeoutseconds;
) E8 s" c; I1 h" p: r?>连接mysql4 ^' f* y1 |& l& i$ S
mysql_connect('localhost', 'username', 'password');也允许使用变量形式。
1 W6 O0 o2 d6 Q, e- _/ Gmysql_connect($server, $db_user, $db_pass);如果mysql数据库没有密码的话可以使用下面代码连接
3 G6 @7 L0 p' q1 h# P5 R' G/ Emysql_connect($server, $db_user);查询数据库的代码:" \0 U3 x$ w5 @0 s! C
mysql_db_query('database', 'query');我们只要有访客就要增加一条记录。7 M+ ]! Q3 d; {4 _
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES' k* b- p( j! o
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");/ q5 K: {2 k1 c
如果用户用错误信息的话,这样处理。9 m2 |; i" M5 O3 A* G. g
if(!($insert)) {8 C6 R7 d) b8 b: E8 W3 K/ a
print "Useronline Insert Failed > ";/ o6 J6 c% K* P, r- I" g! R
}然后实现当超过设置的时间就删除该用户记录。  ?3 q" L) }% D% C/ f  r
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");同样给出删除记录出错的处理。) R# ^5 f0 A, m7 ]
if(!($delete)) {$ p" [3 i- Z+ m
print "Useronline Delete Failed > ";
4 y1 v4 q4 ~% M  D% z  `: d7 ?}下面我们解决数据库中不同IP的问题
" d/ u1 O& y/ v( h# a6 }, w$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");使用# O' o1 v5 m+ q0 |4 v# g
mysql_num_rows(query);来统计用户,代码如下。' a* z+ z) y* F0 `/ P# ^
$user = mysql_num_rows($result);最后关闭数据库。- V" [1 k4 {- B# r( c; W
mysql_close();显示在线的人数。7 \8 P6 @7 ?; H+ `; d% Y
if($user == 1) {
- K' l/ Z; I! L% j. r) W( D  Lprint("1 user online\n");$ b( U- m5 }; V- q7 X4 Q, f+ e( p* O
} else {
. y9 h, c! u4 ~4 f2 Iprint("$user users online\n");
9 F+ W8 u- b3 r! K8 \, c8 }}最终把上面代码写成一个PHP文件如下。7 {: T  P$ B0 d7 O; r
<?php
1 l( p( E8 c3 W: C7 [. Z9 ~//Put your basic server info here: w+ ?& W# Y2 o8 I3 l, K3 Z
$server = "localhost"; //normally localhost
+ ]- g+ D5 i- ^. g$db_user = "root"; //your MySQL database username
2 X# ^3 J& o' O8 @$db_pass = "password"; //your MySQL database password
7 a5 d/ ^3 L! w$database = "users";
4 e( S8 ?% T3 C& V' p4 o8 ?& a$timeoutseconds = 300; //it will delete all people which haven't refreshed(so probbably are
1 [6 }# ^' [3 S- Q6 U! j// offline or inactive) in $timieoutseconds time (so it actually checks the people that are active in the last
) t+ N5 w0 b6 {( U$ f  u0 b0 x0 ^: F// $timeoutseconds seconds)+ I7 r" x% w6 u; }
//this is where PHP gets the time
) j9 {* b( B% f1 M/ v$timestamp = time();' a8 q0 e& {) K7 Y4 }$ K
//counts the timeout, all people which have been seen last online in earlier than this timestamp, will get removed5 u) T1 Q+ T0 o+ s/ Q* z0 c
$timeout = $timestamp-$timeoutseconds;
6 I7 P# c( P( {3 \; y3 r+ K//connect to database" D+ v+ p% g9 s1 E# o2 i, m
mysql_connect($server, $db_user);
& G' ~/ {: l  L& q) ~//add the timestamp from the user to the online list4 h5 C$ Q5 P5 {. u
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
' |8 o1 c( l  b0 o$ x6 U, c('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");  ]5 H# Z  \  B* h& p
if(!($insert)) {1 f2 M( S) G( w7 v5 S' L
print "Useronline Insert Failed > ";
/ t: K) G: y, g" O4 M* d3 l9 I}
" ?3 c0 F) A5 n& [7 V# N//delete the peoples which haven't been online/active in the last $timeoutseconds seconds.
/ V7 d4 ^1 ~; {1 ?& u$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");/ G/ C6 `# B- M$ L
if(!($delete)) {$ y# ]1 K4 I  K0 |# M( N/ O- f
print "Useronline Delete Failed > ";# {$ U7 f9 {# q. h0 T
}: ]2 u: q* N. a$ s1 [
//select the amount of people online, all uniques, which are online on THIS page( i  r8 U0 v! s- }. t& N
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");
7 Q$ y* e0 N# g- vif(!($result)) {( o1 M  o) O8 u1 U& c$ A
print "Useronline Select Error > ";
( E& D( g& C4 p# {: `}
2 c( g6 y  H& A4 h' u% u//Count the number of rows = the number of people online! \, A( g$ E9 g2 c4 W: n) f* j
$user = mysql_num_rows($result);! s3 x- k5 m" v1 ^& t5 w3 }
//spit out the results7 k' l4 u: f7 Y5 N# h0 k
mysql_close();1 s8 s! M. ~" I; [
if($user == 1) {+ }7 h1 v4 Z5 B3 ~
print("1 user online\n");
) E7 d* K; |" a  y, \7 q} else {5 t& J4 ?5 B. S8 Y  G; C8 k! M9 W. L
print("$user users online\n");
* {  d0 F5 S/ I; ]: v}
$ W/ N" [" B1 o. L. U: l# k?>5 V* k) f' V: W: k# q9 d/ A
$ i1 w( a( e- e8 f
以上代码我是在时代互联提供的免费试用主机上测试的,感觉效果还不错,性能很稳定,不信的话你也可以去试试。当然,记得指出我这个程序中的不足先哦。, h9 H' Y) X  c" }/ d' C: I
时代互联是行业内最早开始提供PHP 虚拟主机的公司之一,现在已经是的金牌域名注册商和中国互联网客户满意度十佳单位,产品功能已经非常完善 ,特别提一下几个特有用的功能:数据库自动备份、免费集成繁简通简装版、多域名绑定、多子网站支持、GCDN 网站加速器、镜像加速、高级访问统计系统、支持WAP、可选电信/网通/香港机房。% k/ u: B& C" B' H$ p9 z2 d
我之前建站的时候选的是他们公司标准商务E型,因为这款机型还特别支持ASP.NET3.5(3.0/2.0/1.1)/ASP。
# J4 R9 ]- g* ]7 }' T9 K7 u5 Z当然啦,这两款主机也是相当不错的。* Y) S' }) V+ C2 C; w: }( r
智强商务B型:Unix /Linux 操作系统+独立网页空间1000MB+送1000MB邮局空间+100MB MySQL数据库空间=1869元/年
6 B) C! L- T0 P标准商务B型:Unix /Linux 操作系统+独立网页空间500MB+送800MB邮局空间+60MB MySQL数据库空间=1365元/年
( w4 a  ]' L) O提供一下这个公司的联系方式:请见:http://www.now.cn/vhost/
. ]8 t% D1 @* R空间专线: 0756-2623871  QQ:168075865,好象还有全球免费网络电话:全球免费咨询电话 http://www.now.cn/callcenter/call.net?LineName=550 _+ a2 J% m( m9 u8 L. j
自己加QQ去问吧。

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