Home  >  Article  >  Backend Development  >  PHP+MYSQL example: Program code for the number of people online on the website_PHP tutorial

PHP+MYSQL example: Program code for the number of people online on the website_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:29:561246browse

PHP example tutorial: Program code for the number of people online on the website, with MYSQL database support in the background. You can directly count the number of people currently online on the website.

The first is to create a MYSQL database table.

CREATE TABLE tablename (
field type(max_length) DEFAULT default_value (NOT) NULL
}

SQL statements that can be used.

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)
);

Next we start using the PHP script, first defining the MYSQL information.

$server = "localhost"; //Your server
$db_user = "root"; //Your mysql username
$db_pass = "password"; //Your mysql password
$database = "users"; //Table name

Set the statistical time (number of people online within seconds)

$timeoutseconds = 300;

Get the current time.

$timestamp = time();

Full code above:

$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;
?>

Connect mysql

mysql_connect(localhost, username, password);

Variable forms are also allowed.

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

If the mysql database does not have a password, you can use the following code to connect (of course it is recommended that you set your own password, so at least the hacker has to decrypt it)

mysql_connect($server, $db_user);

Query database code:

mysql_db_query(database, query);

As long as we have a visitor, we will add a record.

$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
($timestamp,".$_SERVER[REMOTE_ADDR].",".$_SERVER[PHP_SELF].")");

Then we give the handling method if the user uses error message.

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

Then we have to realize that when the time we set exceeds, we will delete the user record.

$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");

The handling of errors in deleting records is also provided.

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

Below we show how many different IPs there are in the database

$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file=".$_SERVER[PHP_SELF]." ");

We use

mysql_num_rows(query);

To count users, the code is as follows.

$user = mysql_num_rows($result);

Finally we have to close the database.

mysql_close();

Show the number of people online.

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

Finally write the above code into a PHP file as follows.

//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 havent 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 havent 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);

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/531651.htmlTechArticlePHP实例教程:网站在线人数的程序代码,后台有MYSQL数据库支持。可以直接统计出网站当前的在线人数。 首先是创建MYSQL数据库表。 CREATE...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn