返回列表 发帖

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

刚用PHP写了一个网站在线人数的程序,请大家进来指点下!
( j5 g/ T4 [; y/ ]! P我是用PHP+MYSQL来写的,原理:网站在线人数的程序代码+后台有MYSQL数据库支持,可以直接统计出网站当前的在线人数。1 h- j  v0 F* g8 l7 @( ?5 V
首先我创建MYSQL数据库表。+ [: b0 R+ u  m( o' t4 s
CREATE TABLE tablename (+ \9 f" @2 S0 B) L
field type(max_length) DEFAULT 'default_value' (NOT) NULL
1 G6 T: m7 T, ~}可以使用的SQL语句。# `- ?' V& d6 S3 v! Y; h1 o1 G
CREATE TABLE useronline (
4 p# {: @3 u) u  Rtimestamp int(15) DEFAULT '0' NOT NULL,9 T- p4 y# p& h" u3 }
ip varchar(40) NOT NULL,  _6 ^$ ]& b% ?, I
file varchar(100) NOT NULL,, {5 t: H1 m4 P' y! j6 Y
PRIMARY KEY (timestamp),7 N# A. |* h. H6 H* {
KEY ip (ip),
' [5 j& b% w7 n" ^' G0 a  jKEY file (file)
$ P1 d( U; {, ~4 ^9 F, x9 ^% y);下面我们是PHP脚本,首先我定义MYSQL的信息。- K3 y+ f5 t4 K  H; x, b
$server = "localhost"; //你的服务器& L2 z* z2 j6 s- O
$db_user = "root"; //你的mysql的用户名
7 b& ~+ g# S6 N: i, ~6 j! a8 w$db_pass = "password"; //你的mysql的密码( S& w0 E' O9 T  f
$database = "users"; //表的名字设置统计的时间(多少秒内在线人数)5 S7 p# b( q5 _( D! o% Y! }
$timeoutseconds = 300;取当前时间。
2 Y0 l" G( @! d6 O$timestamp = time();上面的完整代码:
' ~& x( c/ H, d' |: J  N<?php7 ?3 {) j+ B" `) [" Y' @
$server = "localhost"; //your server
+ p0 S0 p% Z# W; F/ @$db_user = "root"; //your mysql database username
: L" g9 A. W- S) b$db_pass = "password"; //your mysql database password if any  w4 ?- A% {& @% G; c* i" F/ B
$database = "users"; //the db name
: x% f/ Z' {' V: S6 ?. e$timeoutseconds = 300;//timeoutseconds limit6 t7 D$ f' A( R" M* D. g7 e+ E
//get the current time
# T4 t, Z  }: S$timestamp = time();% d/ V2 U2 }& `7 h0 l# I
//calculate the lowest timestamp allowed
+ Y- h& x, \2 M% a; x9 e$timeout = $timestamp-$timeoutseconds;& W1 e- p4 U# y4 ?& p$ ^2 {
?>连接mysql
" Y4 [& o, C4 {  A( `mysql_connect('localhost', 'username', 'password');也允许使用变量形式。, Y1 Z( ~( {7 P9 b; I/ @" A
mysql_connect($server, $db_user, $db_pass);如果mysql数据库没有密码的话可以使用下面代码连接# ^4 V- g8 ^% x! b
mysql_connect($server, $db_user);查询数据库的代码:
; E9 h! V+ U3 n. u, d8 omysql_db_query('database', 'query');我们只要有访客就要增加一条记录。2 J; B3 V+ ^5 C
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
9 d) ?; P, u" _* V* Y('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");# _* T' e/ f4 [5 i% A! d
如果用户用错误信息的话,这样处理。
2 C# L; |6 M% O: X6 o# u! S7 V# f1 y7 S: `if(!($insert)) {
7 t; O, V5 ]3 Yprint "Useronline Insert Failed > ";
3 B6 d* j! F0 g! e}然后实现当超过设置的时间就删除该用户记录。3 A  @. A6 L7 ~/ @3 K
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");同样给出删除记录出错的处理。
( w  z1 s1 v- p+ G' u4 Fif(!($delete)) {
' o6 R' y, R2 |3 k: t4 hprint "Useronline Delete Failed > ";
4 x: ^9 M1 p& |. \% F/ q}下面我们解决数据库中不同IP的问题' q# s7 e3 n7 R/ o- O
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");使用
" f. c$ z7 }" n- n- x$ `6 {mysql_num_rows(query);来统计用户,代码如下。3 {2 S/ \# ?- _0 B6 G9 `& O. V# R
$user = mysql_num_rows($result);最后关闭数据库。. S4 [" z8 ?6 {1 P& K
mysql_close();显示在线的人数。
, C- h2 Q/ W6 c: A  G& Eif($user == 1) {
1 S1 w* n# ?3 T/ Qprint("1 user online\n");6 ^( G. s2 \! V4 w# G8 @
} else {
9 b! o) r1 O- @0 P( i) J& n# D! _print("$user users online\n");9 Z; {5 Z0 `3 x& w1 N
}最终把上面代码写成一个PHP文件如下。" f& e% _: v" r% b- R
<?php
) k# \; _( K) U$ F4 M2 w, M//Put your basic server info here
8 h! [3 Z* f- ^+ Y% R$server = "localhost"; //normally localhost
- _( P% q- C* H$db_user = "root"; //your MySQL database username
$ O4 a* e+ k& A2 Z; C, c5 P- f$db_pass = "password"; //your MySQL database password
" s! B- Q( M, P8 B4 {7 r6 Q$database = "users";
# T1 O( C+ o. R$timeoutseconds = 300; //it will delete all people which haven't refreshed(so probbably are
) \+ q+ R# k7 ^( j1 k// offline or inactive) in $timieoutseconds time (so it actually checks the people that are active in the last1 Y- b. a' t& l& x4 a% \1 X
// $timeoutseconds seconds)
: k- a2 V, N; }& e2 Q//this is where PHP gets the time  U1 @5 T1 c0 b
$timestamp = time();5 M6 U! ^! e( T3 a) T, y7 L' Q
//counts the timeout, all people which have been seen last online in earlier than this timestamp, will get removed
. h  m+ U8 ^, n6 T$timeout = $timestamp-$timeoutseconds;( }2 Y7 z/ ^8 J0 b/ K* y# n
//connect to database
+ B8 k! U" K' ~4 A6 Pmysql_connect($server, $db_user);
4 y& Z' E/ n+ m) v! O//add the timestamp from the user to the online list
! Z" F0 Y* g* |$insert = mysql_db_query($database, "INSERT INTO useronline VALUES+ }2 M. b9 F( l! g4 D
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
0 ?1 B8 i( B$ Q' I3 Sif(!($insert)) {
, E9 ^" e: f' D  l: P; u) bprint "Useronline Insert Failed > ";
# t. z6 s2 ?+ c. ^/ Q}
' R1 C- g8 z% ?* u% v7 f//delete the peoples which haven't been online/active in the last $timeoutseconds seconds.
4 I0 ]" ], \& O1 e( S& h& k" ^$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");* q6 t& o& M* }9 }6 b
if(!($delete)) {6 e# V7 w7 t) g. B
print "Useronline Delete Failed > ";
' X  \7 U0 l2 ?$ T" d! r( R}
$ h1 C! w& K3 x$ P4 I# J" y//select the amount of people online, all uniques, which are online on THIS page9 B  c; z' c* [/ i/ `' G  y
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");$ J8 Z* u; d' z+ u
if(!($result)) {* \8 X( K* ?6 K3 X2 ~) }/ h3 Q9 p, T
print "Useronline Select Error > ";
) P, w6 I. L( H! T}
% e% j7 ^; p% P: s' f3 w//Count the number of rows = the number of people online
, @7 W$ [* S# I- l7 x$user = mysql_num_rows($result);
2 y' T- j: y% q: {, u; E: S//spit out the results7 q1 M! F9 A# _! h4 u* z
mysql_close();7 B1 f/ Z! f1 Z3 j. l
if($user == 1) {+ O; F/ y5 @: ^4 J% Y; p* t+ g
print("1 user online\n");$ @, j, z; N( U" @; n
} else {
4 l$ p0 q) G1 r2 L/ o8 T/ Sprint("$user users online\n");
1 m8 M% P1 D3 W* V0 q0 C1 f1 W}
, R( X: K. }( ~5 G* ~?>
) f" }. [5 i& p1 c( K0 C% X5 a; G
' j( H9 C' x5 d  e6 R' X- }6 C以上代码我是在时代互联提供的免费试用主机上测试的,感觉效果还不错,性能很稳定,不信的话你也可以去试试。当然,记得指出我这个程序中的不足先哦。
, t2 N2 c% G2 O1 K& u: t6 `时代互联是行业内最早开始提供PHP 虚拟主机的公司之一,现在已经是的金牌域名注册商和中国互联网客户满意度十佳单位,产品功能已经非常完善 ,特别提一下几个特有用的功能:数据库自动备份、免费集成繁简通简装版、多域名绑定、多子网站支持、GCDN 网站加速器、镜像加速、高级访问统计系统、支持WAP、可选电信/网通/香港机房。
. a6 X9 c$ _1 ~. }  V& m我之前建站的时候选的是他们公司标准商务E型,因为这款机型还特别支持ASP.NET3.5(3.0/2.0/1.1)/ASP。4 ~1 J$ C6 E) ?2 `
当然啦,这两款主机也是相当不错的。
7 b0 S4 t; l& \: k4 M" {. J智强商务B型:Unix /Linux 操作系统+独立网页空间1000MB+送1000MB邮局空间+100MB MySQL数据库空间=1869元/年
2 L# P+ a- [! d标准商务B型:Unix /Linux 操作系统+独立网页空间500MB+送800MB邮局空间+60MB MySQL数据库空间=1365元/年6 |- q0 W8 ?$ l" v) \& t
提供一下这个公司的联系方式:请见:http://www.now.cn/vhost/
) d; w+ Z) G  C: F空间专线: 0756-2623871  QQ:168075865,好象还有全球免费网络电话:全球免费咨询电话 http://www.now.cn/callcenter/call.net?LineName=55
+ P4 t! {5 u# @1 t自己加QQ去问吧。

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