关于单点登录的理解(强行登录,T别人下线)

第一种

1:首先的第一个人登录,设置登录状态,session,保存其登录的时间,并写到数据库中

2:第二个人登录,检测是否有人登录,如果有人登录,就T他下线,把自己的登录时间写入到数据库中

3:在判断登录状态的时候判断登录时间是否和数据库的登录时间一样,不一样的,session设置过期,使之下线

第二种:

1,同上1

2:第二个人登录,检测是否有人登录,有人登录,并且其在一个小时内有过访问,则自己不能登录。如果上个用户一小时都没有访问,则自己可以登录

3:如果某个人意外退出了,自己可以登录,同ip的其他电脑是不能登录的。cookie记录

4:使用memcache记录每次的访问时间,供2使用。

5:记录每次登录的时间,ip。

6:同3,判断T下线。

 

 

 

附上部门代码:(以下代码基于zf2开发)

作为参考

登录代码

登录方法里

$cookieService = new \Init\Model\Cookie('/',null,3600);
  $cookieUser = $cookieService->getCookie('user');
       
        if ($login_type == 1) {//如果是管理员登录
            $res = $this->getAccount($code, $username, $password);
            if($cookieUser){
             if($cookieUser == $res['user_name']){//存在cookie,且和现在的相等//可以登录
              $cookieexists = true;
             }else{//存在cookie和当前的不相等   则需要进一步判断该新用户是否登录
              $cookieexists = false;
             }
            }else{//不存在cookie,需要进一步判断用户是否登录
             $cookieexists = false;//IE设置成true可以登录,说明ie没有cookie
            }
            if (!$res) {//管理员登录失败
                $login_result->loginMsg = "用户名或者密码错误,请注意登录角色是否正确!";
                $login_result->loginStatus = 2; //管理员登录失败
                $login_result->errorType   = 1;
                return $login_result;
            }
            else if(!$cookieexists){
             $last_access_time = \Init\Model\mem::getMem()->get($res['id'].'_'.$res['user_name'].'_last_access_time')?\Init\Model\mem::getMem()->get($res['id'].'_'.$res['user_name'].'_last_access_time'):'1970-01-01';
             if($res['islogin']=='1' &&  (strtotime($date)-strtotime($last_access_time))<=3600)             
             { //5月21号改动:同ip不能登录($res['last_login_ip']!=$_SERVER['REMOTE_ADDR']||1)
     //cookie不存在,不允许登录
              $login_result->loginMsg = "该用户已经登录,请确认是否为本人操作!";
              $login_result->loginStatus = 2; //管理员登录失败
              $login_result->errorType   = 4;
              return $login_result;
             }
            }
        
            //管理员登录成功
             //$data = $this->model->fetchOne(array('user_name' => $username, 'code' => $code));
               $cookieService->setCookie('user', $res['user_name']);
                $this->editMember(array('islogin'=>'1','last_login_time'=>$date,'last_login_ip'=>$_SERVER['REMOTE_ADDR']), $res['id']);
                \Init\Model\mem::getMem()->set($res['id'].'_'.$res['user_name'].'_last_access_time',$date,0,3600);
             


T人下线和更新memecache,cookie代码

zf2的module.php文件

  //记录最后访问时间
        $session = new \Zend\Session\Container();
        if($session->login){
         $current_ip = $_SERVER['REMOTE_ADDR'];
         $type = $session->login->user->type;
         $id = $session->login->user->id;
         $user_name = $session->login->user->user_name;
         
         $login = $session->login;
         $memberOper = new \User\Oper\MemberOper();
         $employeeOper = new \User\Oper\EmployeeOper();
         
         //判断ip是否相等,不相等被T下线。(和邦管理员与企业管理员)
         if($type==1||$type==2){
          $memberinfo = $memberOper->getMember($id);
          if($memberinfo['last_login_ip']!=$current_ip || $session->login->lastLoginTime !=$memberinfo['last_login_time']){
           $session->login = null;
           $session->t = 1;
           return;
          }
         }
         else
         {
          $memberinfo = $employeeOper->getEmp($id);
          if($memberinfo['account']['last_login_ip']!=$current_ip || $session->login->lastLoginTime !=$memberinfo['account']['last_login_time']){
           $session->login = null;
           $session->t = 1;
           return;
          }
         }
         \Init\Model\mem::getMem()->set($id.'_'.$user_name.'_last_access_time',date('Y-m-d H:i:s',time()),0,3600);
         $cookieService = new \Init\Model\Cookie('/',null,3600);
         $cookieService->setCookieExpire('user');
         $cookieService->setCookie('user', $user_name);

 

 

layout

里判断登录T人下线代码

$session  = new Zend\Session\Container();
$cookie = new \Init\Model\Cookie('/',null,3600);
$identity = $session->login->identity;
$userType = $session->login->user->type;
if (!$session->login->user) {
 $cookie->setCookieExpire('user');
 if(!$session->t){
    header("location: /login");
    exit;
 }
 if($session->t==1){
  $session->t = null;
  echo "<script>alert('您的账号在别处登录!被迫下线,请核实!');window.location= '/login';</script>";
  exit;
 }
}

 

登出的设置

 public function logoutAction()
 {
  $cookie = new \Init\Model\Cookie('/',null,3600);
  $session = new Container();
  $code = $session->login->user->code;
  //set this user not online,set last_logout_time;
  $type = $session->login->user->type;
  $id = $session->login->user->id;
  $user_name = $session->login->user->user_name;
  //登出的时候需要设置memcache
  \Init\Model\mem::getMem()->delete($id.'_'.$user_name.'_last_access_time');
  if($type==1 || $type==2)
  {//公司账号,hb_member
   $memberOper = new \User\Oper\MemberOper();
   $memberOper->editMember(array('islogin'=>'0','last_logout_time'=>date('Y-m-d H:i:s',time())), $id);
  }
  else
  {//员工账号,hb_employee
   $employeeOper = new \User\Oper\EmployeeOper();
   $employeeOper->updateAcc(array('islogin'=>'0','last_logout_time'=>date('Y-m-d H:i:s',time())), array('id'=>$id));
  }
  $session->login = null;
  $cookie ->setCookieExpire('user');
  return $this->redirect()->toRoute('home'); 
 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值