java - 根据一张登录登出日志表计算每个用户在线时长,求个思路
阿神
阿神 2017-04-18 10:31:06
0
5
1726

有一张日志表,记录着每个用户的操作日志信息,例如登录或注销,每个操作都有对应的createtime,以及当前的userName

  • 需求如下:

    • 统计每个用户在一天内在线时长(登录到注销这段时间,可能一天登录注销多次)

    • 如果用户电脑直接关机,或者非正常关闭系统,这时日志表不会产生注销日志,所以将用户最后一次的操作日志(可能是增删改查)作为注销时间,进行在线时长的计算

  • 疑问:

    • 我觉得在日志记录的时候需要记录每次会话的sessionId,否则无法进行上面计算?

阿神
阿神

闭关修行中......

reply all(5)
Peter_Zhu

There is no need to record sessionid. Do you want to use sessionid to record the last operation time of abnormal exit?
The last piece of data logged in by the same user must be a logout operation
1. Normal logout, the next piece of data must be a login
2. After normal operations (add, delete, modify, check) and abnormal shutdown, the next time it must be a login operation

So the length of a login time is the current login time - the time of the previous record before the next login

However, if the user does not log in after abnormal shutdown, it needs to be processed separately. For example, the sessionid will expire if there is no operation for 20 minutes. Just check whether the last record is more than 20 minutes ago. If it is more than 20 minutes ago, it will be considered as an abnormal exit.

黄舟

Anyway, there must be user identification to determine which user’s behavior log it is,

When writing the log, write the time difference between each login and logout into the log, and add it up at the end?

Ty80

The table can be designed like this

用户id   登录时间  退出时间

When logging in, the login time is recorded in the session, and the exit time is set to: login time + timeout time.

When the user is active, based on the login time recorded in the session, the exit time of that record is updated: current time + timeout time.

When exiting, according to the login time recorded in the session, the exit time of that record is updated to: the current time.

巴扎黑

Original table (assumed):

record_id    uid    name      opr_type    c_time
1            10     cxl       land        2016-12-26 08:00:00
2            10     cxl       out         2016-12-26 23:00:00

The final generated table:

用户名    日期(asc)    当天第一次登陆时间     当天最后一次登出时间     登陆时长(结果非准确值)
cxl      2016-12-26    2016-12-26 08:00:00  2016-12-26 15:00:00   3小时10分
cxl      2016-12-27    2016-12-26 08:30:00  2016-12-26 23:00:00   6小时10分

Detailed process:

1. 日期分组
2. 当天第一次登陆时间
     2.1 第一种情况:若当天第一条记录操作类型是 登出,那么 当天第一次登陆时间 = 00:00:01
     2.2 第二种情况:若当天第一条记录操作类型是 登陆,那么 当天第一次登陆时间 = c_time
    
3. 当天最后一次登出时间
    3.1 第一种情况:若当天最后一条记录操作类型是 登陆,那么 当天最后一次登出时间 = 23:59:59
    3.2 第二种情况:若当天最后一条记录操作类型是 登出,那么 当天最后一次登出时间 = c_time
4. 登陆时长 
    4.1 当天最后一次登出时间 - 当天第一次登陆时间

Give me some ideas. I personally think it is not easy to implement. (Logic processing requires PHP, and paging is also troublesome. It has to be displayed in paging according to the date, instead of paging according to the number of records taken out like limit 1 , 10. Single paging sql cannot derive the final table structure), so we can only go here....

洪涛

Can I ask everyone a question? Login can be recorded, but how to record logout? The user may not actually click the logout button. If it is the web, the user can just close the browser and leave it alone. How do you know when you logged out? session?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!