How to Prevent Users From directly accessing the Method Instance in the php background

Source: Internet
Author: User

1) create a BaseController to inherit the Controller (all operations on the background must inherit the BaseController ):

Add the following in BaseController:

Copy codeThe Code is as follows:
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 has not logged on. ', '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:

Copy codeThe Code is as follows:
<? Php

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

Public function init (){

}

/**
* Check for Logon
* @ 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;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.