Home >Backend Development >PHP Tutorial >Online Beijing time An attempt to count user online time under PHP

Online Beijing time An attempt to count user online time under PHP

WBOY
WBOYOriginal
2016-07-29 08:43:411053browse

Listed below are some of the more commonly used methods:
First introduce the data table structure involved, the four fields:

Copy the code The code is as follows:


uid: user id
session_id: The session_id generated by the system after the user logs in. PHP can use the session_id() function to obtain it.
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 is active. 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, the current time + 600s as the logout time, and insert it into the database.

Copy the 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, update the user's logout time

Copy code 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 sessi
mysql_query($sql);
}


The advantage of this method is that it is relatively simple to implement and can be applied to large Most websites do not require additional servers, 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 used for small and medium-sized websites. question.
The above introduces an attempt to count users' online time under online Beijing time in PHP, including the content of online Beijing time. I hope it will be helpful to friends who are interested in PHP tutorials.

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