Home >Backend Development >PHP Tutorial >Write a program for user online display_PHP tutorial

Write a program for user online display_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 16:06:09818browse

At the beginning of this article, the author assumes that the reader can already write a user login authentication program.
-------------------------- --------------------------------
The counter can complete the total number of times the web page is visited, 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, we must first create a database in mysql. Let's give This database is named line, and a data table named line is created. The fields in the table are "user name (name varchar (20)), time (time datetime)". Of course, readers can also add data tables as needed. field.

After establishing the database, you can start designing the program. Now let’s clarify the ideas. 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 The user has logged in to the program, so adding records to the database can be done in the login.php file:

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 everyone Logged-in users have a record in the database. Let’s complete the program line.php for online user display:

mysql_connect("local","","");
mysql_select_db(line);
$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 can display the number of all online users and their respective User name, of course, this program is still very imperfect. If one of the users logs out, the database should not have a record of this user. Therefore, a delete function must be added to the logout program, assuming it is 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, and then continue on line Add code to .php to make the function more complete. First, we have to specify how long the user will be considered to have left when he does not continue to browse line.php. Here, a time limit of 5 minutes is given, which means that the program will display the The user situation is 5 minutes before now, so a current time must be set in line.php to tell the program to start executing from this time, 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 any user can see all online users within 5 minutes when executing line.php. To complete this function, the following database statement is required:

delete from line where timeminute)

But there is another problem is that if a user has been executing line.php for more than 5 minutes, the program must identify the user and continue To display the user, you have to use cookies to update the time record of the database. Because it is login authentication, there will be a cookie to remember the user's information. Assume that the cookie variable that records the user's name is $cookiename ( The specific variables depend on the cookie settings), and the rest is easy to handle. Use this cookie variable to complete the database modification:

update line set time='$time' where name='$cookiename '

Let’s improve line.php:

//Set the current time
$time=date('Y-m-d H:i:s');
mysql_connect("local","","");
mysql_select_db(line);

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

//Delete user records older than 5 minutes
mysql_query("delete from line where time
$result=mysql_query("select * from line");
$num=mysql_numrows($result);
if (!empty($num)) {
echo "
";
echo "The number of people online now is: $num";
echo "
";
for ($i=0;$i<$num;$i++){
$name=mysql_result($result,$i,"name");
echo "";
}
}
?>

Good user online display function completed.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/315471.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...
Statement:
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
";
echo "The number of people online now is: $num";
echo "
User: $name