第一种方式:采用model

1. 模型:Code.php

<?php
namespace app\models;

use yii\base\Model;
class Code extends Model
{
  public $code;//添加的验证码字段

  public function rules()
  {
    return [
      //captchaAction 是生成验证码的控制器
      ['code', 'captcha', 'captchaAction' => 'demo/captcha', 'message' => '验证码不正确'],

    ];
  }
}

控制器:DemoController.php
<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;

class DemoController extends Controller
{
  public function actions()
  {
    return [
      'captcha' => [
      'class' => 'yii\captcha\CaptchaAction',
      'maxLength' => 4,
      'minLength' => 4,
      'width' => 80,
      'height' => 40,
      ],
    ];
  }
  public function actionCode()
  {
    $model = new \app\models\Code();
    if(Yii::$app->request->isPost && $model->load(Yii::$app->request->post())) {
    if($model->valodate) {//验证
      echo '验证成功';
    } else{
      //失败
      var_dump($model->getErrors());
    }
  }
  return $this->render('code', ['model' => $model]);
}
}

HTML :code.php
<?php
use \yii\helpers\Html;
?>
<?= Html::beginForm('', 'post', ['class' => 'form']);?>
<?=\yii\captcha\Captcha::widget([
  'model' =>$model,   //Model
  'attribute' => 'code',   //验证码的字段
  'captchaAction' => 'demo/captcha',   //验证码的action 与 Model是对应的
  'template' => '{input}{image}',   //模板,可以自定义
  'options' => ['id' => 'input'],   //input的HTML属性配置
  'imageOptions' => ['alt' => '点击刷新验证码'],   //image的HTML属性配置
])?>
<?=Html:submitButton('提交', ['class' => 'submit'])?>
<?php ActiveForm::end(); ?>

验证码显示:

第二种方式: 不采用model

2. 控制器:

<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;

class DemoController extends Controller
{
  public function actions()
  {
    return [
      'captcha' => [
      'class' => 'yii\captcha\CaptchaAction',
      'maxLength' => 4,
      'minLength' => 4,
      'width' => 80,
      'height' => 40,
      ],

    ];
  }
  public function actionCodeTwo(){
    if(Yii::$app->request->isPost) {//验证验证码
    $code = $_POST['code'];
    //实例化一个验证码验证对象
    $cpValidate = new yii\captcha\CaptchaValidator();
    //配置action为当前的
    $cpValidate->captchaAction = 'demo/captcha';
    //创建一个验证码对象
    $cpAction = $cpValidate->createCaptchaAction();
    //读取验证码
    $scode = $cpAction->getVerifyCode();
    if($code == $scode) {  

      echo ‘正确’;
    }else{
       echo '错误';
    }

    return $this->render('code');
}

视图:
<?php
use \yii\helpers\Html;
?>
<?= Html::beginForm('', 'post', ['class' => 'form']);?>
<?=\yii\captcha\Captcha::widget([
  'name' => 'code',
  'captchaAction' => 'demo/captcha', //验证码的action 与 Model是对应的
  'template' => '{input}{image}', //模板,可以自定义
  'options' => ['id' => 'input'], //input的HTML属性配置
  'imageOptions' => ['alt' => '点击刷新验证码'], //image的HTML属性配置
])?>
<?=Html:submitButton('提交', ['class' => 'submit'])?>
<?php ActiveForm::end(); ?>

验证码显示:

yii2.0验证码的两种实现方式的更多相关文章

  1. css中两种居中方式text-align:center和margin:0 auto 的使用场景

    关于使用text-align:center和margin:0 auto 两种居中方式的比较 前言:最近由于要学习后端,需要提前学习一部分前端知识,补了补css知识,发现狂神在讲这一部分讲的不是特别清楚 ...

  2. iOS活动倒计时的两种实现方式

    代码地址如下:http://www.demodashi.com/demo/11076.html 在做些活动界面或者限时验证码时, 经常会使用一些倒计时突出展现. 现提供两种方案: 一.使用NSTime ...

  3. Web APi之认证(Authentication)两种实现方式【二】(十三)

    前言 上一节我们详细讲解了认证及其基本信息,这一节我们通过两种不同方式来实现认证,并且分析如何合理的利用这两种方式,文中涉及到的基础知识,请参看上一篇文中,就不再叙述废话. 序言 对于所谓的认证说到底 ...

  4. Android中BroadcastReceiver的两种注册方式(静态和动态)详解

    今天我们一起来探讨下安卓中BroadcastReceiver组件以及详细分析下它的两种注册方式. BroadcastReceiver也就是"广播接收者"的意思,顾名思义,它就是用来 ...

  5. struts2+spring的两种整合方式

    也许有些人会因为学习了struts1,会以为struts2.struts1与spring的整合也是一样的,其实这两者相差甚远.下面就来讲解一下struts2与spring的整合两种方案.(部分转载,里 ...

  6. easyui datagride 两种查询方式

    easyui datagride 两种查询方式function doReseach() { //$('#tt').datagrid('load', { // FixedCompany: $('.c_s ...

  7. PlaceHolder的两种实现方式

    placeholder属性是HTML5 中为input添加的.在input上提供一个占位符,文字形式展示输入字段预期值的提示信息(hint),该字段会在输入为空时显示. 如 <input typ ...

  8. Ajax中的get和post两种请求方式的异同

    Ajax中我们经常用到get和post请求.那么什么时候用get请求,什么时候用post方式请求呢? 在做回答前我们首先要了解get和post的区别.   1. get是把参数数据队列加到提交表单的A ...

  9. 第一章 Mybtais的两种启动方式

    Mybatis的两种启动方式如下: 1.xml实现: xml的实现方式中,主要是通过手动创建SqlSession,然后调用session.selectOne()方法实现来实现. 首先是创建Config ...

随机推荐

  1. GPRS DTU工作的原理与应用场景

    GPRS DTU是属于物联网无线数据终端设备的中一种,它主要是利用公用运营商的GPRS网络(又称G网)来为用户提供无线长距离数据传输的功能.一般都是采用的高性能工业级8/16/32位通信处理器和工业级 ...

  2. 常用数据结构-namedtuple(命名元祖)

    namedtuple从根本上来说是一个带有数据名称的元祖.它包含元祖的所有特性,但也有一些元祖没有的额外特性.使用namedtuple可以很容易创建轻量级对象类型. namedtuple将是你的代码更 ...

  3. Django实现文件上传功能

    文件上传 关注公众号"轻松学编程"了解更多. 1.创建上传文件夹 在static文件夹下创建uploads用于存储接收上传的文件 在settings中配置,MEDIA_ROOT=o ...

  4. Win10 Terminal + WSL 2 安装配置指南,精致开发体验

    自从 Windows Terminal 正式发布后就再没有用过 Windows 系统自带的终端了.主要是 Terminal 简洁且灵活,更重要的是支持特殊字体,通过一些简单的配置可以使得终端看起来更舒 ...

  5. Git的介绍及使用

    一.配置用户信息: 配置用户名和邮箱: $ git config --global user.name "chunyu" $ git config --global user.em ...

  6. java 动态增加应用服务器,出现的消息队列的消费者提报错问题

    java 动态增加应用服务器,出现的消息队列的消费者提报错问题 在项目中,有这样的业务场景,在某一个时间段,客户流量瞬间增大,服务器瞬间很大,出现高并发问题.有一种解决方案就是脚本动态增加业务服务器, ...

  7. C++语言学习之STL 的组成

    STL有三大核心部分:容器(Container).算法(Algorithms).迭代器(Iterator),容器适配器(container adaptor),函数对象(functor),除此之外还有S ...

  8. 关于synchronized无法锁住Integer原因

    原因 在多线程的时候,为了保证数据安全,必须在修改数据时使用线程同步,java中的synchronized用来实现线程同步.线程列队. 学完多线程基础的我,写一个多线程交替输出1,2,3,4,5... ...

  9. kafka的基本安装与使用

    kafka的基本安装与使用 1.上官网下载tar包 2.解压 3.运行zookeeper 4.运行kafka服务器 5.创建topic 6.发送消息 7.监听消息 1.上官网下载tar包 https: ...

  10. delphi key解密转c# 解决string 不可变长度问题

    遇见问题: delphi的解密需要在c#里面实现 方法一:delphi编写delphi dll组件,c#里面调用 方法二:c#重写delphi的代码进行解析 方法一: delphi部分代码: libr ...