thinkphp框架同一個賬戶在一個瀏覽器中登入。A瀏覽器登入了,B登入A下線,
阿新 • • 發佈:2019-01-03
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">密 碼:</label> <input type="password" name="password" id="password" value="[email protected]" /> </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; } } } }