Home >Backend Development >PHP Tutorial >Detailed explanation of how to implement user online time statistics in PHP

Detailed explanation of how to implement user online time statistics in PHP

高洛峰
高洛峰Original
2016-11-30 13:28:351844browse

First introduce the data table structure involved, four fields:
The code is as follows:
uid: user id
session_id: session_id generated by the system after the user logs in, PHP However, use the session_id() function to obtain
login_time: login time
logout_time : logout time

1. The client regularly sends requests to the server. 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. Set up a scheduled polling script on the server. 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, and 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 acts, that is, every time he clicks on a page, if the session exists, that is, when he is logged in, the user's logout time is updated
The code is 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 update operation of the database and increases the load on the system, but it should not be a problem for small and medium-sized websites.

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