Heim  >  Artikel  >  php教程  >  php后台如何避免用户直接进入方法实例

php后台如何避免用户直接进入方法实例

WBOY
WBOYOriginal
2016-06-13 09:31:41793Durchsuche

1)创建BaseController控制器继承Controller(后台的一切操作要继承BaseController):

在BaseController里面添加:

复制代码 代码如下:


public function checkLogin() {

        if (Yii::app()->authority->isLogin() == Yii::app()->authority->getStatus('NOTLOGIN')) {
            $url = $this->createUrl('user/login');
            if (Yii::app()->request->isPostRequest && Yii::app()->request->isAjaxRequest) {
                echo json_encode(array('code' => -101, 'message' => '用户未登录。', 'callback' => 'window.location="' . $url . '";'));
            } else if (Yii::app()->request->isAjaxRequest) {
                echo '';
            } else {
                $this->redirect($url);
            }
            exit;
        }
        return true;
    }

在components目录下创建Authority.php文件:

复制代码 代码如下:



/**
 * 权限检查组件
 */
class Authority extends CComponent {
    private $NOTLOGIN = -1;
    private $FAILED = -2;
    private $PASS = 1;

    public function init() {

    }

    /**
     * 检查是否登陆
     * @return boolean 
     */
    function isLogin() {
        return isset(Yii::app()->session['user']) ? $this->PASS : $this->NOTLOGIN;
    }

  
    /**
     * 获取状态值
     * @param string $name
     * @return int 
     */
    public function getStatus($name){
        return $this->$name;
    }
}

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn