PHP Advanced: Write a user online display program

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 authentication program.
----------------------------------------------------------
Registers can complete the total number of times to access the Web page, but do not know the amount of traffic in a period of dynamic records, the following is a description of how to write a different period of time to dynamically display the amount of access to the method.
To record the amount of traffic, first of all, in MySQL to establish a database, give this database named line, while establishing a data table named line, the field in the table is "user name (name varchar (20)), Time (datetime)." Of course, readers can also add fields to the datasheet as needed.
After the database is established, you can begin to design the program, now first to clarify the train of thought, to show the number of visits, of course, the database must have records, I have assumed that the reader has the ability to write a user login program, so add a record to the database can be in the login program hypothesis for login.php add:
Pay the present time first: $time =date (' y-m-d h:i:s ');
mysql_select_db (line);
mysql_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 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 "The number of online now: $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 program has been able to show the number of users and the user name of all online, of course, the program is still very imperfect. If one of the users logs out, the database should not have a record of the user. Therefore, it is also necessary to add the deletion function to the logout.php in the login procedure:
mysql_select_db (line);
mysql_query ("Delete from line where name= ' $name '");
At this time a basic user online function has been completed, and then continue to add code in the line.php to make the function more perfect, first we have to specify how long the user did not continue to browse the line.php that the user has left, here given a limit of 5 minutes, That is, the program will show the first 5 minutes from now the user situation, so must be set within the line.php now time to tell the program to start from this time, and then implement the program execution of the database records in the time minus now more than 5 minutes of all records deleted, so that any user in the execution When line.php, you can see all the online users within 5 minutes, the following database statement is required to complete this function:

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.