|

- UID
- 455
- 帖子
- 3
- 精华
- 0
- 积分
- 8
- 金币
- 3
- 威望
- 0
- 贡献
- 0
|
刚用PHP写了一个网站在线人数的程序,请大家进来指点下!
刚用PHP写了一个网站在线人数的程序,请大家进来指点下!* G3 n3 D! u" [0 i; Z4 ?& e
我是用PHP+MYSQL来写的,原理:网站在线人数的程序代码+后台有MYSQL数据库支持,可以直接统计出网站当前的在线人数。3 Z1 D7 L0 z5 Y: G/ ?! A: K
首先我创建MYSQL数据库表。
( O/ p& r# `5 ] I* ECREATE TABLE tablename (
- M" N; P( m7 B& R/ z- X7 x# Jfield type(max_length) DEFAULT 'default_value' (NOT) NULL
3 g+ F: G) H& S* }( S+ M}可以使用的SQL语句。% l- J8 q8 {# X4 ^6 P0 {
CREATE TABLE useronline (" n- o, g% K; g8 A6 |1 h
timestamp int(15) DEFAULT '0' NOT NULL,
3 ~* W6 z' [% \- N6 J( Nip varchar(40) NOT NULL,2 x) \8 \ `) u/ P, T/ a, D* n( q" E
file varchar(100) NOT NULL,' c: [& Y2 O9 R+ J/ V" c0 J7 k0 x
PRIMARY KEY (timestamp)," _4 f! N) n9 ~( n
KEY ip (ip),
! |' @2 v5 H' W6 Y' HKEY file (file)/ Q4 d+ s& n% n: X
);下面我们是PHP脚本,首先我定义MYSQL的信息。: L; Y7 M1 f0 ]" y" b- s
$server = "localhost"; //你的服务器8 C# i& L5 l$ D, ]! H
$db_user = "root"; //你的mysql的用户名
; ]/ X2 K |% B& Y) P) W2 F9 Z$db_pass = "password"; //你的mysql的密码
( `$ S6 C- ~. c+ W$ M# v$database = "users"; //表的名字设置统计的时间(多少秒内在线人数)# i) q3 `! j* {3 v
$timeoutseconds = 300;取当前时间。
8 H4 T* q; c7 C0 \# u7 F2 a$timestamp = time();上面的完整代码:/ B( D; E, b3 C2 W) f
<?php
9 `) O% f; E; V# j( T$server = "localhost"; //your server7 J7 r* h# n, \* M
$db_user = "root"; //your mysql database username
2 Y( k+ P- L# \1 R" `3 e% k8 ?" @. D$db_pass = "password"; //your mysql database password if any- m2 X- i$ n$ T0 ~3 c5 \+ M, q: K
$database = "users"; //the db name
# x; }$ w; r* g" L8 |! q% y$timeoutseconds = 300;//timeoutseconds limit* J4 e$ s( M B1 W4 u, Y) O, {
//get the current time3 q) ]( x5 L0 ]* M+ q4 Q
$timestamp = time();2 E- r# k; z# @4 m
//calculate the lowest timestamp allowed
0 \" b9 l) U; O' _- |9 Y+ n5 L$timeout = $timestamp-$timeoutseconds;$ {1 ]8 X% K0 h6 X) M9 M
?>连接mysql
& M4 p" s; _: P# A7 dmysql_connect('localhost', 'username', 'password');也允许使用变量形式。
* T) b4 d& |& C! A& Rmysql_connect($server, $db_user, $db_pass);如果mysql数据库没有密码的话可以使用下面代码连接
. O7 K: e# M" x; y+ h1 E* W8 _mysql_connect($server, $db_user);查询数据库的代码:/ |# t9 J5 e2 I* S' E
mysql_db_query('database', 'query');我们只要有访客就要增加一条记录。
, B/ a0 ]3 g {+ s$insert = mysql_db_query($database, "INSERT INTO useronline VALUES9 f( v7 _. Z$ X. w K- g \
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
1 f$ ~8 h! S( O# E6 v2 n+ B+ ?" A如果用户用错误信息的话,这样处理。
" G3 j6 i8 P$ u0 s1 b) ~9 Gif(!($insert)) {+ s& `, M7 s. r% v0 k! ~8 p
print "Useronline Insert Failed > ";+ a2 W$ [& u( z4 r" G
}然后实现当超过设置的时间就删除该用户记录。$ u0 s8 Z: Q$ Y" ^) v. b6 r; h
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");同样给出删除记录出错的处理。8 l! q. H6 T- D% e. H" [+ @
if(!($delete)) {
0 P. E# x2 B% L, U5 a8 M! K; ~) nprint "Useronline Delete Failed > ";& q- Q7 f6 j( N& D
}下面我们解决数据库中不同IP的问题9 R/ I1 s0 y. }6 H) G. o
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");使用
q8 Y5 D) M- m2 bmysql_num_rows(query);来统计用户,代码如下。" S4 ~, z* K5 u: H1 Z" i& S# l1 @
$user = mysql_num_rows($result);最后关闭数据库。# l# X! p& W. E N/ t
mysql_close();显示在线的人数。
- x, t& ?3 i& n* e" X$ hif($user == 1) {
& g5 h% l7 v1 g5 \8 v; |* jprint("1 user online\n");# U, g) Q3 Z$ O) I/ o
} else {
& O/ l, }( G$ W1 g( o" N- Xprint("$user users online\n");
1 f) f3 _5 [- e( a; ]( Z}最终把上面代码写成一个PHP文件如下。2 v% `5 N& T/ o
<?php8 |9 ?+ C. g5 X$ R x4 S3 T
//Put your basic server info here
& f# N8 H3 s# E% Z2 Q$server = "localhost"; //normally localhost3 h# M3 p* m+ ~
$db_user = "root"; //your MySQL database username
; V. m! c, d4 S, ^0 s1 S$db_pass = "password"; //your MySQL database password
. y$ i" d9 q9 y! f9 `* o3 G$ ?8 M6 ]$database = "users";" K; x* f; ?, F+ D5 Q5 n
$timeoutseconds = 300; //it will delete all people which haven't refreshed(so probbably are
0 n- U! I+ @( h, e# n) S4 n// offline or inactive) in $timieoutseconds time (so it actually checks the people that are active in the last
[9 l2 I& V) S/ m! S// $timeoutseconds seconds) q. @; h" Z1 ^7 H' t
//this is where PHP gets the time
9 Y3 Q" g3 j8 o: s$timestamp = time();
/ v4 n. X Y. E2 U//counts the timeout, all people which have been seen last online in earlier than this timestamp, will get removed0 |! t/ v2 E" d3 U. T0 L
$timeout = $timestamp-$timeoutseconds;
) l; H9 x: c d' a7 `- W//connect to database
B. P$ z! U5 Z( q' bmysql_connect($server, $db_user);
& L2 j. f% V$ z+ E//add the timestamp from the user to the online list
2 Q/ A: @0 [" C8 p( D$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
& N0 h' {5 s& j0 r+ S% A7 s('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");* i3 q" A D3 U6 D4 G r
if(!($insert)) {% a! v( }2 }; e6 y% A
print "Useronline Insert Failed > ";
" O5 m5 R5 [9 v}
9 s7 F% \' F0 Q; s: B: o, _//delete the peoples which haven't been online/active in the last $timeoutseconds seconds.
' ~6 r: F0 z$ \( b. J5 j" W$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");
& \1 l6 i' b8 d: x0 zif(!($delete)) {$ H( x8 y2 @# N+ `- Z6 _- [# i
print "Useronline Delete Failed > ";
! s3 l; Y* `3 f* t+ _ { \}
0 u/ p* q( Y4 N' ?( h//select the amount of people online, all uniques, which are online on THIS page
# w6 Y% S% q1 Z1 I% I" G- r$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");
/ z7 ^- J% N7 a1 z6 Q$ A0 s+ `; w1 gif(!($result)) {7 q" M2 |' h# L& Q9 h. W
print "Useronline Select Error > ";
* ^% ]! i& @! R4 x+ Q7 e4 r}
- t% g: H2 J# M5 E, v//Count the number of rows = the number of people online, P2 ?; a5 d; w- x: `2 _" S
$user = mysql_num_rows($result);1 Z5 E- X" {7 L7 s
//spit out the results8 l; ~) m. r! |+ b* ^; }! U
mysql_close();
- V3 m, p9 g( D) W5 Lif($user == 1) {; g& ^8 ~8 L. S" J/ F0 s/ p) q
print("1 user online\n");
" o6 U6 Q7 n1 J' i} else {6 Q- W, P P9 ^" a, p. t( F
print("$user users online\n");
& C8 w0 Z' p$ ~' p1 Q4 Z}+ M# C; o$ r& ^$ Q/ A
?>! ~% h: ?" d2 |6 ^8 x1 g3 _. m8 C

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