Board logo

标题: 刚用PHP写了一个网站在线人数的程序,请大家进来指点下! [打印本页]

作者: lilcy88    时间: 2008-5-28 10:40     标题: 刚用PHP写了一个网站在线人数的程序,请大家进来指点下!

刚用PHP写了一个网站在线人数的程序,请大家进来指点下!1 _# l3 y  J# \1 _: k
我是用PHP+MYSQL来写的,原理:网站在线人数的程序代码+后台有MYSQL数据库支持,可以直接统计出网站当前的在线人数。$ i& x6 ~+ }- {4 U5 ~
首先我创建MYSQL数据库表。
5 Q& \* L; M0 _) `CREATE TABLE tablename (
1 F$ n/ u( {0 @4 h, Rfield type(max_length) DEFAULT 'default_value' (NOT) NULL
3 J; S' ~7 L2 A1 G$ {! \+ h}可以使用的SQL语句。% n  o5 P. t( B) ~
CREATE TABLE useronline (
8 i) u$ h3 G7 S; {timestamp int(15) DEFAULT '0' NOT NULL,* }  l# x! u: k+ x+ T
ip varchar(40) NOT NULL,% I, Z" [5 l; E0 W& L
file varchar(100) NOT NULL,$ w  z: w! T* K+ L6 i; _/ i6 G5 U* L# s) z
PRIMARY KEY (timestamp),
9 m* p' Y% d2 ^: pKEY ip (ip),
1 {$ t0 Y$ R! q* e) ]1 AKEY file (file)0 Y; W! ^' j% V7 n
);下面我们是PHP脚本,首先我定义MYSQL的信息。2 C% N7 e3 s3 B; O4 N
$server = "localhost"; //你的服务器
. `+ b8 c$ B5 ^  ?$db_user = "root"; //你的mysql的用户名( k4 T. {8 A2 f! X
$db_pass = "password"; //你的mysql的密码, y4 b8 l7 _! {9 D, l
$database = "users"; //表的名字设置统计的时间(多少秒内在线人数)
+ b8 u# _0 [' l5 J$timeoutseconds = 300;取当前时间。4 A: b& M: L( H& S: L$ }7 F
$timestamp = time();上面的完整代码:
  C) J$ r; j$ `% X8 [. u5 a<?php! Z$ T1 y4 d4 S0 w7 X8 a8 m# M
$server = "localhost"; //your server  a, Y$ d, c. u% P+ L2 @8 r
$db_user = "root"; //your mysql database username' \$ |3 [0 [5 v$ t0 c9 B
$db_pass = "password"; //your mysql database password if any
0 ]4 ?1 t5 k7 u9 u$database = "users"; //the db name. p. \+ H+ _! g" r! T& Y8 r
$timeoutseconds = 300;//timeoutseconds limit/ Y4 R- s% F- A/ u/ r
//get the current time
/ t  _, c# w! y, O$timestamp = time();
- r2 b4 L6 |4 L( v) \: H//calculate the lowest timestamp allowed3 h- f( C3 ^; ^
$timeout = $timestamp-$timeoutseconds;; t6 o! {$ _7 [$ ]9 m( e: A% w
?>连接mysql
: E$ g, |; e3 O, B1 I( v1 \, t2 [( E; lmysql_connect('localhost', 'username', 'password');也允许使用变量形式。3 I# _  {' i. h! x$ g" f
mysql_connect($server, $db_user, $db_pass);如果mysql数据库没有密码的话可以使用下面代码连接
8 s6 g) \; u) D7 Dmysql_connect($server, $db_user);查询数据库的代码:7 ~( r) x2 f1 o, k2 ?6 c* h, b
mysql_db_query('database', 'query');我们只要有访客就要增加一条记录。/ @3 `+ O1 r( h2 S, Y6 H: d: F
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES3 L, S1 ]- `; w  M: T
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
% u* M& b& w4 e  t# D: Y如果用户用错误信息的话,这样处理。( l+ J) j- q! f1 l% g  {7 r0 M  @$ S
if(!($insert)) {5 H# o+ O, L/ Q# P8 k; W8 v
print "Useronline Insert Failed > ";6 H" ^4 @2 y; ?& E; o2 H' G
}然后实现当超过设置的时间就删除该用户记录。
1 B5 p; H5 R4 A( O6 T2 [+ `1 a$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");同样给出删除记录出错的处理。
7 p. N$ t& u$ j! w9 H6 A4 p  t: Y7 j: Pif(!($delete)) {
& m/ {. B. {+ W2 H9 mprint "Useronline Delete Failed > ";
1 ~/ X5 J1 T9 q7 V# S4 c}下面我们解决数据库中不同IP的问题0 e/ }: E9 E" {
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");使用
+ `6 c/ o! D/ V) u  P+ P0 dmysql_num_rows(query);来统计用户,代码如下。
  c* G$ y) P% E) ~6 Q4 y$user = mysql_num_rows($result);最后关闭数据库。
& s7 l) x$ \5 T2 o6 y% S6 l$ A- {( vmysql_close();显示在线的人数。
9 R/ ^. C" G7 E- hif($user == 1) {
- e9 f, o3 J- T6 m5 _6 oprint("1 user online\n");: W" u4 E' R& ~( P2 `
} else {
$ W! V+ R" l: x, ?4 z6 vprint("$user users online\n");
) S! [3 }. T6 D  M' t! \}最终把上面代码写成一个PHP文件如下。
" o* T$ y/ ^" ]* L3 [<?php
5 U! e3 _& \7 ~4 X- T//Put your basic server info here( w+ U( ?, Q! b+ Q1 K
$server = "localhost"; //normally localhost
( x; d8 ?* c5 E8 [9 B3 F$db_user = "root"; //your MySQL database username# b! H1 I6 g' T$ G! }; A% ]
$db_pass = "password"; //your MySQL database password
- |# ^' B  o: Y5 q3 j+ ~$database = "users";
% J3 u9 g% T1 o7 _7 T- o+ g' [$timeoutseconds = 300; //it will delete all people which haven't refreshed(so probbably are, U7 \& d" ~" L4 L1 s7 {' S2 ?
// offline or inactive) in $timieoutseconds time (so it actually checks the people that are active in the last% N+ W6 L) S! I  z
// $timeoutseconds seconds)
: w, t9 G( u  w8 h/ j2 t" m//this is where PHP gets the time% _! r. Y5 \/ R2 d' T8 G1 D
$timestamp = time();9 C' Z( U4 F- m1 \+ [' ~- ]
//counts the timeout, all people which have been seen last online in earlier than this timestamp, will get removed
7 P# |& m* M5 L1 ^$timeout = $timestamp-$timeoutseconds;
' A  t" i2 `/ l//connect to database
+ w9 d9 E7 n0 s. k  G! ?0 Rmysql_connect($server, $db_user);4 f$ i3 ?% G3 h
//add the timestamp from the user to the online list: P' j- a$ f  R2 ?
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
: \% B6 X$ p0 ]1 y('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");! u- @, e) G7 A) `- _* U( l
if(!($insert)) {
! {: f$ b$ e% @! m1 ]1 Dprint "Useronline Insert Failed > ";$ ^* H* G; m6 x
}6 c7 Y& T8 W/ b/ e9 A! ?) y! B, g
//delete the peoples which haven't been online/active in the last $timeoutseconds seconds.
" ]5 O; O, g: y$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");' W  ]3 j6 ~8 q& _8 }
if(!($delete)) {. c, p8 \' O5 h* u/ o4 @- f8 Q
print "Useronline Delete Failed > ";
* o$ `0 K$ l# V  [: @% y1 f}
8 [  [: P- P2 U5 j. A! y  a( U//select the amount of people online, all uniques, which are online on THIS page' ?/ n1 B0 M4 W! I' [' s# N; ~) J" Z
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");: B/ r# v! x4 o4 H3 P# e: b
if(!($result)) {2 |7 z' X5 R/ U0 y+ {% K! Z; g0 S! C
print "Useronline Select Error > ";
' a' O% W  C+ X, V, j! |}, f: N* |1 \9 E
//Count the number of rows = the number of people online
" b8 ^' U& p% }" L; d7 R$user = mysql_num_rows($result);% K* o. e: o- L/ s  z
//spit out the results3 r9 k: N0 E  T  h2 e/ n0 V+ T1 P. X
mysql_close();# a% v. E4 a; L/ w. o, ]: t$ Z
if($user == 1) {, G: s+ v- S5 y6 X# E7 a. H
print("1 user online\n");) \8 \3 L' Z+ f! H
} else {! l- j9 k; P& `8 k, V! O# }) c
print("$user users online\n");% h7 [+ c! L3 Q0 i0 @6 ^
}! a$ N+ V. Z0 F6 F7 k. B3 e) `& Q; P
?>: g. G0 R7 L& g4 a- p

3 H& b! v7 D) B& Z以上代码我是在时代互联提供的免费试用主机上测试的,感觉效果还不错,性能很稳定,不信的话你也可以去试试。当然,记得指出我这个程序中的不足先哦。
( _/ f& n) K& l8 O9 l时代互联是行业内最早开始提供PHP 虚拟主机的公司之一,现在已经是的金牌域名注册商和中国互联网客户满意度十佳单位,产品功能已经非常完善 ,特别提一下几个特有用的功能:数据库自动备份、免费集成繁简通简装版、多域名绑定、多子网站支持、GCDN 网站加速器、镜像加速、高级访问统计系统、支持WAP、可选电信/网通/香港机房。+ D, [5 ]% n  T& t$ x
我之前建站的时候选的是他们公司标准商务E型,因为这款机型还特别支持ASP.NET3.5(3.0/2.0/1.1)/ASP。2 s( N2 O; s+ U% {% [
当然啦,这两款主机也是相当不错的。
  m) n8 C3 }7 S: J6 z智强商务B型:Unix /Linux 操作系统+独立网页空间1000MB+送1000MB邮局空间+100MB MySQL数据库空间=1869元/年
7 x8 n8 [5 R- [" j& C$ E9 p2 P标准商务B型:Unix /Linux 操作系统+独立网页空间500MB+送800MB邮局空间+60MB MySQL数据库空间=1365元/年; E0 w7 n) @8 G& ~
提供一下这个公司的联系方式:请见:http://www.now.cn/vhost/ $ c. |8 I( C1 I
空间专线: 0756-2623871  QQ:168075865,好象还有全球免费网络电话:全球免费咨询电话 http://www.now.cn/callcenter/call.net?LineName=553 ?# @% c. W$ k1 |$ i: Y
自己加QQ去问吧。




欢迎光临 捌玖网络工作室 (http://www.89w.org/) Powered by Discuz! 7.2