PHP implementation of user online time statistics detailed

Source: Internet
Author: User
First introduce the data table structure involved, four fields:
The code is as follows:
Uid<int (>): User ID
Session_id<varchar >: session_id,php generated by the user after login is obtained using the session_id () function
Login_time<int (>): Login time
Logout_time<int (>): Logout time

1, the client sends the request to the server side periodically. The implementation of the method is to insert a record after the user login, and then set a timer on the client JS, such as every 10 minutes to the server to send a request, so as to achieve the purpose of updating the logout time, of course, the shorter the interval setting, the Uid,session_id,login_time The more accurate the data may be, the higher the load on the corresponding system, which can set a suitable value according to the actual situation. This method is widely used in webgame, because almost all of Webgame's requests are AJAX requests, without refreshing the page, once the page is refreshed, the timer loses its value, which is also the limitation of this method.

2, the service set a timed polling script. This method is written on the server side of a timed execution of the script, such as 5 minutes to execute, according to the records in the database to determine whether the session_id of each session is still present on the server, if there is update logout_time, does not exist to skip. This can also be more accurate statistics online time, but the disadvantage is the need to have the control of the server, otherwise unable to set the timing script, Linux system can be implemented through CRONTAB, Windows system can be completed by the scheduled task. If you are just buying a virtual host, then this method is also not suitable for you.

3. Update the logout time when the user is active each time. In this way, when the user is inactive or exiting, the log out time will naturally exist in the database, this is also the focus of this article discussion of the scheme. The implementation method is given below.
First, after the user login success, record its uid,session_id, and the current time as the login time, now time 600s as the logout time, insert the database.
The code is as follows:
Copy the code code 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 when the user each activity, that is, every click on a page, if the session exists that is in the login state, update the user log out time
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 relatively simple to achieve, can be applied to most of the Web site, no additional server requirements, but also can be more accurate statistics on the user's online time.

The disadvantage is also obvious, increase the database update operation, increase the load of the system, but for small and medium-sized web site should not be a problem.
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.