How does the php background prevent users from going directly into method instances

  • 2020-10-23 20:02:49
  • OfStack

1) Create BaseController controller to inherit Controller (the background 1 cut operation should inherit BaseController):

Add to 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' => ' The user is not logged in. ', 'callback' => 'window.location="' . $url . '";')); 
            } else if (Yii::app()->request->isAjaxRequest) { 
                echo '<script language="javascript">window.location="' . $url . '";</script>'; 
            } else { 
                $this->redirect($url); 
            } 
            exit; 
        } 
        return true; 
    } 

Create the Authority.php file in the components directory:


<?php 

/** 
 *  Permission check component  
 */
class Authority extends CComponent { 
    private $NOTLOGIN = -1; 
    private $FAILED = -2; 
    private $PASS = 1; 

    public function init() { 

    } 

    /** 
     *  Check whether to log in  
     * @return boolean  
     */
    function isLogin() { 
        return isset(Yii::app()->session['user']) ? $this->PASS : $this->NOTLOGIN; 
    } 

   
    /** 
     *  Get status value  
     * @param string $name 
     * @return int  
     */
    public function getStatus($name){ 
        return $this->$name; 
    } 
} 


Related articles: