


Detailed steps for adding verification code in yii2 yii2.0 verification code yii verification code not displayed yii2 custom verification
Author: Bailang Source: http://www.manks.top/article/yii2_captcha The copyright of this article belongs to the author. Reprinting is welcome, but this statement must be retained without the author’s consent, and a link to the original text must be provided in an obvious position on the article page. Otherwise, we reserve the right to pursue legal liability.
I originally thought that the verification code of the yii2 framework was very comprehensive. I tried Baidu and Google. 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
<span>public</span><span>function</span><span> actions() { </span><span>return</span><span> [ </span>'captcha' =><span> [ </span>'class' => 'yii\captcha\CaptchaAction', 'maxLength' => 4, 'minLength' => 4<span> ]</span>,<span> ]; }</span>
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 the verification code background. Colors, font files and other settings can be found here.
2. Continue configuring siteController.
<span>public</span><span>function</span><span> behaviors() { </span><span>return</span><span> [ </span>'access' =><span> [ </span>'class' => AccessControl::className(), 'rules' =><span> [ [ </span>'actions' => ['login', 'error', 'captcha'], 'allow' => <span>true</span>,<span> ]</span>,<span> ]</span>,<span> ]; }</span>
Add captcha method to make the actions of access rules accessible.
3. Let’s take a look at the view layer and add the verification code input.
<span>use</span><span> yii\captcha\Captcha; </span><?= <span>$form</span>->field(<span>$model</span>, 'verifyCode')->widget(Captcha::className(),<span> [ </span>'template' => '<div><div>{image}</div><div>{input}</div></div>',<span> ]) </span>?>
4. 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
<span>class</span> LoginForm <span>extends</span><span> Model { </span><span>//</span><span>...... public $verifyCode; </span><span>public</span><span>function</span><span> rules() { </span><span>return</span><span> [ </span><span>//</span><span>...... </span> ['verifyCode', 'captcha'],<span> ]; } </span><span>public</span><span>function</span><span> attributeLabels() { </span><span>return</span><span> [ </span>'verifyCode' => '', <span>//</span><span>验证码的名称,根据个人喜好设定 </span><span> ]; } } </span><span>//</span><span>定义了verifyCode属性 //rules规则添加了验证 //label中定义了其显示名称</span>
5. Go to the fourth step to basically configure the verification The code 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.
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.
$('Verification Code Object').click();
That is, click the verification code again when the page is refreshed to force a refresh.
The above introduces the detailed steps for adding verification codes in yii2, including the content of yii and verification codes. I hope it will be helpful to friends who are interested in PHP tutorials.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



What should I do if Google Chrome does not display the verification code image? Sometimes you need a verification code to log in to a web page using Google Chrome. Some users find that Google Chrome cannot display the content of the image properly when using image verification codes. What should be done? The editor below will introduce how to deal with the Google Chrome verification code not being displayed. I hope it will be helpful to everyone! Method introduction: 1. Enter the software, click the "More" button in the upper right corner, and select "Settings" in the option list below to enter. 2. After entering the new interface, click the "Privacy Settings and Security" option on the left. 3. Then click "Website Settings" on the right

The virtual number can receive the verification code. As long as the mobile phone number filled in during registration complies with the regulations and the mobile phone number can be connected normally, you can receive the SMS verification code. However, you need to be careful when using virtual mobile phone numbers. Some websites do not support virtual mobile phone number registration, so you need to choose a regular virtual mobile phone number service provider.

Failure to receive the verification code on your mobile phone is caused by network problems, mobile phone settings problems, mobile phone operator problems and personal settings problems. Detailed introduction: 1. Network problems. The network environment where the mobile phone is located is unstable or the signal is weak, which may cause the verification code to be unable to be delivered in time; 2. Mobile phone setting problems. The text message or voice function of the mobile phone is accidentally turned off, or the The verification code sending number is added to the blacklist, resulting in the verification code not being received normally; 3. Mobile phone operator issues, the mobile phone operator may have malfunctions or maintenance, resulting in the verification code not being delivered in time, etc.

PHP image processing case: How to implement the verification code function of images. With the rapid development of the Internet, verification codes have become one of the important means to protect website security. Verification code is a verification method that uses image recognition technology to determine whether the user is a real user. This article will introduce how to use PHP to implement the verification code function of images, and come with code examples. Introduction A verification code is a picture containing random characters. The user needs to enter the characters in the picture to pass the verification. The main process of implementing verification code includes generating random characters and drawing characters into pictures.

With the development of the Internet and the popularity of smartphones, the verification code login function is adopted by more and more websites and applications. Verification code login is a login method that verifies the user's identity by entering the correct verification code to improve security and prevent malicious attacks. In PHP development, implementing a simple verification code login function is not complicated and can be completed through the following steps. Create a database table First, we need to create a table in the database to store verification code information. The table structure can contain the following fields: id: auto-incrementing primary key phon

How to create a verification code image using PHP? CAPTCHA is a commonly used method to verify whether the user is a human and not a machine. On websites, we often see verification code images, which require users to enter random characters or numbers displayed on the image to complete operations such as login, registration, and commenting. This article will introduce how to use PHP to create a verification code image and provide specific code examples. 1. PHPGD library To create a verification code image, we need to use PHP's GD library. The GD library is an extension for processing images.

Receiving verification codes from various platforms on your mobile phone may be because your personal information has been stolen, your mobile phone number has been misused, or your mobile phone number has been filled in incorrectly or misused. Detailed introduction: 1. Personal information has been stolen. Hackers or criminals may obtain your personal information through various channels, and then use this information to register accounts on various platforms; 2. Mobile phone numbers have been abused, and some criminals will use A large number of mobile phone numbers are obtained through various means, and then these mobile phone numbers are used to carry out various fraudulent activities; 3. Mobile phone numbers are filled in incorrectly or misused, etc.

How to use JavaScript to implement the verification code function? With the development of the Internet, verification codes have become one of the indispensable security mechanisms in websites and applications. Verification code (VerificationCode) is a technology used to determine whether the user is a human rather than a machine. With CAPTCHAs, websites and applications can prevent spam submissions, malicious attacks, bot crawlers, and more. This article will introduce how to use JavaScript to implement the verification code function and provide specific code
