非法登录验证---公共控制器的使用

公共模板

<?php
namespace app\admin\common;
use think\Controller;
use think\Session;
/*公共控制器(home控制器)*/
class Base_home extends Controller
{
    function _initialize(){
        parent::_initialize();
        //如果没有session
        if(!Session::has('admin')){
            $this->error('未登录,无权访问···','login/index');
        }
    }
}

要求登录才能访问的控制器

<?php
namespace app\admin\controller;
use app\admin\common\Base_home;
class Home extends Base_home
{
    public function index()
    {
        return $this->fetch();
    }
    public function welcome()
    {
        return $this->fetch();
    }
}

这样一来:

如果非法访问

 

posted @ 2018-06-01 17:30  cl94  阅读(204)  评论(0编辑  收藏  举报