Home >Backend Development >PHP Tutorial >Detailed explanation of steps to add verification code in Yii2 yii2.0 verification code yii verification code not displayed yii2 custom verification

Detailed explanation of steps to add verification code in Yii2 yii2.0 verification code yii verification code not displayed yii2 custom verification

WBOY
WBOYOriginal
2016-07-29 08:51:531069browse

I originally thought that the verification code of the yii2 framework was very comprehensive. I tried Baidu and Google and found that most of the tutorials were scattered and incomplete. I thought about writing a verification code tutorial with complete steps.

We assume that site/login form login requires adding a verification code.

1. The actions method of the siteController controller adds captcha settings

public function actions() { 
return [ 
'captcha' => [ 
'class' => 'yii\captcha\CaptchaAction', 
'maxLength' => 4, 
'minLength' => 4 
], 
]; 
}

Above we simply set the number of digits of the verification code. Some friends are curious about what configuration items there are. For this, you can view the file vendoriisoftyii2captcha, including verification. Code background color, font file and other settings can be found here.

2. Continue to configure siteController.

public function behaviors() { 
return [ 
'access' => [ 
'class' => AccessControl::className(), 
'rules' => [ 
[ 
'actions' => ['login', 'error', 'captcha'], 
'allow' => true, 
], 
], 
]; 
}

Add captcha method accessibility to the actions of access rules.

3. Let’s take a look at the view layer and add the verification code input.

use yii\captcha\Captcha; 
<&#63;= $form->field($model, 'verifyCode')->widget(Captcha::className(), [ 
'template' => '<div><div>{image}</div><div>{input}</div></div>', 
]) ?> 

4. If this is not enough, we also need to add verification rules for the verification code

We are using LoginForm here, so modify the LoginForm file

class LoginForm extends Model { 
//...... public $verifyCode; 
public function rules() { 
return [ 
//...... 
['verifyCode', 'captcha'], 
]; 
} 
public function attributeLabels() { 
return [
'verifyCode' => '', //验证码的名称,根据个人喜好设定 
]; 
} 
} 
//定义了verifyCode属性 
//rules规则添加了验证 
//label中定义了其显示名称 

5. Basically, go to the fourth step Once the verification code is configured, it will be displayed normally. If your background has rbac permission control set up, I'm afraid you still need to add /site/captcha accessibility for as accss in the config.

6. Just see the effect.

yii2 验证码,yii,yii 获取验证码,yii 验证码不正确,验证码,yii 验证码不刷新,yii2.0 验证码,yii 验证码 不显示,yii2 自定义验证

7. Some students asked why the verification code does not refresh when the page is refreshed. I personally think it does not matter whether it refreshes or not. The verification code will only be refreshed when you enter the wrong verification code and the page refreshes. If you have to refresh the page to follow the verification code, try a simple method to achieve it.

$('验证码对象').click();

That is, click the verification code again when the page is refreshed to force a refresh.

The above are the steps for adding verification codes in Yii2 introduced by the editor. I hope it will be helpful to everyone!

The above introduces the detailed steps of adding verification code in Yii2, including the content of yii and verification code. I hope it will be helpful to friends who are interested in PHP tutorials.

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