刚用PHP写了一个网站在线人数的程序,请大家进来指点下!
刚用PHP写了一个网站在线人数的程序,请大家进来指点下!5} HF5b q@e我是用PHP+MYSQL来写的,原理:网站在线人数的程序代码+后台有MYSQL数据库支持,可以直接统计出网站当前的在线人数。
首先我创建MYSQL数据库表。6Pq"w1Dp'k,eO/?
CREATE TABLE tablename (
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,#p;@~3S4|;_8x
file varchar(100) NOT NULL,A/hwN1{m'``
PRIMARY KEY (timestamp),
KEY ip (ip),
KEY file (file)
);下面我们是PHP脚本,首先我定义MYSQL的信息。Ak0TV*fp
$server = "localhost"; //你的服务器bYX%I$R/tcW
$db_user = "root"; //你的mysql的用户名
$db_pass = "password"; //你的mysql的密码G\*{4[G2sv(KXu+o
$database = "users"; //表的名字设置统计的时间(多少秒内在线人数)
$timeoutseconds = 300;取当前时间。
$timestamp = time();上面的完整代码:
<?php
$server = "localhost"; //your server G D|qmF2d+{
$db_user = "root"; //your mysql database username
$db_pass = "password"; //your mysql database password if any
$database = "users"; //the db name/A:LK eS!k
$timeoutseconds = 300;//timeoutseconds limitJ0b%T,h%s0\r
//get the current time
$timestamp = time();!Q7A){;\{y!j*X
//calculate the lowest timestamp allowedfe[[V ?
$timeout = $timestamp-$timeoutseconds;
?>连接mysqln#O;E^j$Z+M
mysql_connect('localhost', 'username', 'password');也允许使用变量形式。 y'Ha+H$B$f
mysql_connect($server, $db_user, $db_pass);如果mysql数据库没有密码的话可以使用下面代码连接zQ9W6Hf~7STs(l
mysql_connect($server, $db_user);查询数据库的代码: