以下sql语句片段: 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, file varchar(100) NOT NULL, PRIMARY KEY (timestamp), KEY ip (ip), KEY file (file) );
数据库建立好了。下面就开始写php代码
以下是代码片段:]
<?php
//下面是连接数据库的一些变量
$server = "localhost";
$db_user = "root";
$db_pass = "password";
$database = "users";
$timeoutseconds = 300; // 设置统计的时间(多少秒内在线人数)
$timestamp = time();
$timeout = $timestamp-$timeoutseconds;
mysql_connect($server, $db_user);//连接mysql
//查询数据库的代码
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
if(!($insert)) {
print "Useronline Insert Failed > ";
}
//超过我们设置的时间我们就要删除该用户记录。
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");
if(!($delete)) {
print "Useronline Delete Failed > ";
}
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");
if(!($result)) {
print "Useronline Select Error > ";
}
$user = mysql_num_rows($result);
mysql_close(); //做完事情记得关闭
if($user == 1) {
print("1 user online
");
} else {
print("$user users online
");
}
?>
好了,就这么多了,本站百度搜索关键字:合肥网站建设、合肥网站设计、合肥建网站、合肥网站优化、品牌网站建设、品牌网站设计
免费电话咨询
18955177249