thinkphp框架同一个账户在一个浏览器中登录。A浏览器登录了,B登录A下线,

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_28761593/article/details/60960068

common控制器

$admin_db      = D('Admin');
$userid = session('userid');
$map = array();
$map["session_id"] = session_id();
$exists = $admin_db->where(array('userid'=>$userid))->field('session_id')->find();//获取已经存入数据库IP
if($map != $exists){
    session('userid', null);
    session('roleid', null);
    cookie('username', null);
    cookie('userid', null);
    $this->success('您已经被迫下线!', U('Index/login'));
}

前台html

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="author" content="wangdong">
<title><{:C('SYSTEM_NAME')}> - 用户登录</title>
<include file="Common:head"/>
<style type="text/css">
form{width:280px;height:120px;margin:30px auto 0;}
form div label{float:left;display:block;width:65px;font-size:16px;padding-top:6px;}
form div{margin:8px auto;}
form div.input input{height:21px;padding:2px 3px;}
form div.input img{cursor:pointer}
#username,#password{width:200px;}
#code{width:68px}
</style>
</head>
<body>
<div class="easyui-dialog" title="用户登录" style="width:380px;height:240px" data-options="closable:false,iconCls:'icons-lock-lock',buttons:[{text:'登录',iconCls:'icons-user-user_go',handler:login}]">
   <form id='form' method="post">
      <div class="input">
         <label for="username">用户名:</label>
           <input type="text" name="username" id="username" value="admin" />
      </div>
      <div class="input">
         <label for="password">&nbsp;&nbsp;码:</label>
           <input type="password" name="password" id="password" value="admin@123" />
      </div>
      <div class="input">
         <label for="code">验证码:</label>
           <input type="text" name="code" id="code" size="4" />
           <span style="margin-left:10px"><img id="code_img" align="top" onclick="changeCode()" src="<{:U('Index/code?code_len=4&font_size=14&width=100&height=28&code='.time())}>" title="点击切换验证码"></span>
      </div>
   </form>
</div>

</div>
<script type="text/javascript">
$(function(){
   $('input:text:first').focus();
   $('form').keyup(function(event){
      if(event.keyCode ==13){
         login();
      }
   });
})
var changeCode = function(){
   var that = document.getElementById('code_img');
   that.src = that.src + '&' + Math.random();
}
var login = function(){
   if(!$('#username').val()){
      $.messager.alert('提示信息', '请填写用户名', 'error');
      return false;
   }
   if(!$('#password').val()){
      $.messager.alert('提示信息', '请填写密码', 'error');
      return false;
   }
   if(!$('#code').val()){
      $.messager.alert('提示信息', '请填写验证码', 'error');
      return false;
   }
   var loginIn = $.post('<{:U('Index/login?dosubmit=1')}>', $("form").serialize(), function(data){

        if(!data.status){
         $.messager.alert('提示信息', data.info, 'error',function(){
                var erroinfo = "<{:C('LOGIN_STATUS_ERROR')}>";
                if(data.info==erroinfo){
                    $.messager.confirm("提示", "是否强制登录?", function (data) {
                        if (data) {
                           //var username = document.getElementById("username").value;
                            $.post(
                                    '<{:U('Update/index/index?dosubmit=1')}>',
                                    $("form").serializeArray(),
                                    function(res){
                                        if(res == 1){
                                            $.messager.alert("提示","现在可以登录了!",'info');
                                        }else{
                                            return false;
                                         }
                                    }
                            )
                        }
                    });
                }
            });
         changeCode();
      }else{
         $.messager.progress({text:'加载中,请稍候...'});
         window.location.href = data.url;
      }
   },'json');
   return false;
}
</script>
</body>
</html>

<{:C('LOGIN_STATUS_ERROR')}>
config中
'LOGIN_STATUS_ERROR'    =>'该用户已在其它地方登录',



最后还有一个修改,用于修改登录状态。放于其它模块Update,之前的都是admin模块中

 
 

扫描二维码关注公众号,回复: 5361773 查看本文章

<?php
namespace Update\Controller;
use Think\Controller;

class IndexController extends Controller {
    public function Index(){
        if(I('get.dosubmit')){
            $where = array();
            $where['username'] = I('post.username');

            $admin = D('Admin');
            $save1 = array();
            $save1['status'] = "0";
            $save1['lastloginip'] = get_client_ip();
            $save1['datetime'] = time();
            $result = $admin->where($where)->save($save1);
            if($result){
                echo 1;
            }else{
                echo 0;
            }
        }
    }
}


猜你喜欢

转载自blog.csdn.net/qq_28761593/article/details/60960068