简体   繁体   English

如何限制 Laravel 4 中每个用户帐户的同时会话数

[英]How to limit number of simultaneous sessions per user account in Laravel 4

I'm working on a project that will be subscription based and the accounts need to have a limit of 2 concurrent sessions per user .我正在开发一个基于订阅的项目,并且每个用户的帐户需要限制为 2 个并发会话

I have some ideas but am not entirely sure how to do this and maybe there is some built in way to do this that I am unaware of.我有一些想法,但我不完全确定如何做到这一点,也许有一些我不知道的内置方法可以做到这一点。

Any suggestions are welcome.欢迎任何建议。

I was thinking that if I switch the session driver to database and then extend the Auth or Session driver to store some extra info in the sessions table, like user id, that this would be somewhat easy to do.我在想,如果我将会话驱动程序切换到数据库,然后扩展身份验证或会话驱动程序以在会话表中存储一些额外的信息,比如用户 ID,那么这会很容易做到。

Not entirely sure how to do this either though so any help would be greatly appreciated!也不完全确定如何做到这一点,所以任何帮助将不胜感激!

I think you talking about multi login from different IPs/Devices?我认为您在谈论来自不同 IP/设备的多重登录? Thats what i think about right now.这就是我现在所想的。 You cannot count "Browser Tabs" or something like that ...你不能算“浏览器标签”或类似的东西......

So, if this is your case your solution is such easy.因此,如果这是您的情况,那么您的解决方案非常简单。 Bind any user login on an active record in a an database or an other persistence.将任何用户登录绑定到数据库中的活动记录或其他持久性。

DB-Model:数据库型号:

userSession (
    'id', // primary key
    'userId', //foreign key
    'sid', //unique session id
    'ip', //unique user id or uniqure device (I preffer IP!)
    'login', //datetime
    'lastClick' //datetime
);

An active session is open if a active record in db for that user exists.如果该用户的 db 中存在活动记录,则活动会话将打开。 You need to check for the given db user record on any user action (HTTP-Request I think).您需要在任何用户操作(我认为是 HTTP 请求)上检查给定的 db 用户记录。 You are able to count active user session for one user now.您现在可以计算一个用户的活动用户会话。 You are also able to kick user now by delete the depending userSession record.您现在还可以通过删除依赖的 userSession 记录来踢用户。

This can be achieved either with sql using the UPDATE in a table and have a column set to active sessions and php check if the sessions are more than 2, if they are deny login, or the other way.这可以通过在表中使用 UPDATE 的 sql 来实现,并将一列设置为活动会话,php 检查会话是否超过 2 个,如果它们被拒绝登录,或者其他方式。

<?php
function checkSessions(){
mysql_connect(data);
$q = "SELECT * FROM users WHERE username=$username && password=$password";
 while($row = mysql_fetch_array($q)){
 if($row["Active Sessions"] >2){
 die("There are too many people using the account right now");
}

You get the idea this is a sample.你明白这是一个样本。

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

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