标题:
刚用PHP写了一个网站在线人数的程序,请大家进来指点下!
[打印本页]
作者:
lilcy88
时间:
2008-5-28 10:40
标题:
刚用PHP写了一个网站在线人数的程序,请大家进来指点下!
刚用PHP写了一个网站在线人数的程序,请大家进来指点下!
! y2 g7 {7 z, L; c7 ?- D3 R: [! A
我是用PHP+MYSQL来写的,原理:网站在线人数的程序代码+后台有MYSQL数据库支持,可以直接统计出网站当前的在线人数。
% ^# @0 v! ~5 S/ ^9 A4 k7 {
首先我创建MYSQL数据库表。
% ~1 G7 t2 e. |, f) g2 U
CREATE TABLE tablename (
% g, w/ k3 } c& j7 x$ J5 U
field type(max_length) DEFAULT 'default_value' (NOT) NULL
$ m6 F+ P) ?* l
}可以使用的SQL语句。
$ a- m5 C; v: B. [; K
CREATE TABLE useronline (
/ U$ l+ Z6 i0 J, ^+ g2 G
timestamp int(15) DEFAULT '0' NOT NULL,
( o% r" Q2 [- y# q( |9 v
ip varchar(40) NOT NULL,
* V- `+ O {' n9 D
file varchar(100) NOT NULL,
3 V) Z; ?4 a) B1 m+ ?
PRIMARY KEY (timestamp),
" [' x* A L0 I
KEY ip (ip),
. ^5 l: R, F" u& N+ t' Z
KEY file (file)
8 K3 Q; ?2 K0 z5 ?2 `
);下面我们是PHP脚本,首先我定义MYSQL的信息。
( |5 `) }) [3 r* W- f( |9 j- s& e
$server = "localhost"; //你的服务器
0 r$ E6 M8 e8 c/ s! c# M) N
$db_user = "root"; //你的mysql的用户名
" R0 Q) V S& \4 V4 L5 E: m3 r- {$ G
$db_pass = "password"; //你的mysql的密码
( }" n& O$ l2 w5 W( {0 s
$database = "users"; //表的名字设置统计的时间(多少秒内在线人数)
) u! i; P, y+ X0 ]& ?
$timeoutseconds = 300;取当前时间。
3 c+ W$ ]" I4 L. r; b
$timestamp = time();上面的完整代码:
* w: u/ p3 ~" f) y' }$ M% K
<?php
2 h6 L; p8 ]2 y1 e& ^+ W
$server = "localhost"; //your server
( E! S8 o, r. V1 N8 R- V. [
$db_user = "root"; //your mysql database username
4 v. n5 f9 \: _$ O
$db_pass = "password"; //your mysql database password if any
8 @* R! i6 X! Z( e: S! z7 A' _
$database = "users"; //the db name
4 w- i4 T, x" A; x8 Z2 ~9 j
$timeoutseconds = 300;//timeoutseconds limit
6 G; q7 K& {$ j8 \" S
//get the current time
& x1 f5 A1 F7 U2 ^. @+ U# n* U
$timestamp = time();
, e9 h: c( o( l: p- ~
//calculate the lowest timestamp allowed
% R1 }2 m5 `+ q/ d1 E' N6 ] x
$timeout = $timestamp-$timeoutseconds;
- U$ A4 R' W2 y6 G O
?>连接mysql
4 m; h Y0 E. G$ s9 C4 N
mysql_connect('localhost', 'username', 'password');也允许使用变量形式。
3 s. ^4 @8 ?% q& E7 e- w) P! \
mysql_connect($server, $db_user, $db_pass);如果mysql数据库没有密码的话可以使用下面代码连接
" p1 E" }+ H2 e( a3 I; n5 Q
mysql_connect($server, $db_user);查询数据库的代码:
b" O3 y. ?+ N; H3 e7 |
mysql_db_query('database', 'query');我们只要有访客就要增加一条记录。
@" f. w! U L/ l, J' U* ? V4 i
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
% {& A5 u& R" I+ Y- a- s! a
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
8 J0 a4 n$ f; n% m3 E# y% d
如果用户用错误信息的话,这样处理。
]3 c' B+ U0 Y3 N: C' T9 o6 u. o
if(!($insert)) {
$ z& _7 @( F$ ^8 R( x
print "Useronline Insert Failed > ";
2 {* r% s+ @. g8 [, r' Y5 h
}然后实现当超过设置的时间就删除该用户记录。
9 @; J5 a o9 v3 ]
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");同样给出删除记录出错的处理。
( n8 K0 h ~+ f# q
if(!($delete)) {
% R, V) Y' w* w9 M
print "Useronline Delete Failed > ";
& l; S2 h! r8 W4 c
}下面我们解决数据库中不同IP的问题
' T- @- @ q4 c5 u
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");使用
5 L0 \; h8 _' A; T* E' t
mysql_num_rows(query);来统计用户,代码如下。
3 N5 r& t1 V1 I" x4 K+ ?
$user = mysql_num_rows($result);最后关闭数据库。
- r% J& |0 \" A2 I: \
mysql_close();显示在线的人数。
0 ~8 L0 Q' E! J- z+ M1 K
if($user == 1) {
3 k% m9 I; Y! U. E/ |
print("1 user online\n");
( g* t$ }8 L+ e
} else {
( R5 N6 e3 c3 F8 v
print("$user users online\n");
0 _7 A. D W) U% q# k4 S9 v' y" \( {
}最终把上面代码写成一个PHP文件如下。
( C6 }+ V. d" ~
<?php
: R8 D" g A$ D+ _! U* o* |# b
//Put your basic server info here
$ D, }2 F; L9 e' c( b3 O. J
$server = "localhost"; //normally localhost
$ \( Z2 f1 b9 |# D
$db_user = "root"; //your MySQL database username
" h/ @% W. M% w ?4 B5 o
$db_pass = "password"; //your MySQL database password
2 h' g* F9 ?% O) K1 a& W4 l& x
$database = "users";
0 v' w3 Y3 `0 e, ~1 x# G
$timeoutseconds = 300; //it will delete all people which haven't refreshed(so probbably are
~% [* a1 K: F6 c! i
// offline or inactive) in $timieoutseconds time (so it actually checks the people that are active in the last
, T9 n; n, k2 d. M
// $timeoutseconds seconds)
2 n+ h% n; c$ J! d/ e s$ G& x* X$ q
//this is where PHP gets the time
% U g# N0 Q( k* L6 n, r
$timestamp = time();
+ E0 X3 x5 l; p; r
//counts the timeout, all people which have been seen last online in earlier than this timestamp, will get removed
1 d" {- b9 V% f
$timeout = $timestamp-$timeoutseconds;
1 H: g0 ]0 L. ?, P: b) L) |
//connect to database
$ y* {0 g. S; A& Y" I; G
mysql_connect($server, $db_user);
- O1 r! @2 k3 w# l |1 w
//add the timestamp from the user to the online list
$ k% e8 o' y% g+ S1 @
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
$ Y9 `8 [/ D- Q" L1 N9 c7 P
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
- T6 A6 ?7 N- V3 ?& z s
if(!($insert)) {
/ e' s" D; g' l* ?- B4 F
print "Useronline Insert Failed > ";
9 |. ^/ A( E) H3 e* B0 H
}
& p9 O$ f* |) j
//delete the peoples which haven't been online/active in the last $timeoutseconds seconds.
' g. D* P! U# o8 X, D9 W
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");
6 C- G" J% V, t& f- H$ |. N
if(!($delete)) {
3 r( Z" e& }6 q
print "Useronline Delete Failed > ";
8 w" A" V% G% ?7 X; S3 g
}
+ C. a, [; f( B/ [5 d* R3 s2 ?
//select the amount of people online, all uniques, which are online on THIS page
3 J9 O* o* V9 A+ i) u$ v0 }" n
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");
, n2 L4 V+ r: i+ m4 Q: l
if(!($result)) {
- l8 y t8 g& Z8 {) C, l
print "Useronline Select Error > ";
0 T, r3 B7 N6 B) @
}
& Z- x) K) N; K) ^, N5 P9 z
//Count the number of rows = the number of people online
7 R" D8 s+ }5 R$ a6 d! z5 P6 }
$user = mysql_num_rows($result);
, s6 {( @6 i! p6 N/ s8 v
//spit out the results
, j' W" L) p+ H& R Q
mysql_close();
* E4 f6 D. Y' C5 o
if($user == 1) {
. P* A6 W! B. m' }
print("1 user online\n");
( s3 G* E$ y$ u
} else {
' U' S9 u' J. L% `
print("$user users online\n");
4 J6 V, Y4 |5 t6 U5 ]: _0 h) @
}
/ r8 T3 T# W- ?6 M2 q; A
?>
1 f7 p) l! Q6 Z# S
$ \- p& ^/ K0 q' B0 V
以上代码我是在时代互联提供的免费试用主机上测试的,感觉效果还不错,性能很稳定,不信的话你也可以去试试。当然,记得指出我这个程序中的不足先哦。
4 g$ A; x e6 B) v
时代互联是行业内最早开始提供PHP 虚拟主机的公司之一,现在已经是的金牌域名注册商和中国互联网客户满意度十佳单位,产品功能已经非常完善 ,特别提一下几个特有用的功能:数据库自动备份、免费集成繁简通简装版、多域名绑定、多子网站支持、GCDN 网站加速器、镜像加速、高级访问统计系统、支持WAP、可选电信/网通/香港机房。
b3 ?* h/ m- F3 l; m; Y5 D3 _
我之前建站的时候选的是他们公司标准商务E型,因为这款机型还特别支持ASP.NET3.5(3.0/2.0/1.1)/ASP。
' \" r4 ]- |8 G2 n3 Y
当然啦,这两款主机也是相当不错的。
/ F/ }: _% J, P! W
智强商务B型:Unix /Linux 操作系统+独立网页空间1000MB+送1000MB邮局空间+100MB MySQL数据库空间=1869元/年
4 j1 K* ]4 H3 s7 X( m1 R
标准商务B型:Unix /Linux 操作系统+独立网页空间500MB+送800MB邮局空间+60MB MySQL数据库空间=1365元/年
5 H0 j& Z" T) T; ~
提供一下这个公司的联系方式:请见:
http://www.now.cn/vhost/
/ c. z: a$ c' e, P6 Y
空间专线: 0756-2623871 QQ:168075865,好象还有全球免费网络电话:全球免费咨询电话
http://www.now.cn/callcenter/call.net?LineName=55
% N4 }2 `' }: X. D
自己加QQ去问吧。
欢迎光临 捌玖网络工作室 (http://89w.org/)
Powered by Discuz! 7.2