PHP example Tutorial: program code for the number of online users of the website, which is supported by the MYSQL database in the background. You can directly count the number of online users of the website.
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)
);
Next we start to use the PHP script to first define MYSQL information.
$ Server = "localhost"; // your server
$ Db_user = "root"; // your mysql user name
$ Db_pass = "password"; // your mysql password
$ 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 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 format is 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 set your own password so that at least hackers have to decrypt it)
Mysql_connect ($ server, $ db_user );
Code for querying the database:
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].") ");
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_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_db_query ($ database, "select distinct ip FROM useronline WHERE file =". $ _ SERVER [PHP_SELF]. "");
We use
Mysql_num_rows (query );
The Code is as follows.
$ User = mysql_num_rows ($ result );
Finally, we need to close the database.
Mysql_close ();
Displays the number of online users.
If ($ user = 1 ){
Print ("1 user online ");
} Else {
Print ("$ user users online ");
}
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 database username
$ Db_pass = "password"; // your MySQL database password
$ Database = "users ";
$ Timeoutseconds = 300; // it will delete all people which havent refreshed (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 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 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 );