PHP website online users-PHP source code

Source: Internet
Author: User
Ec (2); I just used PHP to write a program for online website users. Please come in and give me some advice! I wrote it using PHP + MYSQL. The principle is: program code of the number of online users of the website + MYSQL database support in the background. The current number of online users of the website can be directly counted. First, create a MYSQL database table. CREATETABLEtablename (fieldtype (max_length) defaultdefadefat_value (NOT) NU script ec (2); script


I just wrote a program for online website users in PHP. Please come in and give me some advice!
I wrote it using PHP + MYSQL. The principle is: program code of the number of online users of the website + MYSQL database support in the background. The current number of online users of the website can be directly counted.
First, 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)
). The following is a PHP script. First, I define MYSQL information.
$ Server = "localhost"; // your server
$ Db_user = "root"; // your mysql user name
$ Db_pass = "password"; // your mysql password
$ Database = "users"; // The time when the table name is set for Statistics (number of online users in seconds)
$ Timeoutseconds = 300; returns the current time.
$ Timestamp = time (); 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
$ Timeoutsecondds = 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'); variable form is also allowed.
Mysql_connect ($ server, $ db_user, $ db_pass); If the mysql database does not have a password, use the following code to connect
Mysql_connect ($ server, $ db_user); query the database code:
Mysql_db_query ('database', 'query'); we need to add a record as long as there is a visitor.
$ Insert = mysql_db_query ($ database, "insert into useronline VALUES
('$ Timestamp ','". $ _ SERVER ['remote _ ADDR ']. "','". $ _ SERVER ['php _ SELF ']. "')");
If the user uses the error message, this will be done.
If (! ($ Insert )){
Print "Useronline Insert Failed> ";
} Then, the user record will be deleted when the set time is exceeded.
$ Delete = mysql_db_query ($ database, "delete from useronline WHERE timestamp <$ timeout"); handle deletion record errors.
If (! ($ Delete )){
Print "Useronline Delete Failed> ";
} Here we will solve the problem of different IP addresses 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 users, the Code is as follows.
$ User = mysql_num_rows ($ result); closes the database.
Mysql_close (); displays the number of online users.
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.
// 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 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_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 specified les which haven'tbeen 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 );
// 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.