获得本站免费赞助空间请点这里
返回列表 发帖

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

刚用PHP写了一个网站在线人数的程序,请大家进来指点下!8 h( E7 C) o, ^9 `4 [  ^7 Z' f
我是用PHP+MYSQL来写的,原理:网站在线人数的程序代码+后台有MYSQL数据库支持,可以直接统计出网站当前的在线人数。2 {" T5 g1 H2 d: Z% E
首先我创建MYSQL数据库表。7 `$ r/ b& h* E# s
CREATE TABLE tablename (
/ X) Y+ `3 O8 v  h6 ]( Efield type(max_length) DEFAULT 'default_value' (NOT) NULL- v( ]: ^+ u( O- o8 D
}可以使用的SQL语句。$ n% C, H; u: H; w6 e$ _
CREATE TABLE useronline (
9 |) v0 _* `1 `, y" Atimestamp int(15) DEFAULT '0' NOT NULL,
  Q' ]$ I6 Z1 V% Vip varchar(40) NOT NULL,
4 f8 s( O" R" e) _& N4 tfile varchar(100) NOT NULL,) ?& a5 T. C# P6 O% W
PRIMARY KEY (timestamp),
' D+ H4 r7 t/ ^& r1 j5 E8 xKEY ip (ip),
9 m9 N0 r* ~  @# TKEY file (file)
7 e, B: c2 N4 Y9 L);下面我们是PHP脚本,首先我定义MYSQL的信息。& D8 n* ?/ ~0 `( K. L
$server = "localhost"; //你的服务器
! ^, V2 L3 H  g! c$db_user = "root"; //你的mysql的用户名7 j" T% ]/ d- B# c* U2 E3 d
$db_pass = "password"; //你的mysql的密码. {$ u7 g8 N, p  j& g4 R
$database = "users"; //表的名字设置统计的时间(多少秒内在线人数)
9 ~& R( _( D7 Z% O/ M2 `$timeoutseconds = 300;取当前时间。
. J9 a0 z, X3 }2 i: v6 p$timestamp = time();上面的完整代码:
0 [- l( r9 y2 F<?php- }6 I. R% B, k  H
$server = "localhost"; //your server
5 i! X( N1 q. M' @1 m1 F7 V$db_user = "root"; //your mysql database username
- B9 h- X+ e. K% Q* o# |. j. h/ M$db_pass = "password"; //your mysql database password if any
" I& U2 s) y: z7 u$database = "users"; //the db name! R! b; j2 R3 Q4 K" Y
$timeoutseconds = 300;//timeoutseconds limit
; B  b4 z9 K* q//get the current time: W' U' K2 }/ C. u
$timestamp = time();2 s; E/ j5 U# T6 B
//calculate the lowest timestamp allowed1 d/ q0 I/ I0 n2 N" G
$timeout = $timestamp-$timeoutseconds;
, y% `7 v, D" w?>连接mysql3 ]" W1 P' k: v
mysql_connect('localhost', 'username', 'password');也允许使用变量形式。
/ C$ h5 G; b! Z/ T. V, ?mysql_connect($server, $db_user, $db_pass);如果mysql数据库没有密码的话可以使用下面代码连接# v; u; A; [+ h: f, @. z
mysql_connect($server, $db_user);查询数据库的代码:- c, k0 V7 p& x& ?: y! f( C8 ~
mysql_db_query('database', 'query');我们只要有访客就要增加一条记录。
3 i* i2 o' \: ^. r9 n5 V" q5 A$insert = mysql_db_query($database, "INSERT INTO useronline VALUES* Y1 n7 {3 P! ?# b- ^. ~
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
" a9 ?6 U! m8 U- z' _如果用户用错误信息的话,这样处理。2 y6 a. l! e2 f) a
if(!($insert)) {
; ~7 l% U5 W& E6 F$ iprint "Useronline Insert Failed > ";
% b: M( z3 V2 U1 ], s9 m$ K7 b$ H}然后实现当超过设置的时间就删除该用户记录。
# W& N7 Y# {$ K* E- k$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");同样给出删除记录出错的处理。
. W# T+ \) L( b) i( Oif(!($delete)) {
. R% ^1 a( G% Y; s* Uprint "Useronline Delete Failed > ";
( d" V- ?* i: J3 r# Y}下面我们解决数据库中不同IP的问题
- C/ R7 u  m1 j' W% }$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");使用
6 u+ D3 o. w5 Mmysql_num_rows(query);来统计用户,代码如下。" t4 e% j$ U; b1 b+ Z
$user = mysql_num_rows($result);最后关闭数据库。) y6 P7 T" b' u
mysql_close();显示在线的人数。! U& _" ^4 O2 y" s8 D
if($user == 1) {
0 r+ A" n" X* U& `' `/ w( G+ |" _print("1 user online\n");3 A% h  M/ P1 Y" z" f+ y' j2 j
} else {
% q$ v; @* c$ k/ C" xprint("$user users online\n");6 X, Y4 n& H/ A' q% W
}最终把上面代码写成一个PHP文件如下。
5 |' M+ x8 O# H9 ~<?php4 A$ o& Y# B+ |. l9 s
//Put your basic server info here
- j6 [) o$ P7 ~9 Q: J8 N$server = "localhost"; //normally localhost8 `  |- M3 _9 U) r8 G. ], J
$db_user = "root"; //your MySQL database username
: X3 [& n4 O( z! I7 [  w2 l; j$db_pass = "password"; //your MySQL database password
& n) J# ]8 h' e* Q  F" v7 k$database = "users";
' o2 e  h& R5 @+ n" Q* [$timeoutseconds = 300; //it will delete all people which haven't refreshed(so probbably are- `9 \; n) [! h# I" q+ [
// offline or inactive) in $timieoutseconds time (so it actually checks the people that are active in the last; H7 t) N' \# Q9 q3 i: X# F
// $timeoutseconds seconds)
5 t+ p# K  K; u% h' G//this is where PHP gets the time( D% {" C4 _/ n2 J  q; g) f
$timestamp = time();& _3 T) n( E$ q- X1 C) t1 w0 h
//counts the timeout, all people which have been seen last online in earlier than this timestamp, will get removed7 D9 Y( p( c6 H, B% ]+ q. b9 U
$timeout = $timestamp-$timeoutseconds;
- y2 m  j! h9 Y. Q1 C//connect to database
' Y; V: s7 Q5 z. b7 G/ u. h0 gmysql_connect($server, $db_user);9 F' e3 U8 P. a5 m+ N4 P
//add the timestamp from the user to the online list
2 b7 f# B" W" ~0 _9 b4 D; s$insert = mysql_db_query($database, "INSERT INTO useronline VALUES8 z) u% {* l- F  E/ F0 u
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");4 _+ s8 W5 k5 Q; U+ L9 j5 ]9 x, u
if(!($insert)) {, w+ w4 }$ h! i! c: ?  q' N
print "Useronline Insert Failed > ";) z- ~- O+ e& t- W% ^
}! y8 j, k6 E, h2 {9 H& _5 X* h
//delete the peoples which haven't been online/active in the last $timeoutseconds seconds.
. V: x3 E8 B9 M+ I" h  _: z; `$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");
8 B% r: a: ?1 a) @- D1 j7 Rif(!($delete)) {: M1 h# Z! l$ E- a3 L' q. [) |
print "Useronline Delete Failed > ";
& `# Q$ R& R+ h4 f% V7 b' a}$ A8 D8 M& \( r' D( I' U4 f
//select the amount of people online, all uniques, which are online on THIS page  T2 G9 f& x, m1 e* |! n# b
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");
$ j9 }" P6 f) v& q4 jif(!($result)) {
. ^  k7 E  t6 z$ {5 Oprint "Useronline Select Error > ";
  R. D( Z( _8 E3 k7 r}
3 d% {( j' U( P//Count the number of rows = the number of people online/ T! T9 H* B! u0 l
$user = mysql_num_rows($result);
. |& O& r- w4 e8 n8 L//spit out the results; o- m6 Z/ C( e* ], n1 L: z
mysql_close();) D2 V' L- E& I" E7 Z( f
if($user == 1) {
. p, \* E( s, kprint("1 user online\n");
) n$ y5 j/ B/ ~. P# F2 q} else {
: c* h$ x1 {& O, `print("$user users online\n");
' H9 h% V7 S7 z}
+ J/ @0 [! w$ d  m4 ?. B?>
- R+ O& }8 T  R3 L
5 _& x- G- `9 y. t; _4 P0 q- V以上代码我是在时代互联提供的免费试用主机上测试的,感觉效果还不错,性能很稳定,不信的话你也可以去试试。当然,记得指出我这个程序中的不足先哦。
2 h8 l+ p* A2 A- ~' W' d% X时代互联是行业内最早开始提供PHP 虚拟主机的公司之一,现在已经是的金牌域名注册商和中国互联网客户满意度十佳单位,产品功能已经非常完善 ,特别提一下几个特有用的功能:数据库自动备份、免费集成繁简通简装版、多域名绑定、多子网站支持、GCDN 网站加速器、镜像加速、高级访问统计系统、支持WAP、可选电信/网通/香港机房。1 }( z+ O! C# O# j/ p
我之前建站的时候选的是他们公司标准商务E型,因为这款机型还特别支持ASP.NET3.5(3.0/2.0/1.1)/ASP。
' Z3 i% l, X. {1 m当然啦,这两款主机也是相当不错的。
# C0 b2 q- A  C4 l" x$ R/ u! b智强商务B型:Unix /Linux 操作系统+独立网页空间1000MB+送1000MB邮局空间+100MB MySQL数据库空间=1869元/年  Z+ P8 V; w# `
标准商务B型:Unix /Linux 操作系统+独立网页空间500MB+送800MB邮局空间+60MB MySQL数据库空间=1365元/年
" c5 c. w& h6 r- }0 e: K( b提供一下这个公司的联系方式:请见:http://www.now.cn/vhost/
; r  N& n+ i  ^7 I空间专线: 0756-2623871  QQ:168075865,好象还有全球免费网络电话:全球免费咨询电话 http://www.now.cn/callcenter/call.net?LineName=55
- o, d/ M. G  f8 l! _; Y! I自己加QQ去问吧。

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