The Yii built-in Captcha can basically meet most of the requirements. if you have special requirements on the verification code, you can customize Captcha. the Yii built-in Captcha can basically meet most of the requirements, if you have special requirements on the verification code, you can customize Captcha.
It is implemented by extending CCaptchaAction. In this example, a verification code function is defined to randomly generate addition and subtraction less than 10.
The user must calculate the correct results to pass the verification.
This example is based on the Captcha sample of the UI component in the previous Yii Framework Development Tutorial (20 ).
First, create a MathCaptchaAction in the protected/components directory and reload generateVerifyCode,
RenderImage and other methods:
class MathCaptchaActionextends 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;}}}
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 content of the Captcha sample UI component in the PHP development Framework Yii Framework tutorial (21). For more information, see PHP Chinese website (www.php1.cn )!