Write a program that users display online _php tutorial

Source: Internet
Author: User
Tags set time
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, let's call this database line, and create a data table named line, the table field is "User name (name varchar (20)), Time (DateTime)". Of course, readers can also add fields to the data table as needed.

Set up a good database, you can start the design process, now to clear up the idea, to show the 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 to add records to the database can be added to the login program login.php:

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 login user has a record in the database, the following is 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 "














minute)

However, one of the problems is that if a user has been executing line.php for more than 5 minutes, the program must identify the user and always show the user, in which case the cookie is used to update the database's time record, because it is logged into the authentication, So there will be a cookie to remember the user's information, assuming that this record user name of the cookie variable is $cookiename (the specific variable depends on the cookie settings), the rest is very good, use this cookie variable to complete the database modification:
Br>update line set time= ' $time ' where name= ' $cookiename '

to refine line.php:

!--? //Set Current time
$time =date (' y-m-d h:i:s ');
mysql_connect ("local", "", "");
mysql_select_db (line);

//Update the user's record
mysql_query ("Update line set time= ' $time ' where name= ' $cookiename '");

//delete user records for more than 5 minutes
mysql_query ("Delete from line where time
$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 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 assumes that the logout.php is added with the delete function: Mysql_ select_db (line); mysql_query ("Delete from line where name= ' $name '"); At this time a basic user online functionality has been completed, and then continue to add code within the line.php to make the function more perfect, first we have to specify how long the user has not continued to browse line.php when the user is considered to have left, here given a limit of 5 minutes, In other words, the program will display the first 5 minutes from the beginning of the user situation, so must be set within the line.php a time to inform the program from this time to execute, and then implement the program execution when the database recorded in the time less than the current time of more than 5 minutes of all records deleted, so that any user in the execution line.php, you can see all the online users within 5 minutes, and the following database statement is required to complete this function: Delete from line where time
";
echo "Number of online now: $num";
echo "
Users: $name








http://www.bkjia.com/PHPjc/315471.html www.bkjia.com true http://www.bkjia.com/PHPjc/315471.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 Web pages ...

  • "; for ($i =0; $i < $num; $i + +) {$name =mysql_result ($result, $i, "name"), echo " "; } } ? > Good user online display function completed.
    ";
    echo "Number of online now: $num";
    echo "
    Users: $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.