Online website count statistics using PHP + MySql

Source: Internet
Author: User
Tags set time

The program code of the number of online users of the website, which is supported by the MySQL (the best combination with PHP) database in the background. You can directly count the number of online users of the website.

First, create a MySQL database table (the best combination with PHP.

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,
Prima (the most comprehensive virtual host Management System) ry key (timestamp ),
Key IP (IP ),
Key File (file)
);
 
Next we will start to use the PHP script to first define the information of MySQL (the best combination with PHP.

$ Server = "localhost"; // your server
$ Db_user = "root"; // your MySQL (best combination with PHP) User Name
$ Db_pass = "password"; // password for your MySQL (best combination with PHP)
$ Database = "users"; // table name

 

Set the statistical time (number of online users in seconds)

$ Timeoutseconds = 300;

The current time.

$ Timestamp = Time ();

The complete code above:

<? PHP
$ Server = "localhost"; // your server
$ Db_user = "root"; // your MySQL (the best combination with PHP) database username
$ Db_pass = "password"; // your MySQL (the best combination with PHP) database password if any
$ Database = "users"; // the DB name
$ Timeoutsecondds = 300; // timeoutseconds limit
// Get the current time
$ Timestamp = Time ();
// Calculate the lowest timestamp allowed
$ Timeout = $ timestamp-$ timeoutseconds;
?>

Connect to MySQL (the best combination with PHP)

MySQL (the best combination with PHP) _ connect ('localhost', 'username', 'Password ');

 

Variable format is also allowed.
MySQL (the best combination with PHP) _ connect ($ server, $ db_user, $ db_pass );

If the MySQL database (the best combination with PHP) does not have a password, you can use the following code to connect to the database (of course, we recommend that you set your own password so that at least hackers have to decrypt it)
MySQL (the best combination with PHP) _ connect ($ server, $ db_user );

Code for querying the database:
MySQL (the best combination with PHP) _ db_query ('database', 'query ');

 

We need to add a record as long as there is a visitor.

$ Insert = MySQL (best combination with PHP) _ db_query ($ database, "insert into useronline values
('$ Timestamp ','". $ _ server ['remote _ ADDR ']. "','". $ _ server ['php _ Self ']. "')");

Then, we will provide the handling method if the user uses the error message.
If (! ($ Insert )){
Print "useronline insert failed> ";
}

Then, we need to delete the user record when the set time is exceeded.
$ Delete = MySQL (the best combination with PHP) _ db_query ($ database, "delete from useronline where timestamp <$ timeout ");

The deletion record error is also handled.

If (! ($ Delete )){
Print "useronline Delete failed> ";
}

 

The following shows how many different IP addresses are in the database.

$ Result = MySQL (the best combination with PHP) _ db_query ($ database, "select distinct IP from useronline where file = '". $ _ server ['php _ Self ']. "'");

 

We use MySQL (the best combination with PHP) _ num_rows (query); To count users, the Code is as follows:

$ User = MySQL (the best combination with PHP) _ num_rows ($ result );

 

Finally, we need to close the database.

MySQL (the best combination with PHP) _ close ();

 

Displays the number of online users.

If ($ user = 1 ){
Print ("1 user onlinen ");
} Else {
Print ("$ User Users onlinen ");
}

 

Finally, write the above Code as a PHP file as follows.

<? PHP
// Put your basic server info here
$ Server = "localhost"; // normally localhost
$ Db_user = "root"; // your MySQL (the best combination with PHP) database username
$ Db_pass = "password"; // your MySQL (the best combination with PHP) Database Password
$ Database = "users ";
$ Timeoutseconds = 300; // It will delete all people which haven'trefreshed (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 (the best combination with PHP) _ connect ($ server, $ db_user );
// Add the timestamp from the user to the online list
$ Insert = MySQL (best combination with PHP) _ db_query ($ database, "insert into useronline values
('$ Timestamp ','". $ _ server ['remote _ ADDR ']. "','". $ _ server ['php _ Self ']. "')");
If (! ($ Insert )){
Print "useronline insert failed> ";
}
// Delete the specified les which haven'tbeen online/active in the last $ timeoutseconds seconds.
$ Delete = MySQL (the best combination with PHP) _ 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 (the best combination with PHP) _ 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 (the best combination with PHP) _ num_rows ($ result );
// Spit out the results
MySQL (the best combination with PHP) _ close ();
If ($ user = 1 ){
Print ("1 user onlinen ");
} Else {
Print ("$ User Users onlinen ");
}
?>
 

This article from <a href = 'HTTP: // www.soidc.net '> IDC Expert Network </a> original article: http://www.soidc.net/articles/1213781103032/20080721/1215945385357_1.html

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.