thinkphp框架同一個賬戶在一個瀏覽器中登錄。A瀏覽器登錄了,B登錄A下線,

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模塊中


<?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;
            }
        }
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章