返回列表 发帖

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

刚用PHP写了一个网站在线人数的程序,请大家进来指点下!
1 O" s+ s, P8 y- E我是用PHP+MYSQL来写的,原理:网站在线人数的程序代码+后台有MYSQL数据库支持,可以直接统计出网站当前的在线人数。5 f+ L0 }& o; N. D2 J4 A! B
首先我创建MYSQL数据库表。8 J9 b" e4 E- l9 l5 i: ]& {
CREATE TABLE tablename (
( P( u; s5 p5 efield type(max_length) DEFAULT 'default_value' (NOT) NULL" z% X& v1 ]; i) W0 V" k
}可以使用的SQL语句。! S8 Z: ?% s4 h0 m0 i+ ^+ d1 [: w
CREATE TABLE useronline (4 ~  F9 c: i2 u8 T( ^, G
timestamp int(15) DEFAULT '0' NOT NULL,
5 y9 e4 ^- `' Y& s# Fip varchar(40) NOT NULL,
% I( ?/ ?6 s* U* Y" b! l! ifile varchar(100) NOT NULL,7 k9 H% K3 L. k, l+ ^( h
PRIMARY KEY (timestamp),
' q0 U4 ], h4 V7 D! M( ?9 A, h5 cKEY ip (ip),
7 m0 T2 V: d$ W! i6 J3 \' cKEY file (file)8 B8 r- d2 G; K3 j
);下面我们是PHP脚本,首先我定义MYSQL的信息。+ C" s  Q6 j4 v- |2 j- Z. W6 Y1 V
$server = "localhost"; //你的服务器
6 E! I% A. S* s( h$db_user = "root"; //你的mysql的用户名
5 |8 Q+ G" m; w; k# s$db_pass = "password"; //你的mysql的密码
) v1 g. I! l" k- ^$database = "users"; //表的名字设置统计的时间(多少秒内在线人数); `7 [# h& N3 O' u6 j3 M
$timeoutseconds = 300;取当前时间。
9 ]5 L3 B+ ^7 |5 [$timestamp = time();上面的完整代码:; ^8 e+ z; \/ s7 A4 a4 h" Z
<?php; M( i5 T3 \6 r5 u* K
$server = "localhost"; //your server
& d. Z/ H+ W) v. }3 j$db_user = "root"; //your mysql database username
- @+ Q" j" t, Y$db_pass = "password"; //your mysql database password if any
2 f# f7 T* I1 ^% y5 _- `$database = "users"; //the db name9 k7 t2 M3 N/ Z! b5 P
$timeoutseconds = 300;//timeoutseconds limit
. w2 N( K! i+ v9 ]% S//get the current time
! Y* y; e( i# {$ w$timestamp = time();; A7 H. u. }+ E& O9 [
//calculate the lowest timestamp allowed+ l# Z3 D* a* ~! C6 q
$timeout = $timestamp-$timeoutseconds;
' l* s/ r  z% p: C?>连接mysql; e$ o& s0 B6 Z" F7 R
mysql_connect('localhost', 'username', 'password');也允许使用变量形式。! l' D  k9 ~* H  E. u* n+ t* p6 f
mysql_connect($server, $db_user, $db_pass);如果mysql数据库没有密码的话可以使用下面代码连接6 z# j! x# G$ V% _
mysql_connect($server, $db_user);查询数据库的代码:) i1 n, X) L6 R0 D2 Q2 Z
mysql_db_query('database', 'query');我们只要有访客就要增加一条记录。
, A; [# O9 A! H* U; g! ]$insert = mysql_db_query($database, "INSERT INTO useronline VALUES1 b8 W5 K( h+ I2 _+ u8 [
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
% V! {. W& t& c- i如果用户用错误信息的话,这样处理。7 s+ B+ T! z% z- Y; N  \) ]% W
if(!($insert)) {
5 c6 _6 X$ P6 p; L; Q- F8 |print "Useronline Insert Failed > ";: b$ p; j: y3 s
}然后实现当超过设置的时间就删除该用户记录。5 g5 V! A% {/ S# F, k% v; G" X0 G
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");同样给出删除记录出错的处理。, O, r3 f: o! ]( `7 }
if(!($delete)) {! T1 `; D5 b5 H/ k" m2 H* n* K) d
print "Useronline Delete Failed > ";
1 W' J5 v$ u9 x+ x# n8 f7 Q}下面我们解决数据库中不同IP的问题
9 g: i5 C- j" m: [$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");使用7 u7 p, Q& {# v9 v5 [1 y6 H
mysql_num_rows(query);来统计用户,代码如下。
: m' y! W5 \2 a5 B1 A$user = mysql_num_rows($result);最后关闭数据库。
; S* D% |) w' {) B' }mysql_close();显示在线的人数。7 e& q1 L! f9 ]0 D1 y7 l
if($user == 1) {1 q& m- d2 K1 N! G0 Y2 j
print("1 user online\n");- d% x" g( y# [: e; g/ |
} else {
0 q1 Z4 i1 {- Y/ n1 Eprint("$user users online\n");
6 D8 K" y, a& B5 W}最终把上面代码写成一个PHP文件如下。
4 m2 L8 j9 f7 P) Y4 n. l9 w<?php5 O; C6 Q$ q9 H. a) P- y
//Put your basic server info here* }5 K# `: L& d& j
$server = "localhost"; //normally localhost8 b! m+ b8 Q: t
$db_user = "root"; //your MySQL database username
% n& o' V8 k. U/ [$db_pass = "password"; //your MySQL database password
" w: c! s8 n8 g" e3 f0 Q$database = "users";
0 L6 W; q2 q  N3 }% z$timeoutseconds = 300; //it will delete all people which haven't refreshed(so probbably are
# A: o3 w' ]6 ^4 H/ ]; a3 ~  {9 _// offline or inactive) in $timieoutseconds time (so it actually checks the people that are active in the last
! w+ G$ _" K  l  ^' I+ V// $timeoutseconds seconds)
2 Z( D. f" R2 r. _//this is where PHP gets the time
6 {  x! a8 U- E( j# V! p" T$timestamp = time();% d4 g9 b8 z' R$ B0 n3 f  [  ~
//counts the timeout, all people which have been seen last online in earlier than this timestamp, will get removed
. Q' q1 K& Q5 P/ g% c% h- O$timeout = $timestamp-$timeoutseconds;7 o# y8 M9 |5 U
//connect to database! \% s& L! j% C1 A
mysql_connect($server, $db_user);6 ~$ N/ f# P; F
//add the timestamp from the user to the online list, P6 I  r! ?' l+ X; B* `
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
6 j6 Z: Y2 g9 X' }* ]('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
* Q  E$ z: k. @# f2 V) Cif(!($insert)) {
2 o4 x: [) F; Aprint "Useronline Insert Failed > ";
3 s" G6 ^8 P! s% }. j4 J/ ?4 k/ u0 [. K}# m8 h; @: P. A) k8 n/ i* Y  L$ k9 R
//delete the peoples which haven't been online/active in the last $timeoutseconds seconds.
" @% M9 ~- C* q5 o7 h: q" a0 Z* \+ l3 b$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");
/ e& S! ?& k' q. Z/ O' m" `if(!($delete)) {$ W  C5 P1 V8 x
print "Useronline Delete Failed > ";5 p, B7 r" m6 {/ g7 C- _& [9 d
}$ N  _9 H( z, `1 M  ]8 V
//select the amount of people online, all uniques, which are online on THIS page; m& P1 K# P8 F6 t
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");! |4 ]) ?0 Z$ D7 n1 @
if(!($result)) {
0 f1 L6 I; c' W2 V- Hprint "Useronline Select Error > ";
1 w' m, u' x) _}
$ m1 d% _# J4 W//Count the number of rows = the number of people online6 R4 @8 q  i; `: }2 _
$user = mysql_num_rows($result);
3 }' W* x) M* v( z//spit out the results
) u  S" D/ Q- O! [# o6 Gmysql_close();
$ J. d; Q' |$ e3 ~  `/ e$ tif($user == 1) {
! @* Z) O. B# Z3 k9 \4 O! o- F4 fprint("1 user online\n");: l/ B  a- d2 K2 c$ F3 i
} else {
; W: R/ D3 P4 ^4 l& Mprint("$user users online\n");2 ~: ?" k. v2 T
}( `  B3 x+ z% h7 i8 N$ N
?>! e) F) v0 q9 a5 N5 D
7 i7 X3 X' S( a- E7 X, p
以上代码我是在时代互联提供的免费试用主机上测试的,感觉效果还不错,性能很稳定,不信的话你也可以去试试。当然,记得指出我这个程序中的不足先哦。5 s7 h2 Y' @! E8 u, ]+ ?5 ]! o
时代互联是行业内最早开始提供PHP 虚拟主机的公司之一,现在已经是的金牌域名注册商和中国互联网客户满意度十佳单位,产品功能已经非常完善 ,特别提一下几个特有用的功能:数据库自动备份、免费集成繁简通简装版、多域名绑定、多子网站支持、GCDN 网站加速器、镜像加速、高级访问统计系统、支持WAP、可选电信/网通/香港机房。
' j7 g) k: {! y/ W; K1 T' P我之前建站的时候选的是他们公司标准商务E型,因为这款机型还特别支持ASP.NET3.5(3.0/2.0/1.1)/ASP。
. d% S8 n' E6 p& k2 P' |# D% c当然啦,这两款主机也是相当不错的。
# ~9 d2 K* l! t智强商务B型:Unix /Linux 操作系统+独立网页空间1000MB+送1000MB邮局空间+100MB MySQL数据库空间=1869元/年( g4 z; q( J2 ]# O  u
标准商务B型:Unix /Linux 操作系统+独立网页空间500MB+送800MB邮局空间+60MB MySQL数据库空间=1365元/年; N# E1 n5 k# I. C, C- L
提供一下这个公司的联系方式:请见:http://www.now.cn/vhost/ ( }' ?9 t3 D( s4 X$ z: Z6 \( Z
空间专线: 0756-2623871  QQ:168075865,好象还有全球免费网络电话:全球免费咨询电话 http://www.now.cn/callcenter/call.net?LineName=55" V6 j7 q5 T% I2 U3 j# s' g" j
自己加QQ去问吧。

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