PHP Master Advanced: Write a user online display program _php tutorial

Source: Internet
Author: User
At the beginning of this article, the author assumes that the reader has been able to write a user's login certification program.
----------------------------------------------------------
Registers can complete the total number of times to access the Web page, but do not know the dynamic record of access in a period of time, the following describes how to write a different period of time to dynamically display the number of traffic methods.
  
To record the traffic, first set up a database in MySQL (the best combination with PHP), give the database a name of line, and set up a data table called line, where the fields are "user name (name varchar (20)), Time datetime). Of course the reader can also add fields to the data table as needed.
  
Set up a good database, you can begin to design the program, now to clear up the idea, to show the traffic, of course, the database must have records, I have assumed that the reader has the ability to write a user login program, so to add records to the database can be in the login procedure is assumed to be login.php (as the current mainstream development language ) Add:
  
Pay the present time first: $time =date (y-m-d h:i:s);
MySQL (the best combination of PHP collocation) _select_db (line);
MySQL (best combination with PHP) _query ("INSERT into line (Name,time) VALUES ($name, $time)");
  
OK, now every logged in user has a record in the database, the following to complete the user online display of the program line.php (as the current mainstream development language):
  
   MySQL (and PHP collocation of the best combination) _connect ("local", "", "");
MySQL (the best combination of PHP collocation) _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, one of the problems is that if a user has been continuously executing line.php (as the current mainstream development language) for more than 5 minutes, the program must identify the user and always show the user, where it is necessary to make use of cookies to update the database time record, Because it is logged in the authentication, so there will be a cookie to remember the user's information, assuming that the user name of the cookie variable is $cookiename (the specific variable depends on the setting of the cookie), the rest is very good, use this cookie variable to complete the database modification :
  
Update line set time= $time where Name= $cookiename
  
below to refine line.php (as the current mainstream development language):
  
   Set the current time
$time =date (y-m-d h:i:s);
MySQL (and PHP collocation of the best combination) _connect ("local", "", "");
MySQL (the best combination of PHP collocation) _select_db (line);
  
Update a user's record
MySQL (the best combination of PHP collocation) _query ("Update line set time= $time where Name= $cookiename");
  
Delete user records for more than 5 minutes
MySQL (the best combination of PHP collocation) _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 (best combination with PHP) _result ($result, $i, "name"); echo " "; }}?> the above program has been able to show all the number of online users and user names, of course, this program is not perfect. If one of the users logs out, the database should not have this user's record. So, the logout program is assumed to be logout.php (as the current mainstream development language)   Plus Delete function: MySQL (and PHP collocation of the best combination) _select_db (line);     MySQL (the best combination of PHP collocation) _query ("Delete from line where name= $name"); At this time a basic user online function has been completed, and then continue to line.php (as the current mainstream development language) to add code to make the function more perfect, first we have to specify how long the user has not continued to browse line.php (as the mainstream development language now) that the user has been left, Given a time limit of 5 minutes, which means that the program will display the first 5 minutes of user situation from now on, so it must be line.php (as the current mainstream development language) to set a current time to inform the program from this time to execute, Then implement the program execution when the time of recording in the database minus the current time greater than 5 minutes of all records deleted, so that any user in the execution of line.php (as the current mainstream development language), can see all the online users within 5 minutes, to complete this function requires the following database statement: Delet E From line where time
";
echo "Number of online now: $num";
echo "
Users: $name









";   for ($i =0; $i < $num; $i + +) {$name =mysql (best combination with PHP) _result ($result, $i, "name"); echo " "; }}?> good user online display function completed.
";
echo "Number of online now: $num";
echo "
Users: $name

http://www.bkjia.com/PHPjc/509085.html www.bkjia.com true http://www.bkjia.com/PHPjc/509085.html techarticle at the beginning of this article, the author assumes that the reader has been able to write a user's login authentication program.----------------------------------------------------------register to complete access to the web ...

  • 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.