Php+mysql Example: Program code _php tutorial for online number of web sites

Source: Internet
Author: User
Tags php example

PHP Example Tutorial: Website online number of program code, the background has MySQL database support. You can directly count the current number of people online.

The first is to create a MySQL database table.

CREATE TABLE TableName (
field type (max_length) DEFAULT default_value (not) NULL
}

The SQL statement that you can use.

CREATE TABLE Useronline (
timestamp int (0) DEFAULT is not NULL,
IP varchar (+) is not NULL,
File varchar (+) is not NULL,
PRIMARY KEY (timestamp),
KEY IP (IP),
KEY file (file)
);

Let's start with a PHP script that defines MySQL information first.

$server = "localhost"; Your server
$db _user = "root"; The user name of your MySQL
$db _pass = "password"; The password for your MySQL
$database = "Users"; The name of the table

Set the time of the statistics (number of online people in seconds)

$timeoutseconds = 300;

Take the current time.

$timestamp = time ();

The complete 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 to MySQL

mysql_connect (localhost, username, password);

Also allows the use of variable forms.

Mysql_connect ($server, $db _user, $db _pass);

If the MySQL database does not have a password, you can use the following code connection (of course, it is recommended that you have to set up their own password, so at least hackers have to decrypt AH)

Mysql_connect ($server, $db _user);

Code to query the database:

Mysql_db_query (database, query);

We need to add a record as long as we have visitors.

$insert = Mysql_db_query ($database, "insert into Useronline VALUES
($timestamp, ". $_server[remote_addr].", ". $_server[php_self].");

We then give you a way to handle the error message if the user uses it.

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

And then we have to do it. We will delete the user record when it exceeds the time we set.

$delete = Mysql_db_query ($database, "delete from Useronline where timestamp< $timeout");

Also gives the handling of errors in deleting records.

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

Below we show how many different IPs 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 the users, the code is as follows.

$user = mysql_num_rows ($result);

Finally, we want to close the database.

Mysql_close ();

Shows 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'll delete all people which havent refreshed (so probbably is
Offline or inactive) in $timieoutseconds time (so it actually checks the people that is active in the last
$timeoutseconds seconds)
This is where PHP gets the time
$timestamp = time ();
Counts the timeout, all people which has been seen last online in earlier than this timestamp, 'll 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 is 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);

http://www.bkjia.com/PHPjc/531651.html www.bkjia.com true http://www.bkjia.com/PHPjc/531651.html techarticle PHP Example Tutorial: Website online number of program code, the background has MySQL database support. You can directly count the current number of people online. The first is to create a MySQL database table. CREATE ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.