首先需要在用户表中建两个字段,session_id和logintime,session_id存用户当前登录的session_id,logintime存最后一次登录的时间。
逻辑是这样,当用户在A地方登录,就把用户登录的session_id,logintime更新到数据库中。当这个用户在B地方登录,session_id和logintime也会更新到数据库中,现在数据库中存的是最新的B地方登录的session_id。这样之前在A地方登录的用户,session中的session_id跟数据库中存的sesssion_id就不一样了。我们只需要判断用户在session中的session_id跟数据库中存的session_id是否一样,如果不一样,就清空登录的session,并且跳转到登录页。
Login.php(登录控制器)
<?php
namespace app\index\controller;
use think\Controller;
class Login extends Controller
{
public function index()
{
return $this->fetch();
}
public function login()
{
$username = input('post.username');
$password = input('post.password');
$user = db('user')->where(['username'=>$username,'password'=>$password])->find();
if(!$user){
return ['status'=>0,'msg'=>'用户名或密码错误'];
}
session('username',$username);
$session_id = session_id();//获取到seesion_id
db('user')->where('id',$user['id'])->update(['logintime'=>time(),'session_id'=>$session_id]);
return ['status'=>1,'msg'=>'登录成功'];
}
}
namespace app\index\controller;
use think\Controller;
class Login extends Controller
{
public function index()
{
return $this->fetch();
}
public function login()
{
$username = input('post.username');
$password = input('post.password');
$user = db('user')->where(['username'=>$username,'password'=>$password])->find();
if(!$user){
return ['status'=>0,'msg'=>'用户名或密码错误'];
}
session('username',$username);
$session_id = session_id();//获取到seesion_id
db('user')->where('id',$user['id'])->update(['logintime'=>time(),'session_id'=>$session_id]);
return ['status'=>1,'msg'=>'登录成功'];
}
}
index.html(登录页面前端代码)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户登录</title>
</head>
<body>
用户名:<input type="text" name="username" id="username"><br />
密码:<input type="password" name="password" id="password"><br />
<button id="btn">登录</button>
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js">
</script>
<script>
$('#btn').click(function(){
var username = $('#username').val();
var password = $('#password').val();
$.post('/index.php/index/login/login',{
'username':username,
'password':password
},function(data){
if(data.status == 1){
alert(data.msg);
setTimeout(function(){
window.location.href = '/';
},1000);
}else{
alert(data.msg);
}
});
});
</script>
</body>
</html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户登录</title>
</head>
<body>
用户名:<input type="text" name="username" id="username"><br />
密码:<input type="password" name="password" id="password"><br />
<button id="btn">登录</button>
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js">
</script>
<script>
$('#btn').click(function(){
var username = $('#username').val();
var password = $('#password').val();
$.post('/index.php/index/login/login',{
'username':username,
'password':password
},function(data){
if(data.status == 1){
alert(data.msg);
setTimeout(function(){
window.location.href = '/';
},1000);
}else{
alert(data.msg);
}
});
});
</script>
</body>
</html>
Common.php(需要登录的页面继承的公共控制器)
<?php
namespace app\index\controller;
use think\Controller;
class Common extends Controller
{
protected function initialize()
{
if(!session('?username')){
$this->redirect('/index.php/index/login/index');
}
$sessionName = session('username');
$currentuser = db('user')->where(['username'=>$sessionName])->find();//当前登录的用户
$session_id = $currentuser['session_id'];
if($session_id != session_id()){
session('username',null);
$this->redirect('/index.php/index/login/index');
}
}
}
namespace app\index\controller;
use think\Controller;
class Common extends Controller
{
protected function initialize()
{
if(!session('?username')){
$this->redirect('/index.php/index/login/index');
}
$sessionName = session('username');
$currentuser = db('user')->where(['username'=>$sessionName])->find();//当前登录的用户
$session_id = $currentuser['session_id'];
if($session_id != session_id()){
session('username',null);
$this->redirect('/index.php/index/login/index');
}
}
}