Table of Contents
Yii2增加验证码步骤详解,yii2验证码详解
Home php教程 php手册 Yii2增加验证码步骤详解,yii2验证码详解

Yii2增加验证码步骤详解,yii2验证码详解

Jun 13, 2016 am 08:40 AM
yii yii2 Verification code

Yii2增加验证码步骤详解,yii2验证码详解

本来以为yii2框架验证码这块很全面,尝试百度google了一下,大多数教程写的零零散散不全面,想着自己写一份带有完整步骤的验证码教程。

我们假设site/login 表单登录需要增加验证码。

1、siteController控制器的actions方法增加captcha设置

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

上面我们简单设置了验证码的位数,有小伙伴好奇都有哪些配置项,这个你可以查看文件 vendor\yiisoft\yii2\captcha,包括验证码背景色,字体文件等设置都可以在这里找到。

2、siteController继续配置。

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

为access rules的actions增加captcha方法可访问。

3、我们看看view层,增加验证码input。

use yii\captcha\Captcha; 
<&#63;= $form->field($model, 'verifyCode')->widget(Captcha::className(), [ 
'template' => '<div class="row"><div class="col-lg-3">{image}</div><div class="col-lg-6">{input}</div></div>', 
]) &#63;> 
Copy after login

4、这样还不行,我们还需要增加验证码的验证规则

我们这里用到的是LoginForm,因此修改LoginForm文件

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

5、到第四步基本上配置好验证码就会正常显示了。如果你的后台设定了rbac权限控制,恐怕你仍然需要在config内为as accss增加/site/captcha可访问。

6、看效果就好了。


7、有同学问为啥页面刷新验证码不跟着刷新,我个人觉得刷不刷新不重要,当你输错验证码页面刷新的时候验证码才会刷新。如果你非要刷新页面验证码跟着刷新,尝试一种简单的方法实现。

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

即在页面刷新的时候重新点击一次验证码进行强制刷新。

以上所述是小编给大家介绍的Yii2增加验证码步骤,希望对大家有所帮助!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if Google Chrome does not display the verification code image? Chrome browser does not display the verification code? What should I do if Google Chrome does not display the verification code image? Chrome browser does not display the verification code? Mar 13, 2024 pm 08:55 PM

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

Can virtual numbers receive verification codes? Can virtual numbers receive verification codes? Jan 02, 2024 am 10:22 AM

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.

Why can't I receive the verification code on my phone? Why can't I receive the verification code on my phone? Aug 17, 2023 pm 02:49 PM

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 PHP image processing case: How to implement the verification code function of images Aug 17, 2023 pm 12:09 PM

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.

PHP Development Guide: Implementing Verification Code Login PHP Development Guide: Implementing Verification Code Login Jul 01, 2023 am 09:27 AM

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 use JavaScript to implement verification code function? How to use JavaScript to implement verification code function? Oct 19, 2023 am 10:46 AM

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

Why can't I receive the verification code to register in Youka? Why can't I receive the verification code to register in Youka? Mar 03, 2024 pm 05:31 PM

Youkazhong users need to use the verification code to register, so why can’t they receive the verification code to register? Users may not receive the verification code due to network problems, device problems, or server problems. This introduction to the registration problem of not receiving the verification code can tell you how to install it. The following is a detailed introduction, come and take a look! Why can't you receive the verification code for registration in Youka? Answers: Network problems, equipment problems, and server problems lead to detailed introduction: 1. Solution to network problems: Users need to check their own network and can try to change the network environment. If the network is weak, the verification code will fail to be sent. 2. Equipment problem: Solution: Check if there is any interfering software in your background. You can try restarting the device or reinstalling the software.

How to send multiple emails with image verification codes using PHP How to send multiple emails with image verification codes using PHP Sep 13, 2023 am 11:55 AM

How to use PHP to send multiple emails with image verification codes, specific code examples are required. In modern society, email has become an important communication tool. Sometimes we encounter situations where we need to send emails with verification codes, such as registration confirmation emails, password reset emails, etc. In order to increase security and user experience, we often use image verification codes. This article will introduce how to use PHP to send multiple emails with image verification codes, and provide specific code examples. First, we need to introduce the PHPMailer library, which is a non-

See all articles