获得本站免费赞助空间请点这里
返回列表 发帖

刚用PHP写了一个网站在线人数的程序,请大家进来指点下!

刚用PHP写了一个网站在线人数的程序,请大家进来指点下!
. S5 h# `; V/ V2 [+ L我是用PHP+MYSQL来写的,原理:网站在线人数的程序代码+后台有MYSQL数据库支持,可以直接统计出网站当前的在线人数。
" F1 E1 D) T4 K3 K8 f首先我创建MYSQL数据库表。7 y/ u# z- [* G, \) ?; ^( e) t# `
CREATE TABLE tablename (6 w" q, X$ K2 p1 I+ Y, |/ f
field type(max_length) DEFAULT 'default_value' (NOT) NULL! e3 P6 g  c0 V' |, u0 @6 \
}可以使用的SQL语句。
2 H4 E% Y, h3 ?. [CREATE TABLE useronline (' m) G+ }- B) t; P
timestamp int(15) DEFAULT '0' NOT NULL,, W5 D9 o: a6 M3 T
ip varchar(40) NOT NULL,
* _4 I( ?3 R1 L, E& Y& R; b8 Bfile varchar(100) NOT NULL,5 C  M+ q/ [# t$ R% S
PRIMARY KEY (timestamp),, W+ \- y% t5 a: ]% F; [
KEY ip (ip),6 x/ m, \3 w% T) O3 \4 ^! v. l
KEY file (file)) u# ?* N; c& S5 e3 W% o
);下面我们是PHP脚本,首先我定义MYSQL的信息。$ b% Z& _4 ?/ \/ r8 A( l8 Q
$server = "localhost"; //你的服务器
& e5 a/ I! @  z  {7 v  s1 v$db_user = "root"; //你的mysql的用户名
3 c; u4 l, i! e$db_pass = "password"; //你的mysql的密码
; R' S7 v: z( h4 e$database = "users"; //表的名字设置统计的时间(多少秒内在线人数)! ^4 U7 ]5 U9 Y, s4 D! l. e
$timeoutseconds = 300;取当前时间。  Z+ W7 o+ l6 W5 C$ h
$timestamp = time();上面的完整代码:9 u& k1 I1 d1 z- L+ G
<?php
3 a6 X, j- M" U3 {; o6 Y% |: I$server = "localhost"; //your server) b. k! l3 ^7 l9 _0 M; O
$db_user = "root"; //your mysql database username' Q3 n5 X. b, A+ c. r9 B
$db_pass = "password"; //your mysql database password if any( @3 m/ B& V3 H& [; d
$database = "users"; //the db name
( o7 \7 F* n0 U) r0 [$timeoutseconds = 300;//timeoutseconds limit
( i1 u! Z6 Y4 q+ S//get the current time; R# e, Q& A* r( Q2 w& |2 {5 w- U
$timestamp = time();+ L! I: E1 y/ [* ~$ H
//calculate the lowest timestamp allowed
/ k8 {6 r0 p5 T0 [: D1 r$timeout = $timestamp-$timeoutseconds;; t' H+ P* N8 \1 E) L% u4 ^( K
?>连接mysql9 o: W1 L/ a7 p
mysql_connect('localhost', 'username', 'password');也允许使用变量形式。
! Q  U0 k0 s( k" f5 Nmysql_connect($server, $db_user, $db_pass);如果mysql数据库没有密码的话可以使用下面代码连接; k5 z+ Z3 @# \) c1 ^  w
mysql_connect($server, $db_user);查询数据库的代码:
5 V8 ?' g% R: U# b- h* mmysql_db_query('database', 'query');我们只要有访客就要增加一条记录。$ X* \# D! d+ ^: c$ p% ]
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
. P2 Z/ R. Z% m- c, r4 ]- J; {; U('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");2 U8 J& Y# r1 r( u. c0 u2 n+ b, R
如果用户用错误信息的话,这样处理。. `2 _" o* w) v/ M
if(!($insert)) {: {9 D6 ]) o, [2 v- K0 u
print "Useronline Insert Failed > ";
; v# K  W/ Q2 l, G( [. a6 l" Q( T}然后实现当超过设置的时间就删除该用户记录。% G6 s! p3 |' z. ^& G5 h, K4 Z. V
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");同样给出删除记录出错的处理。/ V$ n8 G: |' a
if(!($delete)) {
9 A3 Z* }- B& h6 z% Xprint "Useronline Delete Failed > ";$ h& }" R3 x! h8 p; I* O. a
}下面我们解决数据库中不同IP的问题
8 |& q; r8 V# F0 l& r$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");使用
& c, H" o) W$ F1 Y# umysql_num_rows(query);来统计用户,代码如下。
# K* W  M; ?! e$user = mysql_num_rows($result);最后关闭数据库。3 a" X: |4 z; ?+ [9 V
mysql_close();显示在线的人数。
  p- ^" @& _5 y6 o7 Z' I$ Hif($user == 1) {
3 h0 m' g# g% Y8 jprint("1 user online\n");* F/ U: t% B8 Y8 H
} else {" h2 \+ H  _+ _0 {8 U' B: g
print("$user users online\n");
- W. y" v7 u  j) Y1 h}最终把上面代码写成一个PHP文件如下。! q0 \. p" Y/ [$ W5 W% O& t: I) k
<?php
( u; f$ Z  ^4 y//Put your basic server info here
3 R7 A, _. g# u3 E8 U$server = "localhost"; //normally localhost# k6 U, o. G- q/ U- {
$db_user = "root"; //your MySQL database username  t; E# L3 E& m& v/ V$ j
$db_pass = "password"; //your MySQL database password
3 |' F" Z( _# P6 n( N$ B! J$database = "users";
5 F* a, o( [0 @+ b. j0 V( q" q" b5 z$timeoutseconds = 300; //it will delete all people which haven't refreshed(so probbably are# m! u* F9 n# g5 I( K
// offline or inactive) in $timieoutseconds time (so it actually checks the people that are active in the last9 }7 f$ F6 ~( _! g* U8 O7 S
// $timeoutseconds seconds)
) l3 D7 M, \+ o8 }$ L3 K" c//this is where PHP gets the time. N7 ^0 f4 Y3 U/ ^* w. ~( P
$timestamp = time();, @0 B$ m- F( c# Z" Y! V
//counts the timeout, all people which have been seen last online in earlier than this timestamp, will get removed3 ]$ ~; i* j; `0 I
$timeout = $timestamp-$timeoutseconds;# G2 R1 Q7 W; _! Y
//connect to database# O4 U* Y' _' _( t0 [& w
mysql_connect($server, $db_user);
9 U6 }9 A2 I) j//add the timestamp from the user to the online list7 K, _. b8 \$ G3 S- [7 p& c
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES1 S8 P8 }7 }  ]( n
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
" w* \0 E4 H" ?if(!($insert)) {
+ e8 T+ z( S9 L7 Vprint "Useronline Insert Failed > ";
) H7 H' A9 A+ y# K6 J}, T- K8 F, C7 f( Y2 h6 D/ q; y2 o8 s3 ~
//delete the peoples which haven't been online/active in the last $timeoutseconds seconds.
1 B* R& N  f  J+ {1 ~* f8 w) {$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");! O2 v0 ?, E5 j- j
if(!($delete)) {
' x1 V( {. @9 S& tprint "Useronline Delete Failed > ";
6 j9 [. ]: p5 B}2 W5 K' B& K& V' y
//select the amount of people online, all uniques, which are online on THIS page
# d1 w, b3 J$ v3 l  m" m, S6 n$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");5 |% ?$ b4 v7 ~- l* X' R4 M2 D
if(!($result)) {% Z) E- ~+ C8 d! U: D# f8 o
print "Useronline Select Error > ";' [4 w, X$ n* K% m) d2 y5 ]
}
+ j& s5 z3 m  D) q# A//Count the number of rows = the number of people online  f$ M0 L- ]& R& w4 U; Q  g3 M
$user = mysql_num_rows($result);' G$ y/ i# j) {! _% {) E
//spit out the results
1 q+ L% z3 {2 u1 W3 e8 {( V. L; zmysql_close();
% f: h. |, T6 I& f, Wif($user == 1) {
, ~* a8 r' a4 U# ?print("1 user online\n");
% [$ Q8 \+ W/ w8 M2 Z$ q' _, q} else {+ E8 A/ r/ B, s6 O. j; ^
print("$user users online\n");6 I3 E4 K. o* q6 Q
}
7 a7 _1 d: E0 L: A4 {  R. s?>
. t' J0 A" g6 `4 j: x
0 l; p. z) P$ y. F, I以上代码我是在时代互联提供的免费试用主机上测试的,感觉效果还不错,性能很稳定,不信的话你也可以去试试。当然,记得指出我这个程序中的不足先哦。; U$ n- k; r' B0 y0 X5 \* p5 T2 m
时代互联是行业内最早开始提供PHP 虚拟主机的公司之一,现在已经是的金牌域名注册商和中国互联网客户满意度十佳单位,产品功能已经非常完善 ,特别提一下几个特有用的功能:数据库自动备份、免费集成繁简通简装版、多域名绑定、多子网站支持、GCDN 网站加速器、镜像加速、高级访问统计系统、支持WAP、可选电信/网通/香港机房。1 P: j: O: g& o% R7 M; `% q6 N
我之前建站的时候选的是他们公司标准商务E型,因为这款机型还特别支持ASP.NET3.5(3.0/2.0/1.1)/ASP。5 w5 A! j/ [8 X& R  ]' O
当然啦,这两款主机也是相当不错的。  I2 B+ \# ?" i( D4 l* U* `7 b
智强商务B型:Unix /Linux 操作系统+独立网页空间1000MB+送1000MB邮局空间+100MB MySQL数据库空间=1869元/年. F" _, _! Q: U5 _4 d6 G
标准商务B型:Unix /Linux 操作系统+独立网页空间500MB+送800MB邮局空间+60MB MySQL数据库空间=1365元/年. p. w& {8 ^6 V: C, o8 t7 t! Q% [: F
提供一下这个公司的联系方式:请见:http://www.now.cn/vhost/
4 H' Z' }) P: b- P) w" C空间专线: 0756-2623871  QQ:168075865,好象还有全球免费网络电话:全球免费咨询电话 http://www.now.cn/callcenter/call.net?LineName=55
, T' u9 L. L+ t/ J- k4 D& y" [自己加QQ去问吧。

返回列表
【捌玖网络】已经运行: