|

- UID
- 455
- 帖子
- 3
- 精华
- 0
- 积分
- 8
- 金币
- 3
- 威望
- 0
- 贡献
- 0
|
刚用PHP写了一个网站在线人数的程序,请大家进来指点下!
刚用PHP写了一个网站在线人数的程序,请大家进来指点下!
/ I* I, f# m8 h$ p1 n/ B7 e我是用PHP+MYSQL来写的,原理:网站在线人数的程序代码+后台有MYSQL数据库支持,可以直接统计出网站当前的在线人数。
/ l5 p* Z5 S; W" w首先我创建MYSQL数据库表。
% Y6 F. S3 ^/ H/ t6 i! oCREATE TABLE tablename (8 w! E- l9 b+ q/ w! F
field type(max_length) DEFAULT 'default_value' (NOT) NULL
) B5 ]4 z7 O* w: J}可以使用的SQL语句。7 e- l5 f1 _5 t
CREATE TABLE useronline (
1 h7 `2 f' o K% \timestamp int(15) DEFAULT '0' NOT NULL,
1 A1 B0 Y. D6 q [- \ip varchar(40) NOT NULL,
8 }: U% ?6 L7 jfile varchar(100) NOT NULL,
# _& A! W5 v! r7 g) i# N( d9 z8 @PRIMARY KEY (timestamp),
3 g9 n$ B2 V5 t$ z9 HKEY ip (ip),
3 _+ \' O: W# JKEY file (file)
4 G( }3 M" \' g2 C# b);下面我们是PHP脚本,首先我定义MYSQL的信息。
) v/ q$ ?2 R Y1 q$server = "localhost"; //你的服务器8 d% u9 G# r. Q0 a. T6 ]& Y
$db_user = "root"; //你的mysql的用户名% h* T; y) v8 `
$db_pass = "password"; //你的mysql的密码4 [( G" e T( e _; a
$database = "users"; //表的名字设置统计的时间(多少秒内在线人数)* a6 Z' H$ {# d1 p9 k& ^. M
$timeoutseconds = 300;取当前时间。1 E, \# S4 `3 O. u9 G/ t6 l
$timestamp = time();上面的完整代码:
1 |4 X5 p3 R- V4 M<?php
" @" `+ F+ _' H( O- U$server = "localhost"; //your server
6 l i( V+ C$ `8 j$db_user = "root"; //your mysql database username
! ~- U) U" Z8 G/ `% A% I6 ~4 N$db_pass = "password"; //your mysql database password if any3 ^9 U: E% t5 ~% M% Y/ \% ]% \
$database = "users"; //the db name
7 n4 x5 p3 E, P7 S0 X( V$timeoutseconds = 300;//timeoutseconds limit z% z1 W3 i4 [
//get the current time
0 Z1 R/ n9 q5 Q% N K7 ~$timestamp = time();* n" W( X4 M; x5 `
//calculate the lowest timestamp allowed
3 K; J4 W. S& V" R% k$timeout = $timestamp-$timeoutseconds;
4 E7 B; m3 E- U0 X+ V. b?>连接mysql1 u( K- A6 i/ `
mysql_connect('localhost', 'username', 'password');也允许使用变量形式。% o( I) f4 g n; b% r8 ~: ?
mysql_connect($server, $db_user, $db_pass);如果mysql数据库没有密码的话可以使用下面代码连接3 }, k" u% F" L5 U8 M8 m7 C1 l
mysql_connect($server, $db_user);查询数据库的代码:* g, I/ ~) H. e+ I
mysql_db_query('database', 'query');我们只要有访客就要增加一条记录。% ~8 X, r+ K' V: S0 R+ Q
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
6 D; B5 D& } Y('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");! ?9 W b, y6 E1 {
如果用户用错误信息的话,这样处理。8 e4 O; | p4 z! g0 k
if(!($insert)) {. Z7 N; L0 } C% U% u, d. J
print "Useronline Insert Failed > ";
9 E2 n7 W0 c$ D6 ~/ j* u}然后实现当超过设置的时间就删除该用户记录。& D0 C3 h! f+ b7 c7 x
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");同样给出删除记录出错的处理。
6 O7 \5 E) {6 @+ c" K& ^if(!($delete)) {0 ]7 `1 {' ^% w. N/ C; C. y8 K2 t1 N
print "Useronline Delete Failed > ";
( ]- P- {5 f& [}下面我们解决数据库中不同IP的问题
% J1 `2 N1 f8 f% T1 B" G7 U$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");使用8 M5 }1 B, G4 c; q8 p
mysql_num_rows(query);来统计用户,代码如下。' G- s* G2 `- I& i1 B9 } b# V
$user = mysql_num_rows($result);最后关闭数据库。5 h# \$ K8 p' l4 |/ b
mysql_close();显示在线的人数。( m) ]1 F9 I+ h: y) I) X
if($user == 1) {7 g+ Y$ ~+ ]4 b: l' J
print("1 user online\n");6 ]+ O/ X" \" E ^) g6 f
} else {
0 ? b! Q; b- l8 W5 tprint("$user users online\n");
! k: w: S9 [6 r. }1 R0 S. j5 n}最终把上面代码写成一个PHP文件如下。
( r5 K5 m* T. J, p9 B! E# v# e<?php
; N, H5 }7 w( C) g4 ]5 E//Put your basic server info here
, ^3 ~# G/ `5 v6 w9 n$ b: x$server = "localhost"; //normally localhost
p0 L" f7 c5 X4 H" A$db_user = "root"; //your MySQL database username2 D0 ]6 i9 O0 r- i2 H1 ^
$db_pass = "password"; //your MySQL database password6 S r) K% l$ a; m
$database = "users";: U5 A8 b% S( C) P
$timeoutseconds = 300; //it will delete all people which haven't refreshed(so probbably are
k2 M3 t! K( Y- Q7 r// offline or inactive) in $timieoutseconds time (so it actually checks the people that are active in the last! H! s0 Z+ d* o! H5 c
// $timeoutseconds seconds)
6 I: a8 m0 u4 f' @3 M! K//this is where PHP gets the time3 B' `1 j; h& P7 Q/ x! G
$timestamp = time();
) H1 t l; q% M! t% m//counts the timeout, all people which have been seen last online in earlier than this timestamp, will get removed% M; t; U0 A$ _; r% v
$timeout = $timestamp-$timeoutseconds;) B6 }! O0 ~- p' X$ c
//connect to database& F6 s$ l" r9 e! \
mysql_connect($server, $db_user);, |, ^+ ~# n; F
//add the timestamp from the user to the online list2 C1 Q/ k& w' N
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES! e1 P: V6 F# p( M1 u
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");, ^( c. ^3 s8 d7 Y" `# i) A E& e
if(!($insert)) {
. p# _3 h# w- W/ @8 A+ N4 kprint "Useronline Insert Failed > ";
! `4 c5 l6 {) q9 N+ N' F+ U}' @7 A; B/ D+ ^& G
//delete the peoples which haven't been online/active in the last $timeoutseconds seconds.) |3 z% A/ Z. |$ k& b) s }! W
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");. Z: K6 o7 {& c6 x
if(!($delete)) {
! q9 W0 Q8 _4 Q* W$ g# H3 F8 sprint "Useronline Delete Failed > ";7 M0 y; r f% n. n' x: K+ ?: v4 m
}
- ?/ w5 r7 ?: z2 |//select the amount of people online, all uniques, which are online on THIS page: |3 e$ _1 Y, z3 o }* S
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");8 X+ N# C- T# b2 `: o& }# ]
if(!($result)) {$ ]6 s H3 |: Q: h) h
print "Useronline Select Error > ";
( @( |- r/ t% H+ ]4 P}
3 Q$ o, S: E; w/ x3 W//Count the number of rows = the number of people online& u- O( }. p. O" @! c0 x' B' h
$user = mysql_num_rows($result);
& f. S0 B4 E. I7 x* o) ~9 x//spit out the results
2 }9 s) h7 g# G+ o' C( h( g9 ^mysql_close();$ Q& L. r: e5 m1 y5 \
if($user == 1) {0 e$ X3 d& i; D+ r I, o/ t
print("1 user online\n");0 t7 P4 O% ? W/ [
} else {
& F: f0 F; Z& V, M- r" oprint("$user users online\n");
( @- P1 ?0 n! l8 n# ^3 [' b}6 j, n+ G! U) g; {( F) [4 M
?>
) D4 f4 C/ m1 p2 R % e4 p; r1 g4 W
以上代码我是在时代互联提供的免费试用主机上测试的,感觉效果还不错,性能很稳定,不信的话你也可以去试试。当然,记得指出我这个程序中的不足先哦。
) W2 o( j4 N6 v! I2 ~. t, ?3 N: p! R* _ J时代互联是行业内最早开始提供PHP 虚拟主机的公司之一,现在已经是的金牌域名注册商和中国互联网客户满意度十佳单位,产品功能已经非常完善 ,特别提一下几个特有用的功能:数据库自动备份、免费集成繁简通简装版、多域名绑定、多子网站支持、GCDN 网站加速器、镜像加速、高级访问统计系统、支持WAP、可选电信/网通/香港机房。* Y) S8 Q+ t5 w! M' S. w5 V
我之前建站的时候选的是他们公司标准商务E型,因为这款机型还特别支持ASP.NET3.5(3.0/2.0/1.1)/ASP。
8 y# l) v2 {( @% F0 g当然啦,这两款主机也是相当不错的。5 b# c1 x4 u4 c4 L9 ^$ C
智强商务B型:Unix /Linux 操作系统+独立网页空间1000MB+送1000MB邮局空间+100MB MySQL数据库空间=1869元/年, A- [, ]/ M! U& K# a/ O& j
标准商务B型:Unix /Linux 操作系统+独立网页空间500MB+送800MB邮局空间+60MB MySQL数据库空间=1365元/年! ^9 D2 I* I3 l6 D
提供一下这个公司的联系方式:请见:http://www.now.cn/vhost/
* G2 \3 r6 m/ Z& S* u空间专线: 0756-2623871 QQ:168075865,好象还有全球免费网络电话:全球免费咨询电话 http://www.now.cn/callcenter/call.net?LineName=55
8 y" S1 c) y, E* P自己加QQ去问吧。 |
|