This article describes how the PHP background to avoid users directly into the method instance, the need for friends can refer to
1) Create Basecontroller controller to inherit controllers (all operations in the background to inherit Basecontroller):
Add in 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 ' = ' = ' user 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/** * Permissions Check Unit */class Authority extends Ccomponent { private $NOTLOGIN =-1; Private $FAILED =-2; Private $PASS = 1; Public Function init () { } /** * Check for login * @return Boolean * /function IsLogin () { Return Isset (Yii::app ()->session[' user ')? $this->pass: $this->notlogin; } /** * Gets the status value * @param string $name * @return int * /public function GetStatus ($name) { return $this $name; } }
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!