search
HomeBackend DevelopmentPHP TutorialDetailed explanation of how to use the verification code that comes with Yii 2.0

This article mainly introduces some experiences about using the verification code that comes with Yii 2.0, so that novices can get started quickly, and it has certain reference and learning value for everyone. Friends who need it can come here Let’s take a look.

Preface

In the front-end verification code that comes with the official website, there is a contact.php file under the view. If you have nothing to do, you can take a look first. Without further ado, interested friends can take a look at the detailed introduction:

The usage method is as follows:

Step one: Because I created the modules myself, I created a new models directory under my modules (the default gii-generated modules does not have this directory), and I named it LoginForm.php

The code is as follows:

namespace app\modules\XXX\models;//这个你们写自己的命名空间,我以我的modules项目路径为例

use Yii;

use yii\base\Model;

use yii\captcha\Captcha;

class LoginForm extends Model
{ 
 public $name; 

 public $email; 

 public $subject; 

 public $body; 

 public $verifyCode;//验证码这个变量是必须建的,因为要储存验证码的值` /** * @return array the validation rules. */

 public function rules() 
 { 
   return [ 
     // name, email, subject and body are required 
     [['name', 'email', 'subject', 'body'], 'required'], 
     // email has to be a valid email 
     ['email', 'email'], 
     // verifyCode needs to be entered correctly 
     ['verifyCode', 'captcha'],//注意这里,在百度中查到很多教程,这里写的都不一样,最 简单的写法就像我这种写法,当然还有其它各种写法 
     //['verifyCode', 'captcha','captchaAction'=>'admin/index/captcha','message'=>'验 证码不正确!'], 这种写法在官网自带的LoginForm.php中有写到,大家可以没事看看 ]; 
 }
 /*
 * * @return array customized attribute labels 
 */ 
 public function attributeLabels() 
 { 
   return [ 
     // 'verifyCode' => 'Verification Code', 
     'verifyCode' => '',//在官网的教程里是加上了英文字母,我这里先给去掉了,这里去 掉会不会产生影响因为我还没做接收验证,只做了验证码显示的功能,你们可以自己测试下 
   ]; 
 } 
/***/

Then in the second step we go to Controller and add the code

namespace app\modules\XXX\controllers;//你们自己的控制器空间

use yii\web\Controller;

use yii\web\Session;

use Yii;

use app\modules\XXX\models\LoginForm;//XXX你们自己定义的名字

use yii\filters\AccessControl;

use yii\filters\VerbFilter;

/*
 *这个是对应前台模版的action
 */
public function actionLogin()
{
  $loginForm = new LoginForm();//这里要把刚才写的类new下,注意你们要引入文件路径额
  $this->render('login',array('loginForm'=>$loginForm));//变量传到前台模版
}
/**
 * @用户授权规则
 */
public function behaviors()
{
 return [
   'access' => [
    'class' => AccessControl::className(),
    'only' => ['logout', 'signup','login'],//这里一定要加
    'rules' => [
     [
      'actions' => ['login','captcha'],
      'allow' => true,
      'roles' => ['?'],
     ],
     [
      'actions'=>['logout','edit','add','del','index','users','thumb','upload','cutpic','follow','nofollow'],
      'allow' => true,
      'roles' => ['@'],
     ],
    ],
   ],
   'verbs' => [
    'class' => VerbFilter::className(),
    'actions' => [
     'logout' => ['post'],
    ],
   ],
  ];
 }
 /**
  * @验证码独立操作 下面这个actions注意一点,验证码调试出来的样式也许你并不满意,这里就可
以需修改,这些个参数对应的类是@app\vendor\yiisoft\yii2\captcha\CaptchaAction.php,可以参照这个
类里的参数去修改,也可以直接修改这个类的默认参数,这样这里就不需要改了
  */
 public function actions()
 { 
  return [ 
//     'captcha' => 
//     [
//      'class' => 'yii\captcha\CaptchaAction',
//      'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
//     ], //默认的写法
      'captcha' => [
         'class' => 'yii\captcha\CaptchaAction',
         'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
         'backColor'=>0x000000,//背景颜色
         'maxLength' => 6, //最大显示个数
         'minLength' => 5,//最少显示个数
         'padding' => 5,//间距
         'height'=>40,//高度
         'width' => 130, //宽度 
         'foreColor'=>0xffffff,  //字体颜色
         'offset'=>4,  //设置字符偏移量 有效果
         //'controller'=>'login',  //拥有这个动作的controller
       ],
 ];
 }

The second step of the controller code here is It’s done, you should pay attention to the categories to be added and don’t fall behind!

The third step:

In the view template, here is login.php and add the following code

 <?php 
   $form = ActiveForm::begin([
        &#39;id&#39; => &#39;login-form&#39;,         
          ]); 
 ?>
<?php 
 echo Captcha::widget([&#39;name&#39;=>&#39;captchaimg&#39;,&#39;captchaAction&#39;=>&#39;login/captcha&#39;,&#39;imageOptions&#39;=>[&#39;id&#39;=>&#39;captchaimg&#39;, &#39;title&#39;=>&#39;换一个&#39;, &#39;alt&#39;=>&#39;换一个&#39;, &#39;style&#39;=>&#39;cursor:pointer;margin-left:25px;&#39;],&#39;template&#39;=>&#39;{image}&#39;]);//我这里写的跟官方的不一样,因为我这里加了一个参数(login/captcha),这个参数指向你当前控制器名,如果不加这句,就会找到默认的site控制器上去,验证码会一直出不来,在style里是可以写css代码的,可以调试样式 ?>
<?php 
ActiveForm::end(); 
?>

The above is the detailed content of Detailed explanation of how to use the verification code that comes with Yii 2.0. For more information, please follow other related articles on the PHP Chinese website!

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
Python中的SVM实例Python中的SVM实例Jun 11, 2023 pm 08:42 PM

Python中的支持向量机(SupportVectorMachine,SVM)是一个强大的有监督学习算法,可以用来解决分类和回归问题。SVM在处理高维度数据和非线性问题的时候表现出色,被广泛地应用于数据挖掘、图像分类、文本分类、生物信息学等领域。在本文中,我们将介绍在Python中使用SVM进行分类的实例。我们将使用scikit-learn库中的SVM模

Gin框架中的验证码使用实例Gin框架中的验证码使用实例Jun 23, 2023 am 08:10 AM

随着互联网的普及,验证码已经成为了登录、注册、找回密码等操作的必要流程。在Gin框架中,实现验证码功能也变得异常简单。本文将介绍如何在Gin框架中使用第三方库实现验证码功能,并提供示例代码供读者参考。一、安装依赖库在使用验证码之前,我们需要安装一个第三方库goCaptcha。安装goCaptcha可以使用goget命令:$goget-ugithub

学习Golang指针转换的最佳实践示例学习Golang指针转换的最佳实践示例Feb 24, 2024 pm 03:51 PM

Golang是一门功能强大且高效的编程语言,可以用于开发各种应用程序和服务。在Golang中,指针是一种非常重要的概念,它可以帮助我们更灵活和高效地操作数据。指针转换是指在不同类型之间进行指针操作的过程,本文将通过具体的实例来学习Golang中指针转换的最佳实践。1.基本概念在Golang中,每个变量都有一个地址,地址就是变量在内存中的位置。

VUE3入门实例:制作一个简单的视频播放器VUE3入门实例:制作一个简单的视频播放器Jun 15, 2023 pm 09:42 PM

随着新一代前端框架的不断涌现,VUE3作为一个快速、灵活、易上手的前端框架备受热爱。接下来,我们就来一起学习VUE3的基础知识,制作一个简单的视频播放器。一、安装VUE3首先,我们需要在本地安装VUE3。打开命令行工具,执行以下命令:npminstallvue@next接着,新建一个HTML文件,引入VUE3:&lt;!doctypehtml&gt;

Python中的VAE算法实例Python中的VAE算法实例Jun 11, 2023 pm 07:58 PM

VAE是一种生成模型,全称是VariationalAutoencoder,中文译作变分自编码器。它是一种无监督的学习算法,可以用来生成新的数据,比如图像、音频、文本等。与普通的自编码器相比,VAE更加灵活和强大,能够生成更加复杂和真实的数据。Python是目前使用最广泛的编程语言之一,也是深度学习的主要工具之一。在Python中,有许多优秀的机器学习和深度

Python中的GAN算法实例Python中的GAN算法实例Jun 10, 2023 am 09:53 AM

生成对抗网络(GAN,GenerativeAdversarialNetworks)是一种深度学习算法,它通过两个神经网络互相竞争的方式来生成新的数据。GAN被广泛用于图像、音频、文字等领域的生成任务。在本文中,我们将使用Python编写一个GAN算法实例,用于生成手写数字图像。数据集准备我们将使用MNIST数据集作为我们的训练数据集。MNIST数据集包含

PHP 简单网络爬虫开发实例PHP 简单网络爬虫开发实例Jun 13, 2023 pm 06:54 PM

随着互联网的迅速发展,数据已成为了当今信息时代最为重要的资源之一。而网络爬虫作为一种自动化获取和处理网络数据的技术,正越来越受到人们的关注和应用。本文将介绍如何使用PHP开发一个简单的网络爬虫,并实现自动化获取网络数据的功能。一、网络爬虫概述网络爬虫是一种自动化获取和处理网络资源的技术,其主要工作过程是模拟浏览器行为,自动访问指定的URL地址并提取所

快速上手Django框架:详细教程和实例快速上手Django框架:详细教程和实例Sep 28, 2023 pm 03:05 PM

快速上手Django框架:详细教程和实例引言:Django是一款高效灵活的PythonWeb开发框架,由MTV(Model-Template-View)架构驱动。它拥有简单明了的语法和强大的功能,能够帮助开发者快速构建可靠且易于维护的Web应用程序。本文将详细介绍Django的使用方法,并提供具体实例和代码示例,帮助读者快速上手Django框架。一、安装D

See all articles

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 agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment