返回列表 发帖

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

刚用PHP写了一个网站在线人数的程序,请大家进来指点下!6 ^6 V7 U; x6 s9 \/ y3 o- a
我是用PHP+MYSQL来写的,原理:网站在线人数的程序代码+后台有MYSQL数据库支持,可以直接统计出网站当前的在线人数。4 y2 f+ }3 Z/ m  f- T, b
首先我创建MYSQL数据库表。2 u1 k' G1 F( e
CREATE TABLE tablename (
+ P0 @! x% ?& C' n" a5 Kfield type(max_length) DEFAULT 'default_value' (NOT) NULL: h: N4 p- `% Y  N# m% Z; K2 x
}可以使用的SQL语句。
  N$ v4 @6 U4 a; p2 t) k) wCREATE TABLE useronline (2 g6 e! T1 [3 d
timestamp int(15) DEFAULT '0' NOT NULL,5 E  c1 ?- ^0 e, \
ip varchar(40) NOT NULL,
" l/ f' c" W! ~7 R  ~file varchar(100) NOT NULL,; P5 U" ?0 E1 y" Z
PRIMARY KEY (timestamp),
) G; j' v3 p4 }2 [KEY ip (ip),/ @- k. G8 `4 p* ]- w: k
KEY file (file)
) ?. T6 X0 T9 });下面我们是PHP脚本,首先我定义MYSQL的信息。4 a2 J& C3 j* |: V' Q6 d
$server = "localhost"; //你的服务器3 J* a# s3 R7 q$ y, A# m
$db_user = "root"; //你的mysql的用户名! C9 ~4 K) T' S! G
$db_pass = "password"; //你的mysql的密码  x$ o; d! o5 C
$database = "users"; //表的名字设置统计的时间(多少秒内在线人数)
  U9 @2 F, R: l! s$ B: i$timeoutseconds = 300;取当前时间。' r7 W8 v/ w# k
$timestamp = time();上面的完整代码:
+ l* R$ r, f: R1 Q<?php6 C: J- `9 M! L! F
$server = "localhost"; //your server
5 s5 C  B1 ]5 C, E  G/ s; @$db_user = "root"; //your mysql database username
/ s, U! i: P6 z$db_pass = "password"; //your mysql database password if any6 ]3 P4 b1 B! Z; p
$database = "users"; //the db name
* x. q2 s1 N3 K* i$timeoutseconds = 300;//timeoutseconds limit) l6 B7 ^6 v( Y3 e% [) c( a' O6 a
//get the current time" e3 s4 s4 j' M) C( w' Y
$timestamp = time();
  ]3 x1 c. ?3 C, I//calculate the lowest timestamp allowed
$ h# R4 c/ h4 x/ T* w5 ~* M& i$ |! U$timeout = $timestamp-$timeoutseconds;
% b* J6 e( G2 h8 A2 _  q?>连接mysql3 b+ x; T# c% B) d
mysql_connect('localhost', 'username', 'password');也允许使用变量形式。
6 m* D, C: T  n; P$ K/ lmysql_connect($server, $db_user, $db_pass);如果mysql数据库没有密码的话可以使用下面代码连接1 D# _& w' ~7 P. r& Q) ?
mysql_connect($server, $db_user);查询数据库的代码:
* M. h# z8 G  T/ F2 nmysql_db_query('database', 'query');我们只要有访客就要增加一条记录。
$ Z  f5 B2 X: {) o& r& u$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
# k/ i7 M; P: K, |5 T2 `& @% a('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");% P, G2 U+ O; }1 _  _1 _; f
如果用户用错误信息的话,这样处理。
7 V3 S2 M% H6 \) v" nif(!($insert)) {
2 c+ o$ i# B" M+ t+ e' g: C3 bprint "Useronline Insert Failed > ";
. j/ L2 s. v) {* e}然后实现当超过设置的时间就删除该用户记录。
3 r1 ?4 J1 B- D1 d  R$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");同样给出删除记录出错的处理。
* t) ?4 h* j* q! U- Tif(!($delete)) {+ b/ Q, G. @. J! x# Q2 V
print "Useronline Delete Failed > ";
4 }- W8 t% ^' H' H}下面我们解决数据库中不同IP的问题. \  X" n. r' o. X' E1 t
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");使用7 P* U5 p* Y% t7 ~( O3 E$ t6 ^
mysql_num_rows(query);来统计用户,代码如下。; A- V" o, g# m" D0 G' [
$user = mysql_num_rows($result);最后关闭数据库。
  {% D6 w# w3 r- \3 o7 D- `mysql_close();显示在线的人数。
5 R3 }: U, C' ~1 [& _if($user == 1) {
* o& a; K0 P3 X/ B% J7 o( E/ Pprint("1 user online\n");" x1 B4 P; ]7 G+ N/ Q4 b7 g& N
} else {
% f) b% I1 U" M8 j, t0 @5 w; oprint("$user users online\n");
' E& |4 D/ s2 X$ p$ G* G' s8 {# |' K}最终把上面代码写成一个PHP文件如下。
1 k, W* F( M3 N( ~2 g; r, r<?php- a  H, `; y7 l  q5 Q  N
//Put your basic server info here6 |) o# ]% M. p
$server = "localhost"; //normally localhost8 C1 }6 U/ {/ o- B& n) d- i6 f
$db_user = "root"; //your MySQL database username
0 N; _4 A8 b; X/ h- L4 S$db_pass = "password"; //your MySQL database password8 B: a5 z- G3 G" v( J! S
$database = "users";9 E3 r1 v* X  |4 R( E" {9 R# L' g
$timeoutseconds = 300; //it will delete all people which haven't refreshed(so probbably are
+ p' j8 G- e; A' T// offline or inactive) in $timieoutseconds time (so it actually checks the people that are active in the last4 \: n( _& M1 c  d9 i
// $timeoutseconds seconds)% X. D7 D9 i$ K
//this is where PHP gets the time
7 u$ ^' r3 `- k7 R& i$timestamp = time();6 Q$ y/ q6 n0 ]3 I) c0 i( S2 v
//counts the timeout, all people which have been seen last online in earlier than this timestamp, will get removed8 e. `- F7 v+ v9 [8 v  Z  i: I
$timeout = $timestamp-$timeoutseconds;5 R. r  U+ S$ P/ f: f( `
//connect to database3 _" v3 N# q% K$ K( [& |; e3 C/ B9 t6 G
mysql_connect($server, $db_user);) F, S8 s/ K; |$ Y7 N
//add the timestamp from the user to the online list
  e, |7 ?3 R$ r6 n( r$insert = mysql_db_query($database, "INSERT INTO useronline VALUES' V7 E. z) |3 p! l' _- W6 R
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
- E0 a2 r" A/ Oif(!($insert)) {
- s4 u  V" R$ i7 s1 d" ?5 Uprint "Useronline Insert Failed > ";, [/ G0 q% w9 {& B- a
}
- x9 F" U+ t. F; s! z//delete the peoples which haven't been online/active in the last $timeoutseconds seconds.
0 S5 F' J3 B" n. h5 z9 J% r$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");
# L$ f2 r! p- J& p4 c/ N, Wif(!($delete)) {4 h  A4 L0 m( F2 _$ Y% c
print "Useronline Delete Failed > ";
9 M) O6 }# ~$ ?}8 A# `9 f3 g: y. A, p
//select the amount of people online, all uniques, which are online on THIS page1 ?1 ~- `8 G6 U) ]# Q
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");
/ p1 X" v/ L0 ]" Q/ G8 Kif(!($result)) {
4 w9 Q$ D: {! {1 n8 s1 \' w% Aprint "Useronline Select Error > ";; I- H2 }$ {1 L/ o& S; j+ @
}
" k) S3 U- `2 H//Count the number of rows = the number of people online
1 S( e4 R1 r3 V  }3 b  O3 G$user = mysql_num_rows($result);
8 {, ?# s, u' _& F; F//spit out the results
5 [( @1 w6 w4 Pmysql_close();2 q) ]3 @+ K/ b/ ?1 v" k; T
if($user == 1) {
/ L" p$ v0 I" z" ^4 w7 Vprint("1 user online\n");
5 G/ A/ S* M7 i' H} else {
8 ^6 [3 n5 f4 p% P6 p) G  `, n) Uprint("$user users online\n");
- j7 m0 h. @3 s8 }* E  a- a}
" ~- {. a/ O1 O& F$ [7 b$ h/ T?>. D  m- f: A. I' d1 i9 V

& c! b7 s& b& b- [4 J以上代码我是在时代互联提供的免费试用主机上测试的,感觉效果还不错,性能很稳定,不信的话你也可以去试试。当然,记得指出我这个程序中的不足先哦。$ n  i8 j; V6 ^, r9 q
时代互联是行业内最早开始提供PHP 虚拟主机的公司之一,现在已经是的金牌域名注册商和中国互联网客户满意度十佳单位,产品功能已经非常完善 ,特别提一下几个特有用的功能:数据库自动备份、免费集成繁简通简装版、多域名绑定、多子网站支持、GCDN 网站加速器、镜像加速、高级访问统计系统、支持WAP、可选电信/网通/香港机房。& i% |2 F3 t: z& s
我之前建站的时候选的是他们公司标准商务E型,因为这款机型还特别支持ASP.NET3.5(3.0/2.0/1.1)/ASP。% `# o! L2 _0 U' Q2 J: G
当然啦,这两款主机也是相当不错的。
) }" E) r  N6 H6 L智强商务B型:Unix /Linux 操作系统+独立网页空间1000MB+送1000MB邮局空间+100MB MySQL数据库空间=1869元/年# [$ B0 d: \6 @% |8 \
标准商务B型:Unix /Linux 操作系统+独立网页空间500MB+送800MB邮局空间+60MB MySQL数据库空间=1365元/年6 ]  C" J* `) Q" W0 E2 k8 u0 M
提供一下这个公司的联系方式:请见:http://www.now.cn/vhost/
9 v* |7 u+ @6 g空间专线: 0756-2623871  QQ:168075865,好象还有全球免费网络电话:全球免费咨询电话 http://www.now.cn/callcenter/call.net?LineName=55% A; W" I# N3 R) u
自己加QQ去问吧。

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