PHP+MYSQL实例:网站在线人数的程序代码

刚用PHP写了三个网站在窥伺者数的前后相继,请大家进来指引下!作者是用PHP+MYSQL来写的,原理:网址在窥伺者数的程序代码+后台有MYSQL数据库援助,能够平昔总括出网址当前的在线人数。首先本人创设MYSQL数据库表。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(100State of Qatar NOT
NULL,PTiguanIMA福特ExplorerY KEY (timestamp卡塔尔,KEY ip (ip),KEY file
(fileState of Qatar卡塔尔;下边大家是PHP脚本,首先自身定义MYSQL的消息。$server = localhost;
//你的服务器$db_user = root; //你的mysql的客户名$db_pass = password;
//你的mysql的密码$database = users;
//表的名字设置计算的小时(多少秒内在线人数卡塔尔(قطر‎$timeoutseconds =
300;取当前时刻。$timestamp = time(卡塔尔国;上边的全部代码:?php$server =
localhost; //your server$db_user = root; //your mysql database
username$db_pass = password; //your mysql database password if
any$database = users; //the db name$timeoutseconds =
300;//timeoutseconds limit//get the current time$timestamp =
time();//calculate the lowest timestamp allowed$timeout =
$timestamp-$timeoutseconds;?连接mysqlmysql_connect(‘localhost’,
‘username’, ‘password’State of Qatar;也允许利用变量格局。mysql_connect($server,
$db_user,
$db_pass卡塔尔(قطر‎;要是mysql数据库未有密码的话能够使用上边代码连接mysql_connect($server,
$db_userState of Qatar;查询数据库的代码:mysql_db_query(‘database’,
‘query’State of Qatar;大家假若有访客就要扩大一条记下。$insert =
mysql_db_query($database, INSERT INTO useronline
VALUES(‘$timestamp’,’.$_SERVER[‘REMOTE_ADDR’].’,’.$_SERVER[‘PHP_SELF’].’State of Qatar卡塔尔(قطر‎;即使顾客用错误音信的话,那样管理。if(!($insertState of Qatar卡塔尔国{print Useronline Insert Failed
;}然后实现当逾越设置的时日就删除该顾客记录。$delete =
mysql_db_query($database, DELETE FROM useronline WHERE
timestamp$timeoutState of Qatar;同样给出删除记录出错的拍卖。if(!($delete卡塔尔(قطر‎卡塔尔国 {print
Useronline Delete Failed ;}上面咱们化解数据库中分歧IP的难点$result =
mysql_db_query($database, SELECT DISTINCT ip FROM useronline WHERE
file=’.$_SERVER[‘PHP_SELF’].’
);使用mysql_num_rows(query卡塔尔国;来计算顾客,代码如下。$user =
mysql_num_rows($result卡塔尔;最后关闭数据库。mysql_close(卡塔尔国;展现在线的总人口。if($user
== 1卡塔尔 {print(1 user online/n卡塔尔(قطر‎;} else {print($user users
online/n卡塔尔(قطر‎;}最终把地点代码写成二个PHP文件如下。?php//Put your basic
server info here$server = localhost; //normally localhost$db_user =
root; //your MySQL database username$db_pass = password; //your MySQL
database password$database = users;$timeoutseconds = 300; //it will
delete all people which haven’t refreshed(so probbably are// offline or
inactive) in $timieoutseconds time (so it actually checks the people
that are active in the last// $timeoutseconds seconds)//this is where
PHP gets the time$timestamp = time();//counts the timeout, all people
which have been seen last online in earlier than this timestamp, will
get removed$timeout = $timestamp-$timeoutseconds;//connect to
databasemysql_connect($server, $db_user);//add the timestamp from the
user to the online list$insert = mysql_db_query($database, INSERT INTO
useronline
VALUES(‘$timestamp’,’.$_SERVER[‘REMOTE_澳门新葡萄京官网首页 ,ADDR’].’,’.$_SERVER[‘PHP_SELF’].’));if(!($insert))
{print Useronline Insert Failed ;}//delete the peoples which haven’t
been online/active in the last $timeoutseconds seconds.$delete =
mysql_db_query($database, DELETE FROM useronline WHERE
timestamp$timeout);if(!($delete)) {print Useronline Delete Failed
;}//select the amount of people online, all uniques, which are online on
THIS page$result = mysql_db_query($database, SELECT DISTINCT ip FROM
useronline WHERE file=’.$_SERVER[‘PHP_SELF’].’ );if(!($result))
{print Useronline Select Error ;}//Count the number of rows = the number
of people online$user = mysql_num_rows($result);//spit out the
resultsmysql_close();if($user == 1) {print(1 user online/n);} else
{print($user users online/n);}?

PHP实例教程:网址在线人数的程序代码,后台有MYSQL数据库辅助。能够一贯计算出网址当前的在线人数。

第一是创制MYSQL数据库表。

以下为援用的从头到尾的经过:
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脚本,首先定义MYSQL的音信。

$server = “localhost”; //你的服务器

$db_user = “root”; //你的mysql的顾客名

$db_pass = “password”; //你的mysql的密码

$database = “users”; //表的名字

设置总括的小时(多少秒内在线人数卡塔尔(قطر‎

$timeoutseconds = 300;

取当今日子。

$timestamp = time();

上面包车型大巴一体化代码:

以下为引用的剧情:
$insert = mysql_db_query($database, “INSERT INTO useronline VALUES
(‘$timestamp’,'”.$_SERVER[‘REMOTE_ADDR’].”‘,'”.$_SERVER[‘PHP_SELF’].”‘)”);

连接mysql

mysql_connect(‘localhost’, ‘username’, ‘password’);

也同意利用变量格局。

mysql_connect($server, $db_user, $db_pass);

万一mysql数据库未有密码的话能够应用上边代码连接(当然提议大家一定要安装好温馨的密码,那样起码骇客得要解密啊)

mysql_connect($server, $db_user);

查询数据库的代码:

mysql_db_query(‘database’, ‘query’);

我们纵然有访客就要增添一条记下。

以下为引用的内容:
$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

同样给出删除记录出错的拍卖。

以下为援引的内容:
if(!($delete)) {
print “Useronline Delete Failed > “;
}

上面大家展现数据库中有个别许个差别的IP

$result = mysql_db_query($database, “SELECT DISTINCT ip FROM
useronline WHERE file='”.$_SERVER[‘PHP_SELF’].”‘ “);

大家利用

mysql_num_rows(query);

来计算顾客,代码如下。

$user = mysql_num_rows($result);

最后我们要关门数据库。

mysql_close();

显示在线的食指。

以下为援引的从头到尾的经过:
if($user == 1) {
print(“1 user onlinen”);
} else {
print(“$user users onlinen”);
}

末了把地方代码写成一个PHP文件如下。

以下为引用的从头到尾的经过:

//Put your basic server info here
$server = “localhost”; //normally localhost
$db_user = “root”; //your MySQL database username
$db_pass = “password”; //your MySQL database password
$database = “users”;
$timeoutseconds = 300; //it will delete all people which haven’t
refreshed(so probbably are
// offline or inactive) in $timieoutseconds time (so it actually checks
the people that are active in the last
// $timeoutseconds seconds)
//this is where PHP gets the time
$timestamp = time();
//counts the timeout, all people which have been seen last online in
earlier than this timestamp, will get removed
$timeout = $timestamp-$timeoutseconds;
//connect to database
mysql_connect($server, $db_user);
//add the timestamp from the user to the online list
$insert = mysql_db_query($database, “INSERT INTO useronline VALUES
(‘$timestamp’,'”.$_SERVER[‘REMOTE_ADDR’].”‘,'”.$_SERVER[‘PHP_SELF’].”‘)”);
if(!($insert)) {
print “Useronline Insert Failed > “;
}
//delete the peoples which haven’t been online/active in the last
$timeoutseconds seconds.
$delete = mysql_db_query($database, “DELETE FROM useronline WHERE
timestampif(!($delete)) {
print “Useronline Delete Failed > “;
}
//select the amount of people online, all uniques, which are online on
THIS page
$result = mysql_db_query($database, “SELECT DISTINCT ip FROM
useronline WHERE file='”.$_SERVER[‘PHP_SELF’].”‘ “);
if(!($result)) {
print “Useronline Select Error > “;
}
//Count the number of rows = the number of people online
$user = mysql_num_rows($result);
//spit out the results
mysql_close();
if($user == 1) {
print(“1 user onlinen”);
} else {
print(“$user users onlinen”);
}
?>

发表评论

电子邮件地址不会被公开。 必填项已用*标注