yii2.0验证码的两种实现方式
第一种方式:采用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验证码的两种实现方式的更多相关文章
- css中两种居中方式text-align:center和margin:0 auto 的使用场景
关于使用text-align:center和margin:0 auto 两种居中方式的比较 前言:最近由于要学习后端,需要提前学习一部分前端知识,补了补css知识,发现狂神在讲这一部分讲的不是特别清楚 ...
- iOS活动倒计时的两种实现方式
代码地址如下:http://www.demodashi.com/demo/11076.html 在做些活动界面或者限时验证码时, 经常会使用一些倒计时突出展现. 现提供两种方案: 一.使用NSTime ...
- Web APi之认证(Authentication)两种实现方式【二】(十三)
前言 上一节我们详细讲解了认证及其基本信息,这一节我们通过两种不同方式来实现认证,并且分析如何合理的利用这两种方式,文中涉及到的基础知识,请参看上一篇文中,就不再叙述废话. 序言 对于所谓的认证说到底 ...
- Android中BroadcastReceiver的两种注册方式(静态和动态)详解
今天我们一起来探讨下安卓中BroadcastReceiver组件以及详细分析下它的两种注册方式. BroadcastReceiver也就是"广播接收者"的意思,顾名思义,它就是用来 ...
- struts2+spring的两种整合方式
也许有些人会因为学习了struts1,会以为struts2.struts1与spring的整合也是一样的,其实这两者相差甚远.下面就来讲解一下struts2与spring的整合两种方案.(部分转载,里 ...
- easyui datagride 两种查询方式
easyui datagride 两种查询方式function doReseach() { //$('#tt').datagrid('load', { // FixedCompany: $('.c_s ...
- PlaceHolder的两种实现方式
placeholder属性是HTML5 中为input添加的.在input上提供一个占位符,文字形式展示输入字段预期值的提示信息(hint),该字段会在输入为空时显示. 如 <input typ ...
- Ajax中的get和post两种请求方式的异同
Ajax中我们经常用到get和post请求.那么什么时候用get请求,什么时候用post方式请求呢? 在做回答前我们首先要了解get和post的区别. 1. get是把参数数据队列加到提交表单的A ...
- 第一章 Mybtais的两种启动方式
Mybatis的两种启动方式如下: 1.xml实现: xml的实现方式中,主要是通过手动创建SqlSession,然后调用session.selectOne()方法实现来实现. 首先是创建Config ...
随机推荐
- mysql-connector-java各种版本下载地址
mysql-connector-java下载地址: http://mvnrepository.com/artifact/mysql/mysql-connector-java 目录 1.进去后选择自己的 ...
- 前端小程序——js+canvas 给图片添加水印
市场上各种各样的图片处理器有很多,那么作为程序员的我们是不是应该自己做一个呢?那就从加水印开始吧 html: <canvas id="shuiyinTest"> < ...
- D. Bash and a Tough Math Puzzle 解析(線段樹、數論)
Codeforce 914 D. Bash and a Tough Math Puzzle 解析(線段樹.數論) 今天我們來看看CF914D 題目連結 題目 給你一個長度為\(n\)的數列\(a\), ...
- pip intsall 遇到的各种问题
一.pip install 安装指定版本的包 要用pip 安装指定版本的python的包,只需要通过 == 操作符指定. pip install robotframework == 2.8.7 将安装 ...
- Python实现微信支付(三种方式)
Python实现微信支付(三种方式) 关注公众号"轻松学编程"了解更多. 如果需要python SDk源码,可以加我微信[1257309054] 在文末有二维码. 一.准备环境 1 ...
- 微信小程序-游记分享(无后台)
游记分享 博客班级 https://edu.cnblogs.com/campus/zjcsxy/SE2020 作业要求 https://edu.cnblogs.com/campus/zjcsxy/SE ...
- [Luogu P2831] 愤怒的小鸟 (状压DP)
题面: 传送门:https://www.luogu.org/problemnew/show/P2831 Solution 首先,我们可以先康一康题目的数据范围:n<=18,应该是状压或者是搜索. ...
- 快快使用ModelArts,零基础小白也能玩转AI!
摘要: 走过路过不要错过,看Copy攻城狮如何借力华为云ModelArts玩转AI. "自2018年10月发布以来,ModelArts累计服务了众多行业十几万开发者,通过基础平台的完备性和面 ...
- K8S Canal基于Prometheus进行实时指标监控
文章来源于本人的印象笔记,如出现格式问题可访问该链接查看原文 部署canal的prometheus监控到k8s中 1.grafana的docker部署方式:https://grafana.com/gr ...
- Thinkphp3.2 cms之分类管理
四.分类管理 <?php namespace Admin\Controller; use Think\Controller; class CateController extends Contr ...