Php provides detailed online time statistics for users

Source: Internet
Author: User
Php provides detailed online time statistics for users. For more information, see. First, we will introduce the structure of the involved data table, with four fields:
The code is as follows:

The code is as follows:


Uid : User ID
Session_id : Session_id generated by the system after the user logs on, which is obtained by using the session_id () function in PHP.
Login_time : Logon time
Logout_time : Logout time


1. the client regularly sends requests to the server. After a user logs on, insert uid, session_id, and login_time into a record and set a timer in the client js. for example, a request is sent to the server every 10 minutes, to update the logout time, of course, the shorter the interval, the more accurate the data may be, but the load of the corresponding system will be higher, you can set an appropriate value based on the actual situation. This method is widely used in webgame because almost all requests of webgame are ajax requests and do not need to refresh the page. Once the page is refreshed, this timer loses value, which is also the limitation of this method.

2. set a script for regular polling. This method is to write a script for regular execution on the server, for example, to execute it once every five minutes, and determine whether the session_id of each session still exists on the server based on the records in the database, if logout_time exists, logout_time is updated. if logout_time does not exist, skip this step. In this way, you can accurately calculate the online time, but the disadvantage is that you need to have control over the server. Otherwise, you cannot set a scheduled script. in linux, you can use crontab, and in windows, you can schedule tasks. If you only buy a virtual host, this method is also not suitable for you.

3. update the logout time during each activity. In this way, when the user does not activity or exits, the logout time naturally exists in the database. this is also the solution discussed in this article. The following shows the implementation method.
First, after the user successfully logs on, record his uid and session_id, and use the current time as the login time, and the current time s as the logout time to insert the database.
The code is as follows:

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, when the user clicks a page during each activity, if the session exists, that is, when the user is logged on, the user logout time is updated.
The code is as follows:

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 and can be applied to most websites without additional server requirements. It can also accurately count users' online time.

The disadvantage is also obvious. it increases database update operations and system load, but it should not be a problem for small and medium-sized websites.

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.