Home  >  Article  >  Backend Development  >  Detailed explanation of how to implement user online time statistics in PHP_PHP tutorial

Detailed explanation of how to implement user online time statistics in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:23:59723browse

First, let’s introduce the structure of the data table involved, the four fields:
The code is as follows:

Copy the code The code is as follows:

uid: user id
session_id: session_id generated by the system after the user logs in. PHP can use the session_id() function to obtain
login_time : Login time
logout_time : Logout time

1. The client sends requests to the server regularly. The implementation method is to insert uid, session_id, login_time into a record after the user logs in, and then set a timer in the client js, such as sending a request to the server every 10 minutes to achieve the purpose of updating the logout time. , of course, the shorter the interval is set, the more accurate the data may be, but the corresponding system load will be higher. This can be set to an appropriate value based on the actual situation. This method is widely used in webgames, because almost all requests in webgames are ajax requests, and there is no need to refresh the page. Once the page is refreshed, the timer loses its value, which is also the limitation of this method.

2. The server sets a scheduled polling script. This method is to write a scheduled execution script on the server side, for example, execute it once every 5 minutes. Based on the records in the database, it is judged whether the session_id of each session still exists on the server. If it exists, logout_time is updated. If it does not exist, it is skipped. This can also accurately count online time, but the disadvantage is that you need to have control of the server, otherwise you cannot set a timing script. Linux systems can do this through crontab, and Windows systems can do it through scheduled tasks. If you just bought a virtual host, then this method is not suitable for you either.

3. Update the logout time every time the user performs an activity. In this way, when the user is inactive or logs out, the logout time will naturally exist in the database. This is also the solution that this article focuses on. The implementation method is given below.
First, after the user successfully logs in, record his uid and session_id, use the current time as the login time, and the current time of 600s as the logout time, and insert it into the database.
The code is as follows:
Copy code The code is as follows:

$uid = $_SESSION[uid] = $info[ id];
$session_id = $_SESSION[session_id] = session_id();
$login_time = time();
$logout_time = time() 600;
$sql = "INSERT INTO member_login (uid,session_id,login_time,logout_time) values($uid,$session_id,$login_time,$logout_time)";
mysql_query($sql);

Then every time the user activates, That is, every time a page is clicked, if the session exists, that is, when the user is logged in, the user logout time
is updated as follows:
Copy code Code As follows:

if($_SESSION[uid]){
$uid = $_SESSION[uid];
$session_id = $_SESSION[session_id];
$logout_time = time() 600;
$sql = "UPDATE member_login SET logout_time=$logout_time WHERE uid=$uid AND session_id=$session_id";
mysql_query($sql);
}

The advantage of this method is that it is relatively simple to implement, can be applied to most websites, has no additional server requirements, and can also accurately count users’ online time.

The disadvantages are also obvious. It increases the database update operation and increases the system load, but it should not be a problem for small and medium-sized websites.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324376.htmlTechArticleFirst introduce the data table structure involved, four fields: The code is as follows: Copy the code The code is as follows: uidint( 10): User id session_idvarchar(40): Generated by the system after the user logs in...
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