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

An attempt to count user online time under PHP_PHP Tutorial

WBOY
Release: 2016-07-21 15:34:11
Original
1043 people have browsed it

Listed below are some of the more commonly used methods:

First introduce the data table structure involved, the four fields:

Copy 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 session_id () function obtains
login_time: login time
logout_time: logout time

1. The client sends requests to server side. 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, use the current time as the login time, and the current time + 600s as the logout time, and insert them into the database.
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 activity, 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
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 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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322457.htmlTechArticleThe following are some of the more commonly used methods: First, let’s introduce the data table structure involved, the four fields: Copy the code as follows: uidint(10): user id session_idvarchar(40): use...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template