Home  >  Article  >  Backend Development  >  PHP development framework Yii Framework tutorial (21) UI component Captcha example

PHP development framework Yii Framework tutorial (21) UI component Captcha example

黄舟
黄舟Original
2017-01-22 09:11:481536browse

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,
)

PHP development framework Yii Framework tutorial (21) UI component Captcha example 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn