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

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

刚用PHP写了一个网站在线人数的程序,请大家进来指点下!6 o4 N& Z/ S0 @# Z6 w( U
我是用PHP+MYSQL来写的,原理:网站在线人数的程序代码+后台有MYSQL数据库支持,可以直接统计出网站当前的在线人数。
" b$ F/ ]1 F, ?1 c! U0 t5 E首先我创建MYSQL数据库表。/ V- m. w7 h7 D. N
CREATE TABLE tablename (
5 R2 T) c" h% h' Qfield type(max_length) DEFAULT 'default_value' (NOT) NULL7 T# a7 L$ o/ h) N& q5 T% {
}可以使用的SQL语句。
6 L# E; e0 e4 O1 `7 N* wCREATE TABLE useronline (! Z, u! U6 u9 P/ _5 a
timestamp int(15) DEFAULT '0' NOT NULL,0 p$ h; L  H4 M" r/ r! h9 u
ip varchar(40) NOT NULL,  ~, O( Z- ]4 @' h
file varchar(100) NOT NULL,
  X: U. w, s0 X& S4 `7 ~PRIMARY KEY (timestamp),4 i+ z, _+ e" f9 C
KEY ip (ip)," Z. O& H+ r) X! Y
KEY file (file)6 d% A; [) a6 w& s4 @7 Q
);下面我们是PHP脚本,首先我定义MYSQL的信息。
. m, W& G; @* t8 L8 k" [$ P$server = "localhost"; //你的服务器7 R3 `. A$ ^2 A4 V
$db_user = "root"; //你的mysql的用户名1 S4 }* F4 s; n4 z. d$ |2 Q
$db_pass = "password"; //你的mysql的密码+ X% U- U2 ^4 B: E; \% J
$database = "users"; //表的名字设置统计的时间(多少秒内在线人数)
/ q7 V  Q& k5 B9 M8 G' p- h$timeoutseconds = 300;取当前时间。
, t: L% z  k! T8 \( X3 D. m$timestamp = time();上面的完整代码:
* w* @" G1 Y* u<?php
# X* X" G& ^- t+ S* M5 {  c1 Y$server = "localhost"; //your server8 j7 Y5 u" m% G. s
$db_user = "root"; //your mysql database username
0 G9 [' d' `7 O2 Y0 E$ [$db_pass = "password"; //your mysql database password if any
, P, Z$ }2 f5 }) Z" w: G$database = "users"; //the db name3 B: p6 @) o9 {+ x4 ]2 |! q
$timeoutseconds = 300;//timeoutseconds limit3 ?/ j8 I# _+ b( I; `) W& i
//get the current time
8 W! x0 t$ e, r) N, W) s$timestamp = time();7 a9 |9 ?, e& ]8 S# u
//calculate the lowest timestamp allowed& D7 j) c( x  p
$timeout = $timestamp-$timeoutseconds;
# Z& D# f! A' _8 I6 W, E?>连接mysql
0 q  @% e- M. W6 N9 `# \mysql_connect('localhost', 'username', 'password');也允许使用变量形式。
9 F; ^4 z+ J/ F# ?3 v0 I6 qmysql_connect($server, $db_user, $db_pass);如果mysql数据库没有密码的话可以使用下面代码连接# l' k* I# t/ d1 q. L
mysql_connect($server, $db_user);查询数据库的代码:6 b( }/ o$ w% X( P% L9 y
mysql_db_query('database', 'query');我们只要有访客就要增加一条记录。
4 [' M- X; U2 g$insert = mysql_db_query($database, "INSERT INTO useronline VALUES4 r0 b  w: L: o; g1 W
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");  L; c+ ]2 p; m8 O3 ~
如果用户用错误信息的话,这样处理。
6 E5 g; m: S" x0 T1 h; ?0 o5 Bif(!($insert)) {6 c0 b- i* y& Y4 v1 \- M, ?( {0 U
print "Useronline Insert Failed > ";
9 N& N+ A' t' k8 I0 W) i: c' u2 L}然后实现当超过设置的时间就删除该用户记录。; H5 x, Z1 r7 N6 k! P
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");同样给出删除记录出错的处理。3 y6 L7 e1 g1 \% V5 U
if(!($delete)) {
% l, l5 h- J8 e8 |% J% Aprint "Useronline Delete Failed > ";) w; {$ J; _0 b5 m' k
}下面我们解决数据库中不同IP的问题: ~) z  e, {/ c  T+ H6 p
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");使用$ ~+ a2 l8 s$ c) w
mysql_num_rows(query);来统计用户,代码如下。
9 Z7 M. F6 k" E8 j$user = mysql_num_rows($result);最后关闭数据库。" y. N; s. u" w, V2 {
mysql_close();显示在线的人数。* t3 |1 V5 E% t# v8 ]
if($user == 1) {
5 T* C" v& Y0 A$ }( \9 t" [% ?print("1 user online\n");' |  j; j- E( I3 ~
} else {; D% r* }, k7 D* F$ j' z4 G( G
print("$user users online\n");
6 j8 H) k: s% M- Q  e}最终把上面代码写成一个PHP文件如下。
; @. W% E* u6 S4 N6 }2 V<?php
0 d" g  O; ]0 K) t$ L//Put your basic server info here: b- x/ |, X6 i' M5 w7 ~) s& i1 h
$server = "localhost"; //normally localhost& e8 L# n8 _1 n0 K! W
$db_user = "root"; //your MySQL database username1 V7 r: N8 @0 d0 D
$db_pass = "password"; //your MySQL database password
# t* ?- W  a- _0 p( J+ b; v. W$database = "users";
2 F% P  a7 d4 }9 k& t1 ~$timeoutseconds = 300; //it will delete all people which haven't refreshed(so probbably are
) V' v+ n4 B8 I& w// offline or inactive) in $timieoutseconds time (so it actually checks the people that are active in the last
! ]. Y8 J) J' j; W# |* v// $timeoutseconds seconds)
8 @- K. {' d* ]//this is where PHP gets the time
0 b" c' B5 b8 J5 d" L$timestamp = time();
5 _( Y. J, g. `: f# P% d* r//counts the timeout, all people which have been seen last online in earlier than this timestamp, will get removed# {' ~- {+ N/ p' N
$timeout = $timestamp-$timeoutseconds;
; r4 x7 g; [: ^4 C3 P//connect to database
9 q, E. r4 g) I' c5 l  ^mysql_connect($server, $db_user);
# l+ d" \8 K' ]/ R& D/ a' Z* D0 A* v//add the timestamp from the user to the online list% g3 r5 k( O/ d8 D% F! ?
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
! f7 `3 U! t! d: t& n" {('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
+ r5 t  Y% L' g/ ^: h  bif(!($insert)) {
2 q2 O6 f# |; pprint "Useronline Insert Failed > ";& r' {/ p1 y! y
}" ?0 M9 B; l: \* b; ~( s
//delete the peoples which haven't been online/active in the last $timeoutseconds seconds.
6 ^/ T# Y8 P" k3 O$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");) F4 \6 t  ]8 O% k: r+ g  |" T8 t. f
if(!($delete)) {& ^- b8 P8 E- H0 x/ L; Q+ {
print "Useronline Delete Failed > ";
; X8 G& r5 f) g}/ o* Z1 D* R/ e7 W  |
//select the amount of people online, all uniques, which are online on THIS page
* H8 q  P% a  G3 I- s$ t$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");
) h' X5 ~5 k; V. d* [if(!($result)) {
0 g  g% \2 p/ s" s# M+ \( pprint "Useronline Select Error > ";
( W  R/ B5 o- i}
8 f0 Y. I. ?+ a, {+ }//Count the number of rows = the number of people online, M* ]5 F6 j8 t
$user = mysql_num_rows($result);
- o: O+ M2 Z8 G! E! L$ }9 q  r//spit out the results
9 [6 e* t' j1 w& W0 b- o/ pmysql_close();, y" `$ d- @2 ?7 }) i
if($user == 1) {. z& F# w7 h4 y
print("1 user online\n");) p0 `' c' \. \9 z- v
} else {
1 \6 _% u3 o5 ]" hprint("$user users online\n");! W% Z7 l* s0 n$ t, z+ p
}  ?$ Q2 U( w: e+ ^
?>6 j6 q9 }3 l* S2 T. G) z! G

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

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