Number of online PHP websites

Source: Internet
Author: User
Tags current time table name


Just used PHP to write a website online number of procedures, please come in to guide!
I use Php+mysql to write, principle: Website online number of program code + backstage has MySQL database support, you can directly statistics the current number of online Web site.
First I 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 () DEFAULT ' 0 ' not NULL,
IP varchar not NULL,
File varchar not NULL,
PRIMARY KEY (timestamp),
KEY IP (IP),
KEY file (file)
Below we are PHP scripts, first I define MySQL information.
$server = "localhost"; Your server
$db _user = "root"; The username of your MySQL
$db _pass = "password"; your MySQL password.
$database = "Users"; Table Name SET STATISTICS time (number of seconds online)
$timeoutseconds = 300, take the current time.
$timestamp = time (); The complete code above:
<?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;
?> connection 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
Mysql_connect ($server, $db _user); Query the code for 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 ');
If the user uses the error message, this is done.
if (!) ( $insert)) {
Print "Useronline Insert Failed >";
Then implement to delete the user record when the setting is exceeded.
$delete = Mysql_db_query ($database, "delete from Useronline WHERE timestamp< $timeout");
if (!) ( $delete)) {
Print "Useronline Delete Failed >";
Below we solve the problem of different IP in the database
$result = Mysql_db_query ($database, "SELECT DISTINCT IP from useronline WHERE file= '". $_server[' Php_self ']. " "); use
Mysql_num_rows (query) to count the users, the code is as follows.
$user = mysql_num_rows ($result), and finally close the database.
Mysql_close () show the number of people online.
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.
<?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");
}
?>

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.