刚用PHP写了一个网站在线人数的程序,请大家进来指点下!
刚用PHP写了一个网站在线人数的程序,请大家进来指点下!我是用PHP+MYSQL来写的,原理:网站在线人数的程序代码+后台有MYSQL数据库支持,可以直接统计出网站当前的在线人数。
首先我创建MYSQL数据库表。
CREATE TABLE tablename (F3v;oX ?a2R
field type(max_length) DEFAULT 'default_value' (NOT) NULLs [5Gav)I5s p
}可以使用的SQL语句。
CREATE TABLE useronline (m"_$@^_n n%|q
timestamp int(15) DEFAULT '0' NOT NULL,Yf(T$u!`M0t
ip varchar(40) NOT NULL,3H;H(De"Qy"V;a
file varchar(100) NOT NULL,
PRIMARY KEY (timestamp),
KEY ip (ip),WOoWfV?aW2K
KEY file (file)
);下面我们是PHP脚本,首先我定义MYSQL的信息。 {| N"a2N+s6@
$server = "localhost"; //你的服务器r)Zf;Oi_:bNc
$db_user = "root"; //你的mysql的用户名
$db_pass = "password"; //你的mysql的密码7A(FCv7g
$database = "users"; //表的名字设置统计的时间(多少秒内在线人数)
$timeoutseconds = 300;取当前时间。~m&Fym2{I%P
$timestamp = time();上面的完整代码:4\3C O&]Jv h
<?php3i@x#q$V i*f(J
$server = "localhost"; //your server
$db_user = "root"; //your mysql database username
$db_pass = "password"; //your mysql database password if any QM:|9xN8mxZ
$database = "users"; //the db name&[6]}Z)jr C e1i
$timeoutseconds = 300;//timeoutseconds limit{ s1b*pOG
//get the current time+lon3Al3oa;? g1|
$timestamp = time();1N3x9A.i4pc*|u
//calculate the lowest timestamp allowed#y_eV^Q gK
$timeout = $timestamp-$timeoutseconds;2W#U yu(\ t$Mu
?>连接mysqlr)t-g'tYIq
mysql_connect('localhost', 'username', 'password');也允许使用变量形式。