刚用PHP写了一个网站在线人数的程序,请大家进来指点下!
刚用PHP写了一个网站在线人数的程序,请大家进来指点下!'RQ Y1}N'ix我是用PHP+MYSQL来写的,原理:网站在线人数的程序代码+后台有MYSQL数据库支持,可以直接统计出网站当前的在线人数。_/di3om;d@i%N
首先我创建MYSQL数据库表。
CREATE TABLE tablename (k}*]Q|ST4W
field type(max_length) DEFAULT 'default_value' (NOT) NULL
}可以使用的SQL语句。
CREATE TABLE useronline (
timestamp int(15) DEFAULT '0' NOT NULL,
ip varchar(40) NOT NULL,2i5V)o @(}-Z V?@[ m
file varchar(100) NOT NULL, {"g7^2u3Z
PRIMARY KEY (timestamp),/?\l5NQM#m
KEY ip (ip),
KEY file (file)
);下面我们是PHP脚本,首先我定义MYSQL的信息。
$server = "localhost"; //你的服务器]luWDH
$db_user = "root"; //你的mysql的用户名(\VrX4m
$db_pass = "password"; //你的mysql的密码
$database = "users"; //表的名字设置统计的时间(多少秒内在线人数)
$timeoutseconds = 300;取当前时间。
$timestamp = time();上面的完整代码:
<?phpES;Be(g0Z;s
$server = "localhost"; //your server
$db_user = "root"; //your mysql database username
$db_pass = "password"; //your mysql database password if anyX{Q_L
$database = "users"; //the db name
$timeoutseconds = 300;//timeoutseconds limitF(@/j!X3F+m2p y+P[
//get the current time
$timestamp = time();
//calculate the lowest timestamp allowed
$timeout = $timestamp-$timeoutseconds;.G]~a(naor
?>连接mysql