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:
Delete from line where time<date_sub (' $time ', Interval 5
Minute
But one of the problems is that if a user keeps executing line.php for more than 5 minutes, the program must be able to identify the user and always display the user, where it is necessary to use cookies to implement the time record of the update database, 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 cookie variable for the $cookiename (the specific variable depending on the cookie settings), the rest is very good to do, using this cookie variable to complete the database modification:
Update line set time= ' $time ' where name= ' $cookiename '
below to improve line.php:
?
Set the current time
$time =date (' y-m-d h:i:s ');
Mysql_connect ("local", "" "," ");
mysql_select_db (line);
Update a 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<date_sub (' $time ', interval 5 minute)");
$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>";
}
}
?>
Good user online display function completed. Turn from: Dynamic Network production guide www.knowsky.com
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.