PHP to achieve user online time statistics detailed _php skills

Source: Internet
Author: User
Tags current time
First, introduce the data table structure involved, four fields:
The code is as follows:
Copy Code code as follows:

Uid<int >: User ID
Session_id<varchar >: The system produces session_id,php after the user logs on, but uses the session_id () function to obtain
Login_time<int (>): Logon Hours
Logout_time<int >: Logout time

1, the client sends the request to the server side regularly. The implementation method is after the user login, insert uid,session_id,login_time a record, and then set a timer on 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 set The more accurate the data may be, the higher the load will be for the corresponding system, which can be set to a suitable value based on the actual situation. This method is widely used on webgame, because almost all of the Webgame requests are AJAX requests, do not refresh the page, once the page is refreshed, the timer lost value, this is the limitation of this method.

2, the service set a regular polling script. This method is to write a timed script on the server, such as 5 minutes to execute, according to the records in the database to determine whether the session_id of each session is still 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, or can not set the timing of the script, Linux system can be implemented through CRONTAB, Windows system can be completed by planning tasks. If you just buy the virtual host, then this method is also not suitable for you.

3. Update the logout time every time the user is active. In this way, when the user does not move or quit, the logout time will naturally exist in the database, which is also the focus of this article discussion of the program. The implementation method is given below.
First, after the user login successfully, record their uid,session_id, and the current time as the landing time, now time 600s as logout time, insert the database.
The code is as follows:
Copy 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);

And then in each activity of the user, that is, every click of a page, if the session exists that is in the login status, update the user log out time
The code is as follows:
Copy Code code 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 approach is relatively simple to implement, can be applied to most Web sites, no additional server requirements, but also can be more accurate statistics of users online time.

Shortcomings are also obvious, increased the database update operation, increased the system load, but for small and medium-sized sites 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.