Home > Article > Backend Development > PHP development framework Yii Framework tutorial (21) UI component Captcha example
Yii's built-in Captcha can basically meet most needs. If you have special requirements for verification codes, you can customize Captcha. This is
mainly achieved by extending CCaptchaAction. In this example, customization A verification code function that randomly generates addition and subtraction within 10. Users need to calculate the correct result to pass the verification.
This example is based on the Yii Framework development tutorial (20) UI component Captcha example above, with the following modifications
First create a MathCaptchaAction in the protected/components directory and overload generateVerifyCode,
renderImage and other methods:
class MathCaptchaAction extends CCaptchaAction{ protected function generateVerifyCode(){return mt_rand((int)$this->minLength,(int)$this->maxLength);} public function renderImage($code){parent::renderImage($this->getText($code));} protected function getText($code){$code=(int)$code;$rand=mt_rand(1,$code-1);$op=mt_rand(0,1);if($op){ return $code-$rand. '+' . $rand; }else{return $code+$rand. '-' . $rand;}}}
Then modify the rules of SiteController and use the newly created MathCaptchaAction
public function actions() { return array( 'captcha'=>array( 'class' => 'MathCaptchaAction', 'minLength' => 1, 'maxLength' => 10, )
The above is the PHP development framework Yii Framework tutorial (21) UI components Captcha example content, please pay attention to the PHP Chinese website (www.php.cn) for more related content!