Php advanced: write a program for Online User display

Source: Internet
Author: User

At the beginning of this article, the author assumes that the reader has been able to write a user login authentication program.
----------------------------------------------------------
The recorder can complete the total number of visits to web pages, but cannot know the dynamic records of access volume in a period of time. The following describes how to write a method to dynamically display access volume in each period of time.
To record the traffic volume, you must first create a database in mysql, and name the database line and create a data table named line, the fields in the table are respectively "User name (name varchar (20), time datetime )". of course, you can also add fields in the data table as needed.
After the database is created, you can start designing the program. Now, let's clarify the concept. to display the access traffic, of course, the database must have a record, I have assumed that the reader has the ability to write a user login program, so adding a record to the database can be assumed as login in the login program. add in php:
Pay the current time first: $ time = date ('Y-m-d H: I: s ');
Mysql_select_db (line );
Mysql_query ("insert into line (name, time) values ('$ name',' $ Time ')");
Now every logged-in user has a record in the database. The following shows the online display program line. php:
<?
Mysql_connect ("local ","","");
Mysql_select_db (line );
$ Result = mysql_query ("select * from line ");
$ Num = mysql_numrows ($ result );
If (! Empty ($ num )){
Echo "<table> <tr> <td> ";
Echo "Current online users: $ num ";
Echo "</td> </tr> ";
For ($ I = 0; $ I <$ num; $ I ++ ){
$ Name = mysql_result ($ result, $ I, "name ");
Echo "<tr> <td> User: $ name </td> </tr> ";
}
}
?>
The above section shows the number of online users and user names. Of course, this program is not complete yet. if one of the users logs out and leaves, the database should not have this user record. Therefore, the logged out program must be assumed as logout. added the deletion function in php:
Mysql_select_db (line );
Mysql_query ("delete from line where name = '$ name '");
At this time, a basic online user function has been completed, and then proceed to line. add code in php to improve the function. First, we need to specify how long the user will not continue browsing the line. php considers that the user has left. Here, a time limit is set to 5 minutes. That is to say, the program will display the user situation for the first 5 minutes from now on, so line is required. set a current time in php to inform the program to start execution from this time. Then, when the program is executed, delete the time recorded in the database minus all records whose current time is greater than 5 minutes, in this way, any user is executing line. all online users within five minutes can be seen in php. To complete this function, the following database statements are required:

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.