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 access the web page for the total number of times, but cannot know the dynamic records of the access volume in a period. The following describes how to write a method to dynamically display the access volume during each. to record the traffic volume, first, at the beginning of My 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"



































";For ($ I = 0; $ I <$ num; $ I ++ ){$ Name = mysql_result ($ result, $ I, "name ");Echo" ";}}?>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:Delete from line where timeMinute)However, another problem is that a user keeps executing line. if php has been used for more than five minutes, the program must identify the user and keep displaying the user. in this case, we can use cookies to update the database records, because they are log-on authenticated, therefore, there will be a cookie to remember the user information. assume that the cookie variable for recording the user name is $ cookiename (the specific variable depends on the cookie setting ), the rest is easy to do. use this cookie variable to modify the database:Update line set time = '$ time' where name =' $ cookiename'Below we will improve line. php: // Set the current time$ Time = date ('Y-m-d H: I: s ');Mysql_connect ("local ","","");Mysql_select_db (line );// Update user recordsMysql_query ("update line set time = '$ time' where name =' $ cookiename '");// Delete user records that exceed 5 minutesMysql_query ("delete from line where time$ Result = mysql_query ("select * from line ");$ Num = mysql_numrows ($ result );If (! Empty ($ num )){Echo"
";
Echo "current online users: $ num ";
Echo"
User: $ name








";For ($ I = 0; $ I <$ num; $ I ++ ){$ Name = mysql_result ($ result, $ I, "name ");Echo" ";}}?>The online display function of a good user is complete.
";
Echo "current online users: $ num ";
Echo"
User: $ name

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.