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


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

    <?php
    namespace appindexcontroller;
    use thinkController;
    
    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);
    
                        }
                    }
                }
            }
        }
    }
    
  • 相关阅读:
    mvc生成table
    JQ仿ebay右侧flash商品展示
    调查一下,EF的Bug?
    SpringCloud组件Zuul入门解析
    SpringCloud组件Ribbon入门解析
    FTO介绍
    算法网址收藏
    哈拂大学凌晨四点的景象
    【系统学习ES6】第二节:解构赋值
    MySQL高级
  • 原文地址:https://www.cnblogs.com/haima/p/10400034.html
Copyright © 2020-2023  润新知