
- UID
- 455
- 帖子
- 3
- 精华
- 0
- 积分
- 8
- 金币
- 3
- 威望
- 0
- 贡献
- 0
|
刚用PHP写了一个网站在线人数的程序,请大家进来指点下!
刚用PHP写了一个网站在线人数的程序,请大家进来指点下!1 J& [! s4 [2 O; C
我是用PHP+MYSQL来写的,原理:网站在线人数的程序代码+后台有MYSQL数据库支持,可以直接统计出网站当前的在线人数。" r$ B1 }) i- H+ N2 V& q8 q* _' a
首先我创建MYSQL数据库表。
1 C% b+ h' Z9 j( S8 T' W7 V- QCREATE TABLE tablename () c/ \. y, I: z# R
field type(max_length) DEFAULT 'default_value' (NOT) NULL) c; ~8 u% w7 E# ~6 E; L) u
}可以使用的SQL语句。
6 d O- Z4 \7 b% B0 BCREATE TABLE useronline (8 e0 b5 G$ H1 V
timestamp int(15) DEFAULT '0' NOT NULL,2 f. Q5 L* Y9 P, b
ip varchar(40) NOT NULL,' h4 L% L S$ _2 E. d1 D; M% v7 l
file varchar(100) NOT NULL,
: ]" [3 O$ @( S, E; e* oPRIMARY KEY (timestamp),
8 y3 ?/ d0 p' f9 }" g! l s. n- @KEY ip (ip),
2 Z% @2 F0 R' U9 [1 U! P+ wKEY file (file)
0 G- _7 a* {% k) n0 N$ i! ^9 u);下面我们是PHP脚本,首先我定义MYSQL的信息。5 u; I# h* ~# I$ L
$server = "localhost"; //你的服务器
6 F5 ?4 k. p( Q$ n7 U* |$db_user = "root"; //你的mysql的用户名
, N7 n, |& f6 m# r8 s$ a* A, }$db_pass = "password"; //你的mysql的密码
' {9 |% j- V4 q; g& J- N$database = "users"; //表的名字设置统计的时间(多少秒内在线人数)
: D/ N8 I! |2 S) [. D% N2 ]3 b$timeoutseconds = 300;取当前时间。
: D" s. h1 m) i" i7 D( s/ @8 h8 A$timestamp = time();上面的完整代码:
% }- C: F0 n% t( [: O; t2 s9 T, ?2 i<?php
$ k$ o2 B: U* @9 W F! {- o$server = "localhost"; //your server: e) B( L+ V9 y$ |" J
$db_user = "root"; //your mysql database username' y9 }0 J- w) ~2 Y3 q
$db_pass = "password"; //your mysql database password if any5 V$ t# l q$ u% }9 s
$database = "users"; //the db name- d9 U2 f4 Z* W2 \! [
$timeoutseconds = 300;//timeoutseconds limit8 f; A$ m' h9 ?& \) n) c
//get the current time
7 W4 y) L6 M7 S$ s7 h3 q! s2 h$timestamp = time();& C: V+ G% K' ^' r
//calculate the lowest timestamp allowed/ w% G2 @, x/ [. w" F- i
$timeout = $timestamp-$timeoutseconds;
7 G" Z! g, {' Q( }9 y& n" e) B, E?>连接mysql7 O) W; ~9 S. `$ ^" x6 W4 \' @
mysql_connect('localhost', 'username', 'password');也允许使用变量形式。
& A# S2 K6 f3 L) M5 Bmysql_connect($server, $db_user, $db_pass);如果mysql数据库没有密码的话可以使用下面代码连接
. v$ Q; z% U: x7 M* l! pmysql_connect($server, $db_user);查询数据库的代码:
1 ^* W) x% l4 Hmysql_db_query('database', 'query');我们只要有访客就要增加一条记录。& L/ ]6 i- o2 P4 W6 m A0 w# y- I
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
B3 L( c- ?/ p- e2 Z' P# @2 x' \('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
4 Y4 i O+ H+ t6 V }如果用户用错误信息的话,这样处理。8 ?% ~$ J- n) r9 a1 A# q
if(!($insert)) {7 v4 z! ]8 G" F6 D2 _2 B
print "Useronline Insert Failed > ";8 ~: x- t1 g/ |7 U0 P3 |: z
}然后实现当超过设置的时间就删除该用户记录。' Y# x$ d- f, c6 A5 k. }. ~
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");同样给出删除记录出错的处理。
( E3 l! Z4 S" T& j' _5 i* zif(!($delete)) {
- e6 u8 m. ?! R. `. Yprint "Useronline Delete Failed > ";! N6 f. ?# b* ~- ]# A
}下面我们解决数据库中不同IP的问题
J( d B- G1 m7 v0 ~* K" F1 I7 N2 z6 O$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");使用5 f8 Q' h6 h$ J: y
mysql_num_rows(query);来统计用户,代码如下。; e6 H8 Q+ N: _$ i2 o9 X; ~
$user = mysql_num_rows($result);最后关闭数据库。: b/ v- o( l) ~' @; |/ K. B
mysql_close();显示在线的人数。
% ^; h. Q6 x$ T F" B$ u& iif($user == 1) {
8 [; }% K1 F1 Lprint("1 user online\n");
4 U* E) k- Y/ f' H o. h6 e# w5 \} else {
`4 G' A# p* H/ uprint("$user users online\n");: X$ @- @5 `1 {8 J: c+ K
}最终把上面代码写成一个PHP文件如下。
$ a& G ~ Y k- q1 u7 }<?php0 s7 _* d/ G, e
//Put your basic server info here
/ y! X* _0 j9 r0 E3 A3 J" o# ~+ S$server = "localhost"; //normally localhost9 w( y# E5 F; P$ ]0 Y' x
$db_user = "root"; //your MySQL database username
( f$ {' Z* M2 h3 A7 k$db_pass = "password"; //your MySQL database password; F4 d% {- J! E5 K
$database = "users";% Z5 s& J; @ ]( K8 a) O
$timeoutseconds = 300; //it will delete all people which haven't refreshed(so probbably are" o0 G% h. E8 H0 w
// offline or inactive) in $timieoutseconds time (so it actually checks the people that are active in the last2 N, V7 t( S$ @! v% n
// $timeoutseconds seconds)
, b1 h/ t- \; U4 U//this is where PHP gets the time
" p1 ]& H& s" ~* u$timestamp = time();; l( x0 t! X0 E( M" N W: v
//counts the timeout, all people which have been seen last online in earlier than this timestamp, will get removed
, C3 S* H2 U' S9 M# r# F$timeout = $timestamp-$timeoutseconds;9 R7 ]: A7 @. @2 [
//connect to database9 s0 |- s( q$ i$ F& E4 |, _. B
mysql_connect($server, $db_user); v; G# t. q: I2 [$ k0 P
//add the timestamp from the user to the online list
1 h, i, D0 h" q: d1 U- O$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
! S5 k5 l1 I* k, I* \2 v- m('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");1 Y5 ]0 T/ @3 i% i( v: B2 Q
if(!($insert)) {2 i" @5 F5 e" C X4 I0 d
print "Useronline Insert Failed > ";! V/ w) ~, N$ i# b: _) T
}7 i0 g8 `2 T7 J
//delete the peoples which haven't been online/active in the last $timeoutseconds seconds.
' c8 k- q7 n/ D$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");
R% J6 T+ n" c6 l# Uif(!($delete)) {/ W# s* o b q& e1 }
print "Useronline Delete Failed > ";
( x$ z3 i! n2 {9 c( Q; I}4 @8 R7 @6 {7 @/ |
//select the amount of people online, all uniques, which are online on THIS page
: x9 V- x( S9 Y Z4 E( q) Z8 h0 M: p$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");/ r2 p( t4 {, Z6 n1 E
if(!($result)) {
' ^/ R. C# o5 [2 |- @( ~" s) }print "Useronline Select Error > ";
% l, [$ z& D1 q! r; k}
9 m7 I% r$ z1 x//Count the number of rows = the number of people online
5 ~9 F1 _3 [+ k2 {! j' y! Z- M1 H" A$user = mysql_num_rows($result);0 Z# V5 v2 u) W9 k
//spit out the results4 z5 l) y$ @( }2 o- X. [2 ^
mysql_close();
2 d [' X3 E3 ^if($user == 1) {7 f5 {" c$ e6 r# i
print("1 user online\n");
. l8 ?: s' J) Z# |' L} else {
4 F# f2 d9 ^. w' oprint("$user users online\n");1 B [% |# R+ x+ k0 m# b
}
7 U& J3 J" E: I' P9 h4 _9 X' g# R$ t8 X?>
2 X j; U6 A5 ]+ D- ]9 L! V
& S, ^5 A' Q+ k9 r0 y$ h以上代码我是在时代互联提供的免费试用主机上测试的,感觉效果还不错,性能很稳定,不信的话你也可以去试试。当然,记得指出我这个程序中的不足先哦。7 _: \+ W4 n# C4 H# ^& O7 q1 g
时代互联是行业内最早开始提供PHP 虚拟主机的公司之一,现在已经是的金牌域名注册商和中国互联网客户满意度十佳单位,产品功能已经非常完善 ,特别提一下几个特有用的功能:数据库自动备份、免费集成繁简通简装版、多域名绑定、多子网站支持、GCDN 网站加速器、镜像加速、高级访问统计系统、支持WAP、可选电信/网通/香港机房。
5 K* C2 ^! Q2 W7 Q3 ~6 q我之前建站的时候选的是他们公司标准商务E型,因为这款机型还特别支持ASP.NET3.5(3.0/2.0/1.1)/ASP。
* Y+ F% R3 T0 m' W当然啦,这两款主机也是相当不错的。
6 j$ |' I8 f4 n s智强商务B型:Unix /Linux 操作系统+独立网页空间1000MB+送1000MB邮局空间+100MB MySQL数据库空间=1869元/年
9 i% J0 C. j. _$ n- Q$ I标准商务B型:Unix /Linux 操作系统+独立网页空间500MB+送800MB邮局空间+60MB MySQL数据库空间=1365元/年% @( B* o! s% G, {$ I. T
提供一下这个公司的联系方式:请见:http://www.now.cn/vhost/
9 D/ k, t: G6 N$ L$ _# H! b空间专线: 0756-2623871 QQ:168075865,好象还有全球免费网络电话:全球免费咨询电话 http://www.now.cn/callcenter/call.net?LineName=55
4 u6 X5 g' r8 q# R- A( X0 k自己加QQ去问吧。 |
|