Thinkphp5所有页面验证用户是否登陆

新建Base.php控制器,所有的页面继承自它

<?php
namespace app\index\controller;
use think\Controller;

class Base extends Controller
{
    public function __construct(){
        /**
         * 不验证用户登陆的页面
         */

        //不验证用户登陆的页面
        $exception_arth_list=[
            'member/users/login', //登陆页面
            'member/users/reg'    //注册页面
        ];
        $redirect_url='member/users/login';
        $this->checkUserLogin($redirect_url,$exception_arth_list);
    }

   
    /**
    *  检查用户是否登陆,登陆时跳转到登陆页面
    *  $redirect_url 要跳的url (不区别大小写) [str] 例: 'member/Users/login'
    *  $exception_arth_list [array] 不验证用户登陆的页面地址(不区别大小写) 例:     ['member/user/login','member/Users/reg']
    *  $msg 跳转前的提示信息
    */
    protected function checkUserLogin($redirect_url,$exception_arth_list=[],$msg='')
    {
        if(!is_string($redirect_url) || !is_array($exception_arth_list) || !is_string($msg) ){
            die('传入的参数错误.');
        }
      
        //获取到当前访问的页面
        $module=request()->module();//获取当前访问的模块
        $controller=request()->controller();//获取当前访问的控制器
        $action= request()->action();//获取当前访问的方法
        $current_auth_str=$module.'/'.$controller.'/'.$action; //转成字符串
       
        //不验证用户登陆的页面
        //把数组里的全部转小写
        if(!empty($exception_arth_list) && is_array($exception_arth_list)){
            foreach($exception_arth_list as &$v){
                if(!is_string($v)){
                    die('不验证页面数组里的值只能为字符串类型.');
                }
                $v=strtolower($v);
            }
        }
        //当前访问的页面$current_auth_str转为全小写后,如果不在$exception_arth_list客户中就验证用户是否登陆
        if(!empty($exception_arth_list) && is_array($exception_arth_list)){
            if(!in_array(strtolower($current_auth_str),$exception_arth_list)){
                if (!session('user_id')) {
                    if($msg == ''){
                        $this->redirect($redirect_url);
                    }else{
                        $this->error('请先登陆.', $redirect_url);

                    }
                }
            }
        }
    }
}
posted @ 2019-02-19 11:37  HaimaBlog  阅读(2343)  评论(0编辑  收藏  举报