简体   繁体   English

将用户登录-注销时间存储到数据库中的时间有所不同

[英]Storing user login-logout time into database with it's difference

I want to store user's login history on my site. 我想在我的网站上存储用户的登录历史记录。

table: 表:

mysql> desc user_stat;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | varchar(64) | YES  |     | NULL    |       |
| login  | varchar(64) | YES  |     | NULL    |       |
| logout | varchar(64) | YES  |     | NULL    |       |
| diff   | varchar(64) | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+

When user log In I perform insert. 当用户登录时,我执行插入。

insert into user_stat(id,login) values('".$id."','".NOW()."');

But for logout situation is confusing. 但是对于注销情况却令人困惑。 As user may have logged in twise in two diff browser. 由于用户可能已经在两个diff浏览器中登录了twise。 So how can I maintain logout time? 那么如何维护logout时间呢? something like session management (though I have not used session var for login logout). 类似于会话管理(尽管我没有使用session var进行登录注销)。 JS sdk for login logout I used : http://jsfiddle.net/YCFpH/ 我用于登录注销的JS SDK: http : //jsfiddle.net/YCFpH/

Also How can I get the differene of logout and login time in diff column? 另外,如何在diff列中获得注销和登录时间的diff

One way is to convert string of data-time into number and getting differece of it. 一种方法是将数据时间的字符串转换为数字,并使其与众不同。 But I dont what that method. 但是我没有那种方法。

Anyother solution for data time diff? 还有其他解决数据时间差异的方法吗?

But for logout situation is confusing. 但是对于注销情况却令人困惑。 As user may have logged in twise in two diff browser. 由于用户可能已经在两个diff浏览器中登录了twise。 So how can I maintain logout time? 那么如何维护注销时间呢?

Each Login must have some sort of session identifier. 每个登录名必须具有某种会话标识符。 Otherwise, you will never properly correlate your logout time with the correct login time. 否则,您将永远无法正确地将注销时间与正确的登录时间相关联。 There are any number of ways to get this and that will depend on your web server platform. 有很多方法可以做到这一点,这取决于您的Web服务器平台。 At the very least, you can create a uid on the client (see Create GUID / UUID in JavaScript? ) and pass the uid in when the user logs out. 至少,您可以在客户端上创建一个uid(请参阅在JavaScript中创建GUID / UUID? ),并在用户注销时将uid传递给它。

Additionally, if you are not sorting by or joining by the difference time, login and logout should likely be datetime datatypes and the difference simply calculated each time you want it instead of stored. 此外,如果您不是按时差排序或按时差排序,则登录和注销可能应该是日期时间数据类型,并且每次需要时都应简单地计算而不是存储时差。 For example. 例如。

    set @time1 = now();
    set @time2 = date_add(now(), interval 30 second);

    select @time1, @time2, (timediff(@time1,@time2))

Now you get the difference between the times in seconds. 现在,您可以得到以秒为单位的时间差。

2013-12-26 14:17:32 2013-12-26 14:18:02 -00:00:30

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM