|

- UID
- 455
- 帖子
- 3
- 精华
- 0
- 积分
- 8
- 金币
- 3
- 威望
- 0
- 贡献
- 0
|
刚用PHP写了一个网站在线人数的程序,请大家进来指点下!
刚用PHP写了一个网站在线人数的程序,请大家进来指点下!
% s' |, i# R( p+ L我是用PHP+MYSQL来写的,原理:网站在线人数的程序代码+后台有MYSQL数据库支持,可以直接统计出网站当前的在线人数。
4 ?, k6 W9 d' L: q/ d2 W9 V首先我创建MYSQL数据库表。
( k7 i( \8 a" f8 eCREATE TABLE tablename (
/ q, E% \" w, \1 Q' nfield type(max_length) DEFAULT 'default_value' (NOT) NULL
9 `/ `+ v6 k |- Y% M0 g( H# B}可以使用的SQL语句。' J, C- l4 C1 H5 T* l, s
CREATE TABLE useronline (& a7 v0 ^ U* p2 G5 [# X
timestamp int(15) DEFAULT '0' NOT NULL,9 n' k6 b7 J0 J6 ?7 j! c
ip varchar(40) NOT NULL,
3 I9 o9 q# n, U# J4 [% Lfile varchar(100) NOT NULL,
4 @; I2 l- Q% a/ Q- d* jPRIMARY KEY (timestamp),
$ z3 \8 p6 y% kKEY ip (ip),) e* R- h0 i. B9 a0 i
KEY file (file)
( C! P) Y8 A5 n. K; P);下面我们是PHP脚本,首先我定义MYSQL的信息。
& F+ x+ Q" j1 i& F3 R5 s$server = "localhost"; //你的服务器
: n/ }( ~# q) |* J7 E$db_user = "root"; //你的mysql的用户名
$ h' ~6 ~( r: o$ }! d3 k; _$db_pass = "password"; //你的mysql的密码
6 ]- d# `, N t4 d$database = "users"; //表的名字设置统计的时间(多少秒内在线人数)0 f: h/ a% y/ j+ S p6 i
$timeoutseconds = 300;取当前时间。
5 _, o+ `, h7 U" j/ A8 u6 Q' E$timestamp = time();上面的完整代码:. d: S7 ~4 }* Q8 `8 z
<?php9 V8 r' }. g! N* k+ g( v
$server = "localhost"; //your server
# N+ x0 D( F. I- V: {+ R5 N$db_user = "root"; //your mysql database username6 F, R' A7 n! P0 I- y2 N4 y6 |
$db_pass = "password"; //your mysql database password if any
5 }/ e& t6 l( }8 i; f. f$database = "users"; //the db name
+ G$ K- u" A0 V8 l$timeoutseconds = 300;//timeoutseconds limit
& r, N, @+ |4 v" s6 `! o* m' E' R//get the current time3 u+ W, d3 z) Y! N9 i
$timestamp = time();7 e2 ~+ E2 c1 S% `9 o
//calculate the lowest timestamp allowed
" u3 Y8 T' }7 v' V$timeout = $timestamp-$timeoutseconds;4 o8 m" P0 N; E7 d2 m
?>连接mysql
( w2 b9 d9 ], x1 O6 l; Y' E$ Omysql_connect('localhost', 'username', 'password');也允许使用变量形式。0 f" @# H+ {& y0 @! J3 X
mysql_connect($server, $db_user, $db_pass);如果mysql数据库没有密码的话可以使用下面代码连接/ R, `/ d, n. y; X7 Y. W5 g: `- D
mysql_connect($server, $db_user);查询数据库的代码:
: r6 `, q! u0 e" Q$ z: Nmysql_db_query('database', 'query');我们只要有访客就要增加一条记录。7 z# M. q0 Q9 x/ N
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
9 a9 Z9 _* v+ A, E$ o( X('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");3 x1 a/ [) Q6 A/ m7 x: ]
如果用户用错误信息的话,这样处理。8 O4 |) l: w9 l
if(!($insert)) {
2 s$ B9 Y6 L. y7 mprint "Useronline Insert Failed > ";
" k7 M W5 t1 S$ b6 w) e& P}然后实现当超过设置的时间就删除该用户记录。 W: Y: i& M1 @$ R& [0 g& ^1 l9 f
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");同样给出删除记录出错的处理。
k( A6 Z- E6 {3 pif(!($delete)) {' Y* P$ {' c1 |; h
print "Useronline Delete Failed > ";/ N6 ~ [% U% h% [ k) }6 `
}下面我们解决数据库中不同IP的问题) Y$ w, I+ p3 k
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");使用
, o ?/ b: [+ B1 ~* Wmysql_num_rows(query);来统计用户,代码如下。
9 |* e6 R* {$ s1 S% s$user = mysql_num_rows($result);最后关闭数据库。
( a/ m2 z1 U. o# umysql_close();显示在线的人数。8 `7 \! B- E4 \0 j A
if($user == 1) {4 R/ D. E/ X% Q. l
print("1 user online\n");5 X( z, X; t, \) S
} else {
$ V2 N$ Y2 Y2 Bprint("$user users online\n");1 W; V$ k! ~6 G
}最终把上面代码写成一个PHP文件如下。0 o' H7 J7 i. g0 q
<?php
4 f+ F, c. o, u! c//Put your basic server info here: F* q% w3 h g) o) Q0 s
$server = "localhost"; //normally localhost
, u/ m+ H& |& S% Z7 D$db_user = "root"; //your MySQL database username
! }; S- p$ i& y# e$db_pass = "password"; //your MySQL database password
; U" e% e+ c4 P1 V2 Y" e8 H" J$database = "users";
* |1 }7 J' X3 J; v& E- ~$timeoutseconds = 300; //it will delete all people which haven't refreshed(so probbably are) e1 G6 w S2 O( M6 `1 x0 N" l
// offline or inactive) in $timieoutseconds time (so it actually checks the people that are active in the last
- g# f1 }6 Z4 T; T7 C// $timeoutseconds seconds)/ _2 x; a7 ?% f/ K. Y
//this is where PHP gets the time; K. _0 q) R1 u1 G8 ], q
$timestamp = time();% V& m* X4 W+ y5 Q( a
//counts the timeout, all people which have been seen last online in earlier than this timestamp, will get removed1 @3 Z! t; N9 @& I* w8 k% @+ K
$timeout = $timestamp-$timeoutseconds;
7 U: P4 Z1 `" l//connect to database
5 |$ `# J0 n {: T3 X( S/ z% Z [mysql_connect($server, $db_user);
) K1 y2 h2 `' n* j4 A( W+ r9 i//add the timestamp from the user to the online list
3 ~( n. g5 L8 b0 x1 L$insert = mysql_db_query($database, "INSERT INTO useronline VALUES0 x" D" I* D5 v# I# e4 s
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");1 Q0 ^5 W9 Z0 W& ?1 f1 a N$ T' u
if(!($insert)) {
1 U U9 V; u" K4 N5 l9 ]* ?( W) Hprint "Useronline Insert Failed > ";
: v( L" C# p- s" |& |}
/ u: Y, a/ b$ @% m: I//delete the peoples which haven't been online/active in the last $timeoutseconds seconds.
2 F; q. e# w4 e% m5 Q/ t& a9 {' J$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");
9 w3 }" Z: s# v& cif(!($delete)) {
0 G; w7 ]- v: ^& Fprint "Useronline Delete Failed > ";
: e5 i" {& A, {% i4 m& H S/ p% M5 i}
' s5 \$ l1 F0 X* y2 T* W2 W//select the amount of people online, all uniques, which are online on THIS page# [: B, ]5 \& o- @- \+ H
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");2 ]7 K3 i+ Z- @* x$ V# ]2 N
if(!($result)) {
/ G2 V$ l, A2 n: U' l, u/ I2 {print "Useronline Select Error > ";. L& C) m: |1 |" K- O& _
}
1 p7 K% E1 K7 H9 \; n/ P& @: f//Count the number of rows = the number of people online
6 m, V+ x* G* z' j$user = mysql_num_rows($result);
4 B5 j1 k% k5 }4 x//spit out the results. b) f, S! x$ I# Q
mysql_close();
; K, U: v7 {- J3 hif($user == 1) {
, M) f6 n+ ~) p( h9 Fprint("1 user online\n");
( x4 Z) M/ K: P/ C* l; q/ q# K" N} else {
: D* [( Z4 N& b2 qprint("$user users online\n");
" w' I* T& a) q9 ~" ^7 c}1 R+ ~! b5 c9 x0 ~3 }
?>
0 u& f& L2 W2 k; W % o- N7 F* z; w/ P
以上代码我是在时代互联提供的免费试用主机上测试的,感觉效果还不错,性能很稳定,不信的话你也可以去试试。当然,记得指出我这个程序中的不足先哦。
. C/ R W& q. y% G' h时代互联是行业内最早开始提供PHP 虚拟主机的公司之一,现在已经是的金牌域名注册商和中国互联网客户满意度十佳单位,产品功能已经非常完善 ,特别提一下几个特有用的功能:数据库自动备份、免费集成繁简通简装版、多域名绑定、多子网站支持、GCDN 网站加速器、镜像加速、高级访问统计系统、支持WAP、可选电信/网通/香港机房。
1 Y# H) K/ A* w3 n2 g我之前建站的时候选的是他们公司标准商务E型,因为这款机型还特别支持ASP.NET3.5(3.0/2.0/1.1)/ASP。9 V: I& J4 ^* X* R: Y
当然啦,这两款主机也是相当不错的。
8 b; `7 @0 M( w; Y智强商务B型:Unix /Linux 操作系统+独立网页空间1000MB+送1000MB邮局空间+100MB MySQL数据库空间=1869元/年$ J7 R4 ?8 T' L0 |; y
标准商务B型:Unix /Linux 操作系统+独立网页空间500MB+送800MB邮局空间+60MB MySQL数据库空间=1365元/年
) P; w9 \1 @# ]4 g- e+ R提供一下这个公司的联系方式:请见:http://www.now.cn/vhost/
5 N; c' |, u* A+ E9 t9 C空间专线: 0756-2623871 QQ:168075865,好象还有全球免费网络电话:全球免费咨询电话 http://www.now.cn/callcenter/call.net?LineName=55
/ e2 X b# }. C* N1 g4 v自己加QQ去问吧。 |
|