返回列表 发帖

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

刚用PHP写了一个网站在线人数的程序,请大家进来指点下!
* R) Y# ?& k7 v2 t0 A我是用PHP+MYSQL来写的,原理:网站在线人数的程序代码+后台有MYSQL数据库支持,可以直接统计出网站当前的在线人数。
* B$ ^6 F, A7 s. `* W4 f首先我创建MYSQL数据库表。2 \/ @$ }# O  Y# r
CREATE TABLE tablename (' A5 E3 r' d% U( Q6 a) C
field type(max_length) DEFAULT 'default_value' (NOT) NULL. m$ V# D, ?& P8 V" W& m
}可以使用的SQL语句。
5 y6 Z+ V' c' o6 n# S( j6 p  FCREATE TABLE useronline (. a6 i/ q7 Q) x( H- G
timestamp int(15) DEFAULT '0' NOT NULL,
- C3 b& c- `' }2 k7 C) Q/ g9 Lip varchar(40) NOT NULL,
2 @: ^5 U! T  Q+ n' ~file varchar(100) NOT NULL," h- x) c7 b6 v% W
PRIMARY KEY (timestamp),
; P5 I6 w) m  D. z# @KEY ip (ip),
. h# k0 y' j$ V6 C+ a6 B; q) ZKEY file (file)
2 A7 R$ |8 K; |' N8 f+ M& {8 G);下面我们是PHP脚本,首先我定义MYSQL的信息。# ?" w& m- T: J- h; {( a4 V( T
$server = "localhost"; //你的服务器
, D5 |# G2 v9 A$ y; L$ `, C. p$db_user = "root"; //你的mysql的用户名
7 u6 `. D  G- F$db_pass = "password"; //你的mysql的密码
7 G+ |$ O. M# f% B$database = "users"; //表的名字设置统计的时间(多少秒内在线人数)- B7 Q) M5 m: ?* M
$timeoutseconds = 300;取当前时间。) a- o& k3 \  U
$timestamp = time();上面的完整代码:4 Z) i, p8 x" w8 d
<?php
' {6 g7 x/ z! _7 z7 K% {$server = "localhost"; //your server' B. p/ q' a0 \* \5 Y
$db_user = "root"; //your mysql database username
( {, g' M" [* f% z- K$db_pass = "password"; //your mysql database password if any& k8 T0 ]% s7 R9 o9 `+ K
$database = "users"; //the db name# {/ x6 {, m# ]* D
$timeoutseconds = 300;//timeoutseconds limit' F7 g1 D7 {* W8 B' `
//get the current time
/ C" q  j  K# Z! Q, y0 h& H' F$timestamp = time();, X, z5 S' N6 X$ ?, K
//calculate the lowest timestamp allowed& {4 X- Y2 L$ x
$timeout = $timestamp-$timeoutseconds;6 O  X/ D- @3 S( d( m- F
?>连接mysql  p5 C/ w, }9 I; }
mysql_connect('localhost', 'username', 'password');也允许使用变量形式。
$ D9 g+ R! i7 c$ [# H" V5 q/ ]mysql_connect($server, $db_user, $db_pass);如果mysql数据库没有密码的话可以使用下面代码连接
1 M3 ^' k3 O$ g$ p" xmysql_connect($server, $db_user);查询数据库的代码:
9 Y; h, F" W9 F9 e0 ymysql_db_query('database', 'query');我们只要有访客就要增加一条记录。9 [; u* S' @( b# h3 p2 u# s
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES* N; z9 f: i) t0 }0 F( i6 g  I
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");* Y# ^% h# `! q6 D( }, g# u. L* B
如果用户用错误信息的话,这样处理。: j3 @; H) K$ s& ?( k
if(!($insert)) {! a- \( w5 q2 J
print "Useronline Insert Failed > ";. A) q+ Y& r9 e2 g* t% p  ?
}然后实现当超过设置的时间就删除该用户记录。
+ O# Y6 C6 n$ o$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");同样给出删除记录出错的处理。
/ `* K; ~5 U1 h, D5 R( f/ W" m' ?if(!($delete)) {
& s$ O( P& f: C+ [+ G  aprint "Useronline Delete Failed > ";$ D; l) P7 N( m
}下面我们解决数据库中不同IP的问题% E$ s: d- n& V/ f3 f5 h/ h
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");使用. _/ c( n5 i3 e: k9 X( y
mysql_num_rows(query);来统计用户,代码如下。
# q, C# ~* s: u# S6 a* l4 b$user = mysql_num_rows($result);最后关闭数据库。
" |' m8 q2 U7 i# }& k6 e8 T5 o" S+ pmysql_close();显示在线的人数。6 @9 q+ d( g4 C- O3 U) x
if($user == 1) {0 M7 V7 t, n2 a- l
print("1 user online\n");2 c0 V9 D# H/ J- Z: M
} else {8 K6 O9 E$ T' P7 `1 y9 f7 s8 G( t
print("$user users online\n");
# Q4 o& l- s2 y* b}最终把上面代码写成一个PHP文件如下。
7 P* n' p3 B5 t<?php' S3 L) Q% b, p3 u5 U# @  A" N
//Put your basic server info here
( }, \7 z# h$ T$server = "localhost"; //normally localhost
6 p7 m8 f0 y! M) _$db_user = "root"; //your MySQL database username
( w( f+ ^. q: f  l! a$db_pass = "password"; //your MySQL database password
- Z) W, ^- K& [; u; k$database = "users";( C6 [# r8 N$ m0 t" o* a
$timeoutseconds = 300; //it will delete all people which haven't refreshed(so probbably are1 R+ s# N0 A/ Y% @  w
// offline or inactive) in $timieoutseconds time (so it actually checks the people that are active in the last
: p" u5 Q) I0 C2 R// $timeoutseconds seconds)* W% D+ z  C  J& R5 S, O
//this is where PHP gets the time
* h$ e" u# B5 n: c. i3 e9 M$timestamp = time();
2 l% u  D2 G! Q% D+ v//counts the timeout, all people which have been seen last online in earlier than this timestamp, will get removed* A- w9 b4 u5 L; o8 i
$timeout = $timestamp-$timeoutseconds;* a0 u* P8 [; F$ K: n! A
//connect to database" a' H- G% `1 C7 R" [) m, x
mysql_connect($server, $db_user);
" \6 }5 _0 E* O  W9 C1 L//add the timestamp from the user to the online list+ F( p+ {  \0 A( L( M; C) j$ q# H
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
* s& }5 Y4 I# n  B/ }1 j  H('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
2 `) |$ W7 h1 i) H) o" ~- Cif(!($insert)) {4 c, ^* U+ [6 n' I; d7 l
print "Useronline Insert Failed > ";# R, m# e8 ^5 P$ w' ?; g5 N
}
0 |$ i9 Z5 W+ j//delete the peoples which haven't been online/active in the last $timeoutseconds seconds.  e8 V7 K$ n/ v  |9 v% ]
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");
) Z( O8 Z$ `$ q; eif(!($delete)) {
% R& A( G8 j5 v2 @- E" I$ E3 [, wprint "Useronline Delete Failed > ";
! |8 n) c' b& \8 ~}: v7 _5 Y- K7 m# T9 Q" g5 }; r
//select the amount of people online, all uniques, which are online on THIS page
8 {% p! j( r5 ~# Y: E. V+ I! J$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");/ W) \; J9 J8 K5 v+ E9 z
if(!($result)) {
5 i. H# ~0 h- L/ Y$ B. F: Kprint "Useronline Select Error > ";9 y% w$ f  n( n
}
( ^9 T$ ]/ t; B6 |7 ~/ J( }//Count the number of rows = the number of people online: a3 I9 Z( v" H% Y& D
$user = mysql_num_rows($result);
# a( q3 y8 k% S# B+ A& B/ Y0 M) w//spit out the results
/ ^& \- z% D* B5 U9 t! S$ |8 ^5 Fmysql_close();' W) x! K0 D. V; F8 t
if($user == 1) {
4 |4 M3 h: c" \6 `print("1 user online\n");: [) V" j5 e0 {  d9 w6 w
} else {0 v8 t7 @- o' i
print("$user users online\n");3 _% I7 u8 s# n) ]
}, f( Q: T( h& N3 F2 b4 {- [8 u1 M
?>
% s) g% H) K3 x& D) O7 w$ k: U: i' w/ R6 S) f6 ], {
以上代码我是在时代互联提供的免费试用主机上测试的,感觉效果还不错,性能很稳定,不信的话你也可以去试试。当然,记得指出我这个程序中的不足先哦。
. Y( U! l' n7 a# \" Y. h. a时代互联是行业内最早开始提供PHP 虚拟主机的公司之一,现在已经是的金牌域名注册商和中国互联网客户满意度十佳单位,产品功能已经非常完善 ,特别提一下几个特有用的功能:数据库自动备份、免费集成繁简通简装版、多域名绑定、多子网站支持、GCDN 网站加速器、镜像加速、高级访问统计系统、支持WAP、可选电信/网通/香港机房。
0 W/ W5 j9 P' Z' f) W/ i我之前建站的时候选的是他们公司标准商务E型,因为这款机型还特别支持ASP.NET3.5(3.0/2.0/1.1)/ASP。3 U. q/ m# D9 M6 j4 w; n) Y4 y. |% n
当然啦,这两款主机也是相当不错的。
3 a: j/ a: u7 W6 s, B8 w9 X智强商务B型:Unix /Linux 操作系统+独立网页空间1000MB+送1000MB邮局空间+100MB MySQL数据库空间=1869元/年. ]- P! ~5 R* D
标准商务B型:Unix /Linux 操作系统+独立网页空间500MB+送800MB邮局空间+60MB MySQL数据库空间=1365元/年3 B7 W& X- o! U2 K, m
提供一下这个公司的联系方式:请见:http://www.now.cn/vhost/ * ?- r' x  b4 \' R3 h1 t' R
空间专线: 0756-2623871  QQ:168075865,好象还有全球免费网络电话:全球免费咨询电话 http://www.now.cn/callcenter/call.net?LineName=55- }8 B3 N: V! n7 `8 ]4 S: M" i
自己加QQ去问吧。

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