首页 > 软件下载 > UI设计

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

2022-06-12

资源介绍

首先是创建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的信息。

= "localhost"; //你的服务器
= "root"; //你的mysql的用户名
= "password"; //你的mysql的密码
= "users"; //表的名字

设置统计的时间(多少秒内在线人数)

= 300;

取当前时间。

= time();

上面的完整代码:

= "localhost"; //your server
= "root"; //your mysql database username
= "password"; //your mysql database password if any
= "users"; //the db name
= 300;//timeoutseconds limit
//get the current time
= time();
//calculate the lowest timestamp allowed
= -;
?>

连接mysql

mysql_connect(localhost, username, password);

也允许使用变量形式。

mysql_connect(, , );

如果mysql数据库没有密码的话可以使用下面代码连接(当然建议大家一定要设置好自己的密码,这样起码黑客得要解密啊)

mysql_connect(, );

查询数据库的代码:

mysql_db_query(database, query);

我们只要有访客就要增加一条记录。

= mysql_db_query(, "INSERT INTO useronline VALUES
(,".58.20.26.170.","./jiaocheng/sc.php.")");

然后我们给出如果用户用错误信息的处理方式。

if(!()) {
print "Useronline Insert Failed > ";
}

然后我们得实现当超过我们设置的时间我们就要删除该用户记录。

= mysql_db_query(, "DELETE FROM useronline WHERE timestamp<");

同样给出删除记录出错的处理。

if(!()) {
print "Useronline Delete Failed > ";
}

下面我们显示数据库中有多少个不同的IP

= mysql_db_query(, "SELECT DISTINCT ip FROM useronline WHERE file="./jiaocheng/sc.php." ");

我们使用mysql_num_rows(query);来统计用户,代码如下:

= mysql_num_rows();

最后我们要关闭数据库。

mysql_close();

显示在线的人数。

if( == 1) {
print("1 user onlinen");
} else {
print(" users onlinen");
}

最终把上面代码写成一个PHP文件如下。

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

展开全部

版权声明

1 本站所有资源(含游戏)均是软件作者、开发商投稿,任何涉及商业盈利目的均不得使用,否则产生的一切后果将由您自己承担!

2 本站资源下载后不得用于商业用途,所有资源请在下载后24小时内删除。

3 若有关在线投稿、无法下载等问题,请与本站客服人员联系。

4 如侵犯了您的版权、商标等,请立刻联系我们并具体说明情况后,本站将尽快处理删除,联系QQ:2499894784

最近更新

热门排行

最需网客户端 软件问题一手掌握

去 App Store 免费下载 iOS 客户端