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

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

黄舟
Release: 2023-03-05 08:56:02
Original
1634 people have browsed it

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;}}}
Copy after login

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,
)
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template