Php+mysql Example: The number of Web site online program code

Source: Internet
Author: User
Tags count insert connect mysql php file php example php script mysql database

PHP Example Tutorial: The number of Web site online program code, the background has MySQL database support. You can directly calculate the current number of online Web site.

The first is to create a MySQL database table.

The following are the referenced contents:
CREATE TABLE TableName (
field type (max_length) DEFAULT ' Default_value ' (not) NULL
}

The SQL statement that you can use.

The following are the referenced contents:
CREATE TABLE Useronline (
timestamp int () DEFAULT ' 0 ' not NULL,
IP varchar not NULL,
File varchar not NULL,
PRIMARY KEY (timestamp),
KEY IP (IP),
KEY file (file)
);

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

$server = "localhost"; Your server

$db _user = "root"; The username of your MySQL

$db _pass = "password"; your MySQL password.

$database = "Users"; The name of the table

Set STATISTICS time (number of seconds online)

$timeoutseconds = 300;

Take the current time.

$timestamp = time ();

The complete code above:

The following are the referenced contents:
$insert = Mysql_db_query ($database, insert into Useronline VALUES
(' $timestamp ', ' ". $_server[' REMOTE_ADDR ']." ', ' ". $_server[' php_self ');

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, we recommend that you must set up their own password, so at least hackers have to decrypt AH)

Mysql_connect ($server, $db _user);

To query the database code:

Mysql_db_query (' database ', ' query ');

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

The following are the referenced contents:
$insert = Mysql_db_query ($database, insert into Useronline VALUES
(' $timestamp ', ' ". $_server[' REMOTE_ADDR ']." ', ' ". $_server[' php_self ');

Then we give the user a way of handling the error message.

The following are the referenced contents:
if (!) ( $insert)) {
Print "Useronline Insert Failed >";
}

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

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

Also gives the handling of errors in deleting records.

The following are the referenced contents:
if (!) ( $delete)) {
Print "Useronline Delete Failed >";
}

Below we show how many different IP 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 ();

Displays the number of people online.

The following are the referenced contents:
if ($user = = 1) {
Print ("1 user online\n");
} else {
Print ("$user users online\n");
}

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

The following are the referenced contents:

<?php
Put your basic server info
$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 haven ' t refreshed (so probbably are
Offline or inactive) in $timieoutseconds time (so it actually checks the people this 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, '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 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 in 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 online\n");
} else {
Print ("$user users online\n");
}
?>



Related Article

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.