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.
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 applied.
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 apply the PHP script and first define the MYSQL information.
$ Server = \ "localhost \"; // your server
$ Db_user = \ "root \"; // your mysql username
$ 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:
$ 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 \');
Also promised to apply the variable situation.
Mysql_connect ($ server, $ db_user, $ db_pass );
If the mysql database 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_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 a solution for the user to use error information.
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 \ ']. \"\'\");
Our applications
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 \ 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\'t 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 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 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 \");
}
?>