PHP Advanced: Write a program for users to display online_PHP Tutorial
WBOY
Release: 2016-07-20 11:04:41
Original
812 people have browsed it
When starting this article, the author assumes that the reader can already write a user login authentication program. -------------------------- -------------------------------- The counter can complete the total number of visits to the web page, but it cannot After knowing the dynamic record of visits in a period, let’s introduce how to write a method to dynamically display the visits in each period. To record the visits, you must first create a database in mysql. Let’s get the data for this database. Named line, and a data table named line is created at the same time. The fields in the table are "user name (name varchar (20)), time (time datetime)". Of course, readers can also add fields to the data table as needed. After establishing the database, you can start designing the program. Now let’s clarify the idea. If you want to display 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 To add records to the database, you can add the following in the login program, assuming it is login.php: Pay the current time first: $time=date('Y-m-d H:i:s'); mysql_select_db(line); mysql_query("insert into line (name,time) values('$name','$time')"); Okay, now every logged-in user has a record in the database. Let's complete the program line.php for user online display:
mysql_connect("local","",""); mysql_select_db(line); $result=mysql_query ("select * from line"); $num=mysql_numrows($result); if (!empty($num)) { echo "
echo "The number of people online now is: $num"; echo "
" ; } } ?> The above program can already display the number of online users and their user names. Of course, this program is still far from perfect. If one of the users logs out, After that, the database should not have a record of this user. Therefore, the delete function must be added to the logout program, which is assumed to be logout.php: mysql_select_db(line); mysql_query("delete from line where name= '$name'"); At this time, a basic user online function has been completed. Next, continue to add code in line.php to make the function more complete. First, we have to specify how long the user does not continue to browse the line. php will consider that the user has left. Here, a time limit of 5 minutes is given, which means that the program will display the user situation in the first 5 minutes from now, so a current time must be set in line.php to tell the program to start from this time. Time starts executing, and then when the program is executed, the time recorded in the database minus the current time is deleted and all records greater than 5 minutes are deleted, so that when any user executes line.php, they can see all online users within 5 minutes. The following database statement is required to complete this function:
http://www.bkjia.com/PHPjc/445180.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445180.htmlTechArticleWhen starting this article, the author assumes that the reader can already write a user login authentication program. --- -------------------------------------------------- ----- The counter can complete access to the web page...
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn