
- UID
- 455
- 帖子
- 3
- 精华
- 0
- 积分
- 8
- 金币
- 3
- 威望
- 0
- 贡献
- 0
|
刚用PHP写了一个网站在线人数的程序,请大家进来指点下!
刚用PHP写了一个网站在线人数的程序,请大家进来指点下!
) _: H3 m! M1 n9 ~% D我是用PHP+MYSQL来写的,原理:网站在线人数的程序代码+后台有MYSQL数据库支持,可以直接统计出网站当前的在线人数。8 {7 u# }) @ Z) \6 D8 [
首先我创建MYSQL数据库表。9 B& [2 X1 p5 Z: L
CREATE TABLE tablename (
! e8 n' _/ x& u \6 r% mfield type(max_length) DEFAULT 'default_value' (NOT) NULL: n# i6 L" |6 _: l0 q1 S$ v
}可以使用的SQL语句。1 ^7 M) B! G5 M! F* f0 b% R3 ?2 l
CREATE TABLE useronline (0 u1 a4 O# \2 `: z7 ?" k
timestamp int(15) DEFAULT '0' NOT NULL,6 I A/ X4 N! h' x% C* d4 m
ip varchar(40) NOT NULL,
/ R! s& a; C, P0 H) Mfile varchar(100) NOT NULL,3 N4 Z; J% u: G$ Z; y
PRIMARY KEY (timestamp),5 q) Q6 L' r9 B1 }
KEY ip (ip),
, d4 v# c4 C% P7 a. j$ b/ ]KEY file (file)
/ |% F* f& Y) j2 e0 A, r! S! v" ]9 `);下面我们是PHP脚本,首先我定义MYSQL的信息。9 h$ n' D; Y0 ~
$server = "localhost"; //你的服务器
# y) M \# x( Q. D) G- P) Q8 A$db_user = "root"; //你的mysql的用户名
) J' T7 U. {5 S0 K$db_pass = "password"; //你的mysql的密码: x( }1 Z' p4 Q: h* n% G9 `8 j
$database = "users"; //表的名字设置统计的时间(多少秒内在线人数); n2 J* U: \$ d0 X, D* J
$timeoutseconds = 300;取当前时间。
1 [& I3 X; t" O7 \* T$ ^" M$timestamp = time();上面的完整代码:! W' s# o c2 S$ U
<?php
: E! a1 a6 G4 [5 Z) ^% x1 x$server = "localhost"; //your server. w4 L" z) N( a8 {: j+ e) s
$db_user = "root"; //your mysql database username
5 }" ~# U, Y7 h/ j$db_pass = "password"; //your mysql database password if any2 n# J# L. N6 a. O1 S, P4 r& A8 J
$database = "users"; //the db name1 I# E5 k1 V+ @$ P8 n
$timeoutseconds = 300;//timeoutseconds limit& o7 N8 p0 C! x3 \
//get the current time! v- n8 p: `$ p
$timestamp = time();1 m5 |: `" O; {& f2 Q" z
//calculate the lowest timestamp allowed. A3 O; i% e! G! F f8 f" e1 _/ T
$timeout = $timestamp-$timeoutseconds;
/ o$ R, y2 y: }3 b; t?>连接mysql
" I- ]6 [' b5 h; Z. L8 |mysql_connect('localhost', 'username', 'password');也允许使用变量形式。! c: x2 Z+ y; n" w
mysql_connect($server, $db_user, $db_pass);如果mysql数据库没有密码的话可以使用下面代码连接
1 R7 \) i# y) qmysql_connect($server, $db_user);查询数据库的代码:
0 t$ P8 m; p! y& |mysql_db_query('database', 'query');我们只要有访客就要增加一条记录。
9 B( S$ d- Y" ~, }1 h) q# E$insert = mysql_db_query($database, "INSERT INTO useronline VALUES0 L' p5 F5 G h
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
/ I% K& c9 N; n# X如果用户用错误信息的话,这样处理。/ M1 x6 f% s( ^+ ?* L
if(!($insert)) {
% z' m9 [5 T1 u. D: O! Nprint "Useronline Insert Failed > ";3 a4 j7 C9 K) s# `- @' I+ P
}然后实现当超过设置的时间就删除该用户记录。
% y5 Z" u1 J+ G* v$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");同样给出删除记录出错的处理。5 E/ Z5 g$ E* F5 a C7 c( I
if(!($delete)) {2 N" y& q& O- `
print "Useronline Delete Failed > ";
$ o& g/ O6 h5 ], D6 a}下面我们解决数据库中不同IP的问题& U5 d. @& k6 R% k) o, a3 t& l
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");使用
1 U+ c7 p( `0 B5 L8 ?0 \mysql_num_rows(query);来统计用户,代码如下。; O. E/ b" G) B/ H5 l \8 K- {7 Y
$user = mysql_num_rows($result);最后关闭数据库。* E9 k# o7 H$ e1 f% u# A5 M
mysql_close();显示在线的人数。6 A% `* ]% I( ?2 a0 W% X/ r* }
if($user == 1) {
7 j: {& W+ V& r" ~4 b, uprint("1 user online\n");
' {2 B5 f: z( P2 h3 W2 V4 p5 J} else {
% D. i9 [4 _; t$ S0 W4 M# \$ Tprint("$user users online\n");, z) u5 t' H* G+ E$ J
}最终把上面代码写成一个PHP文件如下。
N, A3 c' o+ A% G; d1 x<?php) M- h5 I* X, }, Q7 Q
//Put your basic server info here
4 U6 X& [0 r* @# J$server = "localhost"; //normally localhost) t# B8 c! o- v0 H# T% J* o
$db_user = "root"; //your MySQL database username
7 I5 D. q, z1 q$ E1 _' u, y& z; q$db_pass = "password"; //your MySQL database password5 J$ I# ]+ H) w A( n7 y% K, V
$database = "users";
) @* X9 ^( O3 i$ M; S+ v+ R7 o$timeoutseconds = 300; //it will delete all people which haven't refreshed(so probbably are" D% M4 [: r3 x/ ]2 w, _; i2 S
// offline or inactive) in $timieoutseconds time (so it actually checks the people that are active in the last
7 H0 Y- P4 Q0 P. {* p( R// $timeoutseconds seconds), b" s f& D$ F& v
//this is where PHP gets the time
t. @- D6 w# n! |& \6 m3 @! T$timestamp = time();& o% m# y6 t2 U* x* r: i, k+ |) M' e! m
//counts the timeout, all people which have been seen last online in earlier than this timestamp, will get removed# A/ w4 x0 i3 C% D" S$ z! G
$timeout = $timestamp-$timeoutseconds;+ N j1 f: A, K- U$ E; F6 X
//connect to database
$ j# l8 [: g9 O3 M! Q+ _; omysql_connect($server, $db_user);# c# M: a, W7 u# k7 x4 H. X
//add the timestamp from the user to the online list
- x* u" E: x3 o* f6 g$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
6 H7 ^ k1 S8 e9 ?: c, H('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
% J9 ]& a" Q$ q( A, a7 Yif(!($insert)) {- b7 l+ D4 k9 k9 Y
print "Useronline Insert Failed > ";
0 @; p Y$ b9 w3 ~" k2 }}7 C. ?9 \. \; P
//delete the peoples which haven't been online/active in the last $timeoutseconds seconds.1 x! A9 D% S: M2 o0 \/ V
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");
X' C5 J( G$ R3 A8 {if(!($delete)) {
" s$ A4 [! K' l- q4 g( Oprint "Useronline Delete Failed > ";
8 Z7 g/ V+ ]0 x' ?% T}5 f- x( ?. f+ @
//select the amount of people online, all uniques, which are online on THIS page; J0 C* F9 b. ?6 j* R
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");5 W w6 c, ~) i# M
if(!($result)) {4 l- L' l, ^% s T T
print "Useronline Select Error > ";
4 {. b. U3 b3 V# s% r" r( x}
4 z* E& A: [5 P//Count the number of rows = the number of people online
5 G8 j/ a4 Z2 i P$user = mysql_num_rows($result);
! d0 ~) Y; R- Q# R2 w; N//spit out the results
( D% u% L! P0 P1 Q: J! r: |mysql_close();; f( d5 D. h% f1 w O$ }7 Z6 O
if($user == 1) {
5 T8 O( H0 B) Z Q8 dprint("1 user online\n");
* v4 l R, U0 T% y% w} else {% ?) x2 m/ F; P9 m- O. B
print("$user users online\n");/ `4 Q# l h, F; \- S2 I5 ~1 U! u
}
$ e. i3 A2 a F8 P?>
- |$ l, q) v! ^4 t0 G3 B3 z
% ?9 x+ ]9 v( [以上代码我是在时代互联提供的免费试用主机上测试的,感觉效果还不错,性能很稳定,不信的话你也可以去试试。当然,记得指出我这个程序中的不足先哦。* m8 f/ `, ~* ?8 I, I; _
时代互联是行业内最早开始提供PHP 虚拟主机的公司之一,现在已经是的金牌域名注册商和中国互联网客户满意度十佳单位,产品功能已经非常完善 ,特别提一下几个特有用的功能:数据库自动备份、免费集成繁简通简装版、多域名绑定、多子网站支持、GCDN 网站加速器、镜像加速、高级访问统计系统、支持WAP、可选电信/网通/香港机房。6 B! y0 l& X, N+ t0 p1 x: G
我之前建站的时候选的是他们公司标准商务E型,因为这款机型还特别支持ASP.NET3.5(3.0/2.0/1.1)/ASP。
5 m; Y- I: g6 [+ x$ G# g% l当然啦,这两款主机也是相当不错的。
# P$ S& N' b- u) \$ W) n智强商务B型:Unix /Linux 操作系统+独立网页空间1000MB+送1000MB邮局空间+100MB MySQL数据库空间=1869元/年* k+ I9 U# H: m+ i. _
标准商务B型:Unix /Linux 操作系统+独立网页空间500MB+送800MB邮局空间+60MB MySQL数据库空间=1365元/年7 [- a: M7 g! _' i5 n+ K
提供一下这个公司的联系方式:请见:http://www.now.cn/vhost/
& U8 y7 r7 f0 e8 N, T: g空间专线: 0756-2623871 QQ:168075865,好象还有全球免费网络电话:全球免费咨询电话 http://www.now.cn/callcenter/call.net?LineName=557 O1 @3 P' ~$ u2 o! V4 r$ W
自己加QQ去问吧。 |
|