Board logo

标题: 刚用PHP写了一个网站在线人数的程序,请大家进来指点下! [打印本页]

作者: lilcy88    时间: 2008-5-28 10:40     标题: 刚用PHP写了一个网站在线人数的程序,请大家进来指点下!

刚用PHP写了一个网站在线人数的程序,请大家进来指点下!8 @# I, d0 t3 l) c9 T
我是用PHP+MYSQL来写的,原理:网站在线人数的程序代码+后台有MYSQL数据库支持,可以直接统计出网站当前的在线人数。( @: m$ t8 I& r
首先我创建MYSQL数据库表。
4 d( j' C) w, b  J0 KCREATE TABLE tablename (0 v6 |9 h! R- l0 V3 Y5 \
field type(max_length) DEFAULT 'default_value' (NOT) NULL4 P: M7 q3 F5 j% r- R4 O, \
}可以使用的SQL语句。; D* j  W: ]; P# L/ y& v- N  q
CREATE TABLE useronline (
3 M- G8 k2 F8 }6 e, Qtimestamp int(15) DEFAULT '0' NOT NULL,9 N; U% f2 l: Q9 Z3 G: X7 R
ip varchar(40) NOT NULL," H4 \5 ?0 D% U( B; b
file varchar(100) NOT NULL,: h/ Y. S1 i4 U0 n
PRIMARY KEY (timestamp),( l* v* d2 C, \, m! E  ?
KEY ip (ip)," _6 A- K3 x) p1 J  e9 F. q
KEY file (file)
' e6 l& ^' U! V: r6 R6 j. Y);下面我们是PHP脚本,首先我定义MYSQL的信息。! Q3 d7 j8 D. }. b+ |5 @$ @
$server = "localhost"; //你的服务器
6 j9 C- m# b) f- [! }+ L$db_user = "root"; //你的mysql的用户名
0 w  R5 B7 V! ~* \: h2 z4 X$db_pass = "password"; //你的mysql的密码
3 g" b3 f' W  e" O1 h$database = "users"; //表的名字设置统计的时间(多少秒内在线人数)% \# m/ l& W8 @) c
$timeoutseconds = 300;取当前时间。0 W* K  q+ n- U& h  G) N0 r
$timestamp = time();上面的完整代码:1 q/ J9 i8 ~" ], N. v. p5 C, W+ p! M
<?php
0 d* e& T& q  Z+ R/ h$server = "localhost"; //your server8 A1 Z% l) k; f, |: O# b
$db_user = "root"; //your mysql database username
2 I" i% s5 Q+ Y% s2 m5 ^0 ?$db_pass = "password"; //your mysql database password if any4 E% J% Q, Q) O8 _# y  G6 r
$database = "users"; //the db name/ u5 _% o( y% b# @0 ~2 _
$timeoutseconds = 300;//timeoutseconds limit
6 {' U1 q; G4 b2 q. R. P//get the current time4 J% h4 P: k# i$ P) {- f, V& `( ?
$timestamp = time();- u/ h. Q( w+ d. `8 @, v  |! T& f
//calculate the lowest timestamp allowed# o& t. S; ^$ [) g0 K
$timeout = $timestamp-$timeoutseconds;
- ^6 l# d* C" z  n( g?>连接mysql/ F+ |5 p1 D% W. z: J$ W, y
mysql_connect('localhost', 'username', 'password');也允许使用变量形式。, @: c, v$ Q+ _: I$ G9 Y% @
mysql_connect($server, $db_user, $db_pass);如果mysql数据库没有密码的话可以使用下面代码连接& a3 |, N' o0 ]
mysql_connect($server, $db_user);查询数据库的代码:& N2 z, V- v' }3 l; D5 h1 P0 q/ ~9 U
mysql_db_query('database', 'query');我们只要有访客就要增加一条记录。5 E% y- W) v/ T% b5 t
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES* @4 B! h# \3 [, `, I  {- {
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");; s' B: H- \; K/ O
如果用户用错误信息的话,这样处理。
" Z* d# p6 C* l8 p- a  ~: ]if(!($insert)) {
$ c( |3 ~0 O$ t+ J6 \print "Useronline Insert Failed > ";: `* _+ K" R+ O5 n6 g: z
}然后实现当超过设置的时间就删除该用户记录。
( N) @( m- D: X- L, V6 l4 B3 f$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");同样给出删除记录出错的处理。8 H' c8 |8 y4 c6 |0 ~( N! f
if(!($delete)) {# V/ J7 Y* Y, {' ?$ a/ z  x
print "Useronline Delete Failed > ";
% |" N4 s( A+ r$ s% C" L, l}下面我们解决数据库中不同IP的问题: |, X) r0 g* [. u
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");使用3 S8 `7 o; ^& w2 F/ I1 ?6 [
mysql_num_rows(query);来统计用户,代码如下。
2 @7 a5 J2 a' U  E3 H! ?" X! d$user = mysql_num_rows($result);最后关闭数据库。
6 M  y9 g! ]1 Z7 j; d4 `9 Umysql_close();显示在线的人数。/ _8 y1 a3 b* w8 ?6 f3 s
if($user == 1) {! x7 I: D2 l( Z8 [% ]' n9 v! E9 X
print("1 user online\n");
( u2 r" U  C/ L: J. q! V5 Q} else {
2 `6 j0 z) M8 s' Xprint("$user users online\n");2 c% L* Z; J5 j
}最终把上面代码写成一个PHP文件如下。
) u1 E* p0 j9 T% u# c$ H  O+ _" l<?php
  A% y0 M3 n; A# r//Put your basic server info here
0 N6 d- S3 V) N& j& u4 W- p$server = "localhost"; //normally localhost# r- c6 j" P8 S6 U
$db_user = "root"; //your MySQL database username" a2 I$ j& E2 p, m
$db_pass = "password"; //your MySQL database password9 S* `$ k: k6 g+ M  P4 r
$database = "users";; g8 U! y' j+ a  T7 G9 ~
$timeoutseconds = 300; //it will delete all people which haven't refreshed(so probbably are
5 l0 i, [# [0 u( d// offline or inactive) in $timieoutseconds time (so it actually checks the people that are active in the last$ b6 ?# ?2 `+ u/ y
// $timeoutseconds seconds)1 v5 X3 C0 L$ _" l$ E/ S8 R
//this is where PHP gets the time
. L' |; ]' a9 i$timestamp = time();
3 m  F8 b9 P& G//counts the timeout, all people which have been seen last online in earlier than this timestamp, will get removed
; ?7 k- I; b; L$timeout = $timestamp-$timeoutseconds;
! ?) [9 }  s6 G/ a! @5 z  u//connect to database, C0 y) {2 Z, o! p; y
mysql_connect($server, $db_user);. p- s9 S; m0 e; @3 y2 L' E
//add the timestamp from the user to the online list4 X+ E" A' Q) {9 Q
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES" @8 [, H. y$ D- c! K4 ?. X( x
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
$ V3 y. t3 X2 j8 Y8 f; Oif(!($insert)) {
7 X! S$ ^% h4 Z1 i, U( \; c, a1 n0 Nprint "Useronline Insert Failed > ";
+ I) J( Y, [# K) l0 A! o}
9 [2 _$ Q0 D7 V6 F0 F//delete the peoples which haven't been online/active in the last $timeoutseconds seconds.2 a' Z$ {  f5 Y4 a$ h. i( F  [
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");, ]  k4 C4 q5 x  R$ z4 m2 b* V
if(!($delete)) {
, `  i7 V3 y+ S" b2 @print "Useronline Delete Failed > ";
4 j5 U6 G8 w2 O( o# w}. J; p3 W0 N% W* E
//select the amount of people online, all uniques, which are online on THIS page
9 |& D. q+ ~9 ~/ s$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");/ B+ g2 c: D7 |
if(!($result)) {/ G) `" H1 H: O, o
print "Useronline Select Error > ";3 P6 X4 t) G# k- h4 l0 }
}& b) _. U- r; ?  Q1 H: I- ^
//Count the number of rows = the number of people online
1 {, L2 \  b' ~5 C. \$user = mysql_num_rows($result);
7 F+ b" _: |5 v( ?. u& r//spit out the results1 J& ~/ c  F" u; Y& V
mysql_close();
2 S4 e! b' p: L7 ~2 pif($user == 1) {, z( i4 x  k! C' z! R
print("1 user online\n");
% ]% m/ I  w( ~, I' X( U# S) |; h} else {5 J" w) l* ^$ u3 j3 D0 {: ^
print("$user users online\n");2 X% i7 B8 G1 i4 q
}
: Z" W) E$ D7 o  {8 G?>- }% [) j) l0 S; f

' o: c- Z9 s6 A6 U/ o% z  s以上代码我是在时代互联提供的免费试用主机上测试的,感觉效果还不错,性能很稳定,不信的话你也可以去试试。当然,记得指出我这个程序中的不足先哦。6 f1 y: |: z% ]  V
时代互联是行业内最早开始提供PHP 虚拟主机的公司之一,现在已经是的金牌域名注册商和中国互联网客户满意度十佳单位,产品功能已经非常完善 ,特别提一下几个特有用的功能:数据库自动备份、免费集成繁简通简装版、多域名绑定、多子网站支持、GCDN 网站加速器、镜像加速、高级访问统计系统、支持WAP、可选电信/网通/香港机房。
* [$ r2 [9 M9 y* h我之前建站的时候选的是他们公司标准商务E型,因为这款机型还特别支持ASP.NET3.5(3.0/2.0/1.1)/ASP。1 I9 ]' s, S' Y( d" A5 j5 @
当然啦,这两款主机也是相当不错的。0 u7 Y: f& i+ ]6 g7 }. U
智强商务B型:Unix /Linux 操作系统+独立网页空间1000MB+送1000MB邮局空间+100MB MySQL数据库空间=1869元/年. a7 r. ~  g/ y# K3 A  Z
标准商务B型:Unix /Linux 操作系统+独立网页空间500MB+送800MB邮局空间+60MB MySQL数据库空间=1365元/年
3 ]  I8 z( `* u8 i提供一下这个公司的联系方式:请见:http://www.now.cn/vhost/ $ G5 s! M+ ], E* m- j) @
空间专线: 0756-2623871  QQ:168075865,好象还有全球免费网络电话:全球免费咨询电话 http://www.now.cn/callcenter/call.net?LineName=55
" n5 P5 R: l& l1 ^& f& R2 X自己加QQ去问吧。




欢迎光临 捌玖网络工作室 (http://www.89w.org/) Powered by Discuz! 7.2