
- UID
- 455
- 帖子
- 3
- 精华
- 0
- 积分
- 8
- 金币
- 3
- 威望
- 0
- 贡献
- 0
|
刚用PHP写了一个网站在线人数的程序,请大家进来指点下!
刚用PHP写了一个网站在线人数的程序,请大家进来指点下!4 s) `' w) L4 c- n/ e3 u4 z" Y
我是用PHP+MYSQL来写的,原理:网站在线人数的程序代码+后台有MYSQL数据库支持,可以直接统计出网站当前的在线人数。
$ Q- ?9 v6 T) N# U/ m6 @- \首先我创建MYSQL数据库表。
$ C' A/ }* N7 [+ o- HCREATE TABLE tablename (
3 V) o+ J3 j) V: V$ zfield type(max_length) DEFAULT 'default_value' (NOT) NULL
U5 k! [& I7 g* t}可以使用的SQL语句。4 n0 ?4 M$ D8 l# q5 Q
CREATE TABLE useronline (' y- h( ?8 T$ Z" T1 I0 o: h
timestamp int(15) DEFAULT '0' NOT NULL,! r5 @+ W6 L+ x" H5 n
ip varchar(40) NOT NULL,
7 h$ @& Q/ v# m/ `; v; M+ u& r F/ rfile varchar(100) NOT NULL,% l4 |+ F l7 F
PRIMARY KEY (timestamp),
; E" c8 K; e4 nKEY ip (ip),2 Q1 z8 j0 O+ M0 k& Q
KEY file (file)2 L1 R8 e; {+ d
);下面我们是PHP脚本,首先我定义MYSQL的信息。, c% b/ f6 K6 W4 _3 Q) p; D! ^/ a
$server = "localhost"; //你的服务器$ c6 v f0 [9 D- i% C2 `
$db_user = "root"; //你的mysql的用户名
9 S6 Z% B0 e( O$db_pass = "password"; //你的mysql的密码) r. ], s4 q- x; T6 `) I
$database = "users"; //表的名字设置统计的时间(多少秒内在线人数)
+ i$ t! [) W# g) u$timeoutseconds = 300;取当前时间。
! p: E- V" X. t, f ^7 c8 g. m* d$timestamp = time();上面的完整代码:
8 A$ W3 R/ w$ _" a( O<?php9 l" w3 M/ E" }6 i0 \% ^8 {
$server = "localhost"; //your server& f0 `/ ]4 E: U7 i' x' j2 e$ l) {2 q
$db_user = "root"; //your mysql database username
2 C; `% h" u F5 C& S$db_pass = "password"; //your mysql database password if any
) G; T# b5 i' N4 ^. B; n/ N/ h/ G& a$database = "users"; //the db name
: z3 k7 D; o7 j4 h3 y! d$ z$timeoutseconds = 300;//timeoutseconds limit
3 E. e& b7 \$ `" F& Y u o0 ~//get the current time
0 ^" y1 F- k* g1 Y) i/ F' d; _$timestamp = time();
2 Q4 Y( W& H7 A) y% _/ \//calculate the lowest timestamp allowed
* Y7 G$ Y3 \7 d$ y5 `9 g$timeout = $timestamp-$timeoutseconds;( D6 V$ r: ]* Z' |0 W& c; p! \3 M5 g
?>连接mysql3 t; d @8 R3 k
mysql_connect('localhost', 'username', 'password');也允许使用变量形式。
2 P; l: N8 n6 i7 `+ C s, Wmysql_connect($server, $db_user, $db_pass);如果mysql数据库没有密码的话可以使用下面代码连接
" ^. I2 `/ _* @6 U) f, ymysql_connect($server, $db_user);查询数据库的代码:- h+ w' l' `7 p1 U6 C
mysql_db_query('database', 'query');我们只要有访客就要增加一条记录。
$ J1 V9 f" J+ i) u* |3 g$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
- R7 V; f% g# Q9 O4 S) p5 K- ~('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");0 A8 T9 X" p8 R; k/ f& _
如果用户用错误信息的话,这样处理。; L* C0 n5 y0 G
if(!($insert)) {
+ ]9 V f4 s ]print "Useronline Insert Failed > "; T3 D' a1 P7 g# U$ z# u; y* u r
}然后实现当超过设置的时间就删除该用户记录。
3 e* g& `$ ?9 W6 I2 x6 ]8 |$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");同样给出删除记录出错的处理。
9 r5 ~2 D6 A6 C9 e$ Gif(!($delete)) {
" r& d- k; u0 a* n8 S" X a+ tprint "Useronline Delete Failed > ";. l7 g& q- M& E) t) S. U
}下面我们解决数据库中不同IP的问题
; X7 M$ A, G, t& t$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");使用
% A5 t1 e1 l1 L Cmysql_num_rows(query);来统计用户,代码如下。* G$ C' t* F3 x0 h
$user = mysql_num_rows($result);最后关闭数据库。
! d1 Q: y0 B- V7 \, k1 Y4 P3 U: Jmysql_close();显示在线的人数。
6 K/ U# p) _2 E' U4 J6 s. m4 aif($user == 1) {. X- M1 q& C5 m j f* b
print("1 user online\n");3 M& b7 X4 D# `4 m, j" Y! U
} else {% M! [9 F4 x8 z3 @' b
print("$user users online\n");
) v% l& L2 ]9 u" f5 r8 _}最终把上面代码写成一个PHP文件如下。( T6 s/ X* s& P# i
<?php
; Y: G' A$ T- T* F# A//Put your basic server info here
7 A" X5 J, j8 M6 y$server = "localhost"; //normally localhost
6 Q: e8 P2 b8 J, L. g8 v. t; J$db_user = "root"; //your MySQL database username& |% K% i6 n) S' I* y6 f. i
$db_pass = "password"; //your MySQL database password# v* \; ?; G. U. j" O; `
$database = "users";0 `$ _8 U6 p9 y. r% X, g
$timeoutseconds = 300; //it will delete all people which haven't refreshed(so probbably are, V1 j( Z) Y2 l
// offline or inactive) in $timieoutseconds time (so it actually checks the people that are active in the last# g8 I( U4 s' |! o: x) ^2 U- S
// $timeoutseconds seconds)
9 N! C# l) Y6 r# U4 E7 J* M, K//this is where PHP gets the time. G' { H2 ^! ~5 ~+ a: F
$timestamp = time();, n, i$ l! B3 I
//counts the timeout, all people which have been seen last online in earlier than this timestamp, will get removed, k3 h+ K6 L. A- ]* E; M) J! V
$timeout = $timestamp-$timeoutseconds;# N2 ~ a2 P" p: ?) l
//connect to database8 c- R" [6 N" {! H: {& ?8 \4 M
mysql_connect($server, $db_user);
* F, o" x9 P6 R9 ^! h h) }4 Y9 B//add the timestamp from the user to the online list7 o4 c' [1 ]& B. }2 ~' _: c
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
' j7 |# `: a8 x('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");' v7 B; g2 j t. h5 y6 V) j0 R4 Y6 Z
if(!($insert)) {
& ^6 a/ ^1 m9 D" q+ Sprint "Useronline Insert Failed > ";
* `1 c2 a7 ], l" x# e7 \/ C} p7 A. N2 H6 v) U
//delete the peoples which haven't been online/active in the last $timeoutseconds seconds.5 i0 P. \0 @% b! t: S
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");
1 y, c P) T. x' c' Bif(!($delete)) {
9 P5 g. D* Y1 c& ]6 |% d0 D; @$ nprint "Useronline Delete Failed > ";( A" t. a1 q8 b r" X
}
+ Q& }) o8 o# u7 T//select the amount of people online, all uniques, which are online on THIS page. E2 f' m4 g- h1 t1 N
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");& F% T* G. O8 a) O3 m# m9 X
if(!($result)) {
# r8 h- t# j$ M. K( Lprint "Useronline Select Error > ";
! {5 t$ h# R5 N1 k' y" o}# Y6 B* n& H; Y( x/ ?) j
//Count the number of rows = the number of people online7 ~7 u' Q; W4 G$ K. |" h
$user = mysql_num_rows($result);' M( N0 z1 a' S1 F9 w
//spit out the results9 d3 s$ j. z; a7 [
mysql_close();
' a9 h1 Y8 r2 H1 P% t" g7 ^4 @if($user == 1) { b( o# I# }8 ]+ v
print("1 user online\n");
; X* n* A: L7 s/ A3 c& Y3 y5 M} else {
8 I; M7 I4 D0 N4 vprint("$user users online\n");4 V b& L- h( ~! P' Z) X5 g
}% x+ O2 c; p$ ~
?>+ R8 F$ M" W8 o5 U) V

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