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');
    }
 
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM