thinkphp5 tp5 会话控制 session 登录 退出 检查检验登录 判断是否应该跳转到上次url...

 

<?php
namespace app\admin\controller;
use think\Db;
use think\Validate;
use think\Controller;
use think\facade\Session;
use app\admin\model\OrderModel;
 
class Order extends Controller
{
    public function initialize()
    {
        $event = controller('login','controller');
        $event->check();
    }   
 
    //用户登录
    public function index()
    {
        if($this->request->isAjax()){
            $account = $this->request->param('account');
            $password = $this->request->param('password');
            $admin = Db::table('admin')->where(['account'=>$account, 'password'=>$password])->find();
            if($admin['id']){
                Session::set('admin',$admin);
 
                //判断是否应该跳转到上次url
                if(!empty(Session::get('redirect_url'))){
                    $url = Session::get('redirect_url');
                    Session::delete('redirect_url');
                }else{
                    $url = url('index/index');
                }
 
                exit( json_encode(['check'=>1, 'msg'=>'登录成功!', 'url'=>$url]) );
                 
            }else{
                exit( json_encode(['check'=>0, 'msg'=>'账号或密码错误']) );
            }
        }
 
        if(is_numeric(Session::get('admin.id'))){
            $this->redirect('index/index');
        }
        return $this->fetch();
    }
 
    public function check()//检查登录
    {
        if(!is_numeric(Session::get('admin.id'))){
             
            if($this->request->isAjax()){
                exit( json_encode(['check'=>2, 'url'=>url('login/index')]) );
            }else{
                redirect()->remember();
                $this->redirect('login/index');               
            }
        }
    }
 
    public function out() {
        Session::clear();
        $this->redirect('index/index');
    }
 
}

 

转载于:https://www.cnblogs.com/shaoing/p/8920697.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值