返回列表 发帖

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

刚用PHP写了一个网站在线人数的程序,请大家进来指点下!
, ?. z# [8 w6 k' h0 L% b$ j我是用PHP+MYSQL来写的,原理:网站在线人数的程序代码+后台有MYSQL数据库支持,可以直接统计出网站当前的在线人数。
# U5 ]' s( h: a% `6 s# N' K+ U! ?首先我创建MYSQL数据库表。! }2 O# _1 ?- t% }' ?9 d) b
CREATE TABLE tablename (/ Y! c) k& k5 k
field type(max_length) DEFAULT 'default_value' (NOT) NULL2 C! n6 Q* l. V; B# w" G6 s; ?
}可以使用的SQL语句。
% }: d8 I( U" KCREATE TABLE useronline (5 _2 Z8 S- W% A8 ^
timestamp int(15) DEFAULT '0' NOT NULL,# i3 N$ _4 s" y9 U( {
ip varchar(40) NOT NULL,$ R8 k# u0 A" \
file varchar(100) NOT NULL,
6 K% P" x7 S& @4 l5 _; F2 O5 tPRIMARY KEY (timestamp)," M6 r! O8 V  ]2 d$ `; T/ u
KEY ip (ip),# I% Y5 g. S" B0 E2 q* n
KEY file (file)
7 Z; K# ?# Z. ]0 r9 T2 B);下面我们是PHP脚本,首先我定义MYSQL的信息。
. G1 U8 B9 u1 m# m* ]% g$server = "localhost"; //你的服务器7 S! Z  q0 d9 Q% i/ J
$db_user = "root"; //你的mysql的用户名0 w* W" Q# t+ i, Y$ X2 _
$db_pass = "password"; //你的mysql的密码/ L9 `" G9 l+ ^) D/ N
$database = "users"; //表的名字设置统计的时间(多少秒内在线人数)+ G8 Z! I/ w* B* f- i+ @
$timeoutseconds = 300;取当前时间。8 f( _2 A2 s9 F" x
$timestamp = time();上面的完整代码:  l2 \0 Q$ A: B1 `6 w5 H! R0 H
<?php
4 t( L) A' L. p' s$server = "localhost"; //your server  C9 d  _0 C; y. Y0 W
$db_user = "root"; //your mysql database username! D8 l6 i* s) r( m7 u
$db_pass = "password"; //your mysql database password if any- K( E* i9 q" |" i$ {
$database = "users"; //the db name
; y/ }& x6 X7 d7 k. \$timeoutseconds = 300;//timeoutseconds limit+ R: m! e! h- R1 D  K5 l2 X  y7 R4 \& j
//get the current time
; l* ?6 Z  }' U$ D$timestamp = time();. ~6 B: H' @8 L/ ?
//calculate the lowest timestamp allowed4 l/ S( w( ?$ N  p1 B
$timeout = $timestamp-$timeoutseconds;
$ f8 X0 J( e2 o?>连接mysql- W0 t. M0 l+ `
mysql_connect('localhost', 'username', 'password');也允许使用变量形式。
$ R8 O5 U* c/ g' @9 |) wmysql_connect($server, $db_user, $db_pass);如果mysql数据库没有密码的话可以使用下面代码连接% \- y; ~. o* [7 {/ ^  I
mysql_connect($server, $db_user);查询数据库的代码:* @/ Q$ l7 D, O& y1 y4 b% ]1 Y
mysql_db_query('database', 'query');我们只要有访客就要增加一条记录。$ O) v) D" V8 r" q% _
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES" u% Y0 i: o6 x, G
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
2 ^9 q  [; w9 M% f7 c* F# p8 o% \/ @如果用户用错误信息的话,这样处理。
) M. I  d9 d4 F- x. n6 Gif(!($insert)) {
) _, S2 i/ v+ r4 `  m7 d4 nprint "Useronline Insert Failed > ";/ V# B- l9 R4 s4 ?- l7 ]( h  T3 B
}然后实现当超过设置的时间就删除该用户记录。. [. t& X$ y& M/ C2 H
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");同样给出删除记录出错的处理。
7 E3 {4 V8 D  u- ?# x9 E! Tif(!($delete)) {
! @: D$ \: p$ Aprint "Useronline Delete Failed > ";6 z5 z; k, m* h, H. s5 o9 b+ K
}下面我们解决数据库中不同IP的问题
& K! H) z: F: V8 ~$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");使用
5 ]( ?+ M. X% n+ q7 [: n2 umysql_num_rows(query);来统计用户,代码如下。
5 r! E  ?, w  N3 S3 l" j$user = mysql_num_rows($result);最后关闭数据库。
, {; b! Q& v3 Y5 Z5 s, w9 kmysql_close();显示在线的人数。
1 w7 g5 ^; y% M( Tif($user == 1) {6 |. g2 y$ D6 _# g
print("1 user online\n");
% U( h" T- V; c- K/ B6 D2 l/ c6 h} else {
# [& D7 S: |* c" d' O5 e; Kprint("$user users online\n");
/ v, }+ `9 Y  P0 Y}最终把上面代码写成一个PHP文件如下。
3 u) z  d1 _0 i  i; O7 `& V<?php
9 n& Q5 K0 N/ R5 V//Put your basic server info here
/ d5 j6 Z: _3 h) z6 w3 G+ W$server = "localhost"; //normally localhost
8 C2 i$ [! `, ^  \$db_user = "root"; //your MySQL database username
( d! ]' @2 Y+ s/ ]( q: A$db_pass = "password"; //your MySQL database password8 U1 }3 Z8 T- }% e
$database = "users";
" z4 I  x" K* o3 a% y2 _7 F$timeoutseconds = 300; //it will delete all people which haven't refreshed(so probbably are
/ F+ }5 B# s# V0 _3 M5 @, o// offline or inactive) in $timieoutseconds time (so it actually checks the people that are active in the last% K+ Y) e' b3 z9 b0 m" U+ w1 _
// $timeoutseconds seconds)4 {% v5 k" U+ \" U7 }2 Q9 {) K
//this is where PHP gets the time
) w0 h7 F+ r8 G; [$ q$timestamp = time();
2 F7 i- a  v' S6 u- U/ K1 l//counts the timeout, all people which have been seen last online in earlier than this timestamp, will get removed
; J- k1 I2 l5 n/ F6 s$timeout = $timestamp-$timeoutseconds;* W* E7 V. s6 Z1 l( s  f6 @
//connect to database
' X  ]. C) U  v. L% b; k% K7 Lmysql_connect($server, $db_user);7 h3 i* U- r2 X- j* d7 F" _$ r; G
//add the timestamp from the user to the online list
- v1 \* m# R. W7 g$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
1 {/ B* _# a9 S9 q& {8 S! e('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
% P/ w# d7 F, Cif(!($insert)) {! `. k+ h# a! I8 `' o
print "Useronline Insert Failed > ";- `0 v4 C9 M; e# F! i
}
9 X/ L) _8 `: u& w//delete the peoples which haven't been online/active in the last $timeoutseconds seconds.- x' d' {9 a& G; @0 K4 @
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");
/ U9 z+ m( d1 J  x( Cif(!($delete)) {
$ g# o/ Z3 {* f, K: nprint "Useronline Delete Failed > ";) c& {9 e0 B. b; ]" X% o
}" v! U5 j) Q* M8 i/ T
//select the amount of people online, all uniques, which are online on THIS page/ r+ X9 t) N5 l. Y! o; \: @
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");% L0 f8 j8 m# Z+ W$ r8 Z
if(!($result)) {
. e$ A& y* x* Q5 d. m" ]- |9 xprint "Useronline Select Error > ";# q' p; p. x4 ~1 N5 J$ R
}/ }* ~4 O6 g1 q; R
//Count the number of rows = the number of people online  p" F: O& t8 ^
$user = mysql_num_rows($result);2 ^. }9 N# U4 b, `
//spit out the results9 p- T" ^+ F' U
mysql_close();
; o- Z0 q- R- I+ j/ I! V, l3 lif($user == 1) {: U- {7 a9 f. \: {3 c6 o* C& U
print("1 user online\n");
, f3 R( S# Y" ?6 s; N0 F( _# y} else {
5 _6 @4 P4 I% `0 l0 |print("$user users online\n");
  ^5 {5 T/ n. D9 c: i3 ~}& J1 m6 `# j+ w3 ?
?>3 c/ P, N* v0 F8 `- n8 U
# m. k" I6 I! W$ I' _
以上代码我是在时代互联提供的免费试用主机上测试的,感觉效果还不错,性能很稳定,不信的话你也可以去试试。当然,记得指出我这个程序中的不足先哦。
& A! }' v: B) W" r9 x6 I2 v; A- O时代互联是行业内最早开始提供PHP 虚拟主机的公司之一,现在已经是的金牌域名注册商和中国互联网客户满意度十佳单位,产品功能已经非常完善 ,特别提一下几个特有用的功能:数据库自动备份、免费集成繁简通简装版、多域名绑定、多子网站支持、GCDN 网站加速器、镜像加速、高级访问统计系统、支持WAP、可选电信/网通/香港机房。8 [; |: a, `% L9 |3 K/ _* N8 }; j
我之前建站的时候选的是他们公司标准商务E型,因为这款机型还特别支持ASP.NET3.5(3.0/2.0/1.1)/ASP。1 G. h9 u+ T% ]9 }8 V
当然啦,这两款主机也是相当不错的。! g. t, |) a5 Y3 l: A
智强商务B型:Unix /Linux 操作系统+独立网页空间1000MB+送1000MB邮局空间+100MB MySQL数据库空间=1869元/年
. w2 A' [2 r1 i+ j* X; B( k标准商务B型:Unix /Linux 操作系统+独立网页空间500MB+送800MB邮局空间+60MB MySQL数据库空间=1365元/年
$ t) C/ b, n9 p! R8 Z* B6 s- l提供一下这个公司的联系方式:请见:http://www.now.cn/vhost/
, o" Q+ k3 X! A空间专线: 0756-2623871  QQ:168075865,好象还有全球免费网络电话:全球免费咨询电话 http://www.now.cn/callcenter/call.net?LineName=55" Q; {8 O0 Z4 T0 q. `4 z( V3 G
自己加QQ去问吧。

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