Php advanced tutorial: write a program for online display of users _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Php advanced: write a program for online user display. 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. 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 (the best combination with PHP), 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:
  
Pay the current time first: $ time = date (Y-m-d H: I: s );
MySQL (the best combination with PHP) _ select_db (line );
MySQL (best combination with PHP) _ query ("insert into line (name, time) values ($ name, $ time )");
  
Now, every logged-on user has a record in the database. The following describes the online display program line. php (as the mainstream development language ):
  
   MySQL (the best combination with PHP) _ connect ("local ","","");
MySQL (the best combination with PHP) _ select_db (line );
$ Result = MySQL (the best combination with PHP) _ query ("select * from line ");
$ Num = MySQL (the best combination with PHP) _ numrows ($ result );
If (! Empty ($ num )){
Echo"














Minute)
  
However, another problem is that a user keeps executing line. when php (as the mainstream development language) lasts for more than five minutes, the program must score the user and keep displaying the user, in this case, cookies are used to update the database time records. because the logs are authenticated, a cookie is generated to remember the user data, assume that the cookie variable for recording the user name is $ cookiename (the specific variable depends on the cookie setting), and 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 (as the mainstream development language ):
  
   // Set the current time
$ Time = date (Y-m-d H: I: s );
MySQL (the best combination with PHP) _ connect ("local ","","");
MySQL (the best combination with PHP) _ select_db (line );
  
// Update user records
MySQL (best combination with PHP) _ query ("update line set time = $ time where name = $ cookiename ");
  
// Delete user records that exceed 5 minutes
MySQL (best combination with PHP) _ query ("delete from line where time   
$ Result = MySQL (the best combination with PHP) _ query ("select * from line ");
$ Num = MySQL (the best combination with PHP) _ numrows ($ result );
If (! Empty ($ num )){
Echo" ";For ($ I = 0; $ I <$ num; $ I ++ ){$ Name = MySQL (the best combination with PHP) _ 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. php (as the mainstream development language) with the delete function:  MySQL (the best combination with PHP) _ select_db (line );MySQL (best combination with PHP) _ query ("delete from line where name = $ name ");  At this time, a basic online user function has been completed, and then proceed to line. php (as the mainstream development language) adds code to improve the function. First, we need to specify the time when the user does not continue browsing the line. php (as the mainstream development language) considers that the user has left. the time limit is 5 minutes, that is to say, the program will display the user situation in the first 5 minutes from now on, so line. set a current time in php (as the mainstream development language) to inform the program to execute from this time, then, when the program is executed, all records in the database are deleted after the current time is greater than 5 minutes, so that any user can execute line. php (as the mainstream development language) can be seen by all online users within 5 minutes. to complete this function, you need the following database statement:  Delete from line where time
";
Echo "current online users: $ num ";
Echo"
User: $ name









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

Users. ---------------------------------------------------------- the recorder can access the web...

Related Article

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.