[英]Count multiple login and logout session
i am designing a system where user shall register for a particular period and then logout. 我正在设计一个系统,用户应在该系统中注册特定的时间,然后注销。 he may login and logout multiple times in day.
他一天中可能会多次登录和注销。 i have to count all of those session to measure performance.
我必须数所有这些会话以衡量性能。
so what would be best way to design database for it 所以最好的方式为此设计数据库
create table scheduler(
ID bigint auto_increment,
userID varchar(100),
start_session TIMESTAMP,
end_session TIMESTAMP,
primary key(ID),
INDEX(userID)
)
i have to insert multiple session data of a user. 我必须插入一个用户的多个会话数据。 so is it logical design for my desired task?
那么对我想要的任务而言,这是合乎逻辑的设计吗? one more info this will be for a real time system.
一个更多的信息,这将是一个实时系统。
create table lets say "logins" with few fields, one should be: create table允许说“ logins”且字段很少,一个应该是:
entrie_id entrie_id
userID (which bilongs to user to actualy know which user we talking about) userID(实际上是用户的双向标识符)
Date (which represend current day) 日期(代表当前日期)
login_amount (which should be increased each time user logins) default value 0. login_amount(每次用户登录时应增加)默认值0。
with this you will have statics day by day. 有了这个,您将每天都有静电。
Include updating database after user logins. 用户登录后包括更新数据库。
For example if the user logins with ID 1 and today is 2014-02-14. 例如,如果用户使用ID 1登录并且今天是2014-02-14。
You will have to search for userID = 1 and data 2014-02-14 (which is today in our case) in logins table, if it gets false by searching these, create new entrie with today date stam and user id, else update logins_amount by 1 so if it was first login ever it should do 0 + 1 so you get total logins of 1. 您将必须在登录表中搜索userID = 1和数据2014-02-14(在我们的情况下为今天),如果通过搜索这些错误而创建,请使用今天的日期和用户ID创建新条目,否则更新logins_amount乘以1,因此,如果是首次登录,则应该执行0 + 1,这样您的总登录数为1。
edit: For logout obviously you can do same just in logout script section. 编辑:显然,对于注销,您可以只在注销脚本部分执行相同的操作。 for that you may need to add another field logout_amount etc.
为此,您可能需要添加另一个字段logout_amount等。
Unless you're using websockets there's no way to tell when the user actually disconnects, so I can't see how end_session would ever be meaningful. 除非您使用websocket,否则无法告诉用户何时断开连接,因此我看不到end_session的意义。 If you had a third table called 'visit' however, that could capture all http requests under a given session:
但是,如果您有名为“ visit”的第三个表,则可以捕获给定会话下的所有http请求:
CREATE TABLE user(
id BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL,
email VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL,
salt VARCHAR(255) NOT NULL,
created DATETIME NOT NULL
);
CREATE TABLE session(
id BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL,
user BIGINT NOT NULL,
created DATETIME NOT NULL
index(userid)
);
CREATE TABLE visit(
id BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL,
ip VARCHAR(55) NOT NULL,
uri VARCHAR(255) NOT NULL,
method VARCHAR(20) NOT NULL,
userAgent TEXT NOT NULL,
session BIGINT NULL,
user BIGINT NULL,
created DATETIME NOT NULL
index(userid)
);
This would let you capture meaningful data. 这将使您捕获有意义的数据。 Also if you're concerned about a particular session expiring after X minutes, just add X minutes to the created time when you're validating a user's session.
同样,如果您担心某个特定会话在X分钟后到期,则在验证用户的会话时,只需在创建的时间上加上X分钟即可。 Or when you're looking for a session you can query with a condition on the created time:
或者,当您正在寻找会话时,您可以查询所创建时间的条件:
SELECT
id,
user,
created
FROM session
WHERE created > {recent}
Where recent would be equal to the current time - X minutes. 最近的时间等于当前时间-X分钟。
For better security I would recommend generating a random token for each session, set the user's cookie value to an AES GCM encryption of the user's id and session token, then generate a random salt to hash the session token with, finally store the hashed session token + salt in the database. 为了获得更好的安全性,我建议为每个会话生成一个随机令牌,将用户的cookie值设置为用户ID和会话令牌的AES GCM加密,然后生成一个随机的盐来对会话令牌进行哈希处理,最后存储哈希的会话令牌+盐在数据库中。 If you merely compare the session's id to the value of the cookie I can authenticate to your system as any user... same thing with if you use a randomly generated token.
如果您仅将会话的ID与cookie的值进行比较,那么我可以以任何用户的身份向您的系统进行身份验证...如果您使用随机生成的令牌,则可以做到这一点。
Based on that I'd modify the session table to look more like this: 基于此,我将修改会话表,使其看起来像这样:
CREATE TABLE session(
id BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL,
user BIGINT NOT NULL,
token VARCHAR(255) NOT NULL,
salt VARCHAR(255) NOT NULL,
created DATETIME NOT NULL
index(userid)
);
Lastly, I noticed you're using TIMESTAMP in your schema... that's generally fine, just be aware that you can only store dates from 1970 to 2038. 最后,我注意到您在您的模式中使用TIMESTAMP ...这通常很好,请注意您只能存储1970年到2038年的日期。
If you want to see a working example of this, I have it on github here: https://github.com/kaeawc/play-encryption 如果您想查看此示例,请在github上的https://github.com/kaeawc/play-encryption上找到。
There is a working demo here: http://immense-garden-9877.herokuapp.com/ 这里有一个工作示例: http : //immense-garden-9877.herokuapp.com/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.