验证码的使用是比较频繁的。YII2中已经帮我们做好了封装。

首先我们在控制器里创建一个actions方法,用于使用yii\captcha\CaptchaAction

<?php

namespace app\controllers;

use YII;
use yii\web\Controller; class IndexController extends Controller
{
public function actionIndex()
{
if (YII::$app->request->isPost) {
//获取post过来的验证码
$verify = YII::$app->request->post('verify'); //我们手动进行验证,第二个参数表示是否区分大小写
if ($this->createAction('captcha')->validate($verify, false)) {
echo '成功';
} else {
echo '失败';
} } else {
return $this->renderPartial('index');
}
} //actions的作用主要是共用功能相同的方法
//当用户访问index/captcha时,actions就会调用yii\captcha\CaptchaAction方法
public function actions()
{
return [
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => null,
//背景颜色
'backColor' => 0x000000,
//最大显示个数
'maxLength' => 4,
//最少显示个数
'minLength' => 4,
//间距
'padding' => 2,
//高度
'height' => 30,
//宽度
'width' => 85,
//字体颜色
'foreColor' => 0xffffff,
//设置字符偏移量
'offset' => 4,
],
];
}
}

显示页面代码如下:

<?php
use yii\helpers\Url;
use yii\helpers\Html;
?>
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>分页显示</title>
</head>
<body>
<form action="<?php echo Url::toRoute('index/index'); ?>" method="post">
验证码:<input type="text" name="verify"><br>
<img id="verifyImg" src="<?php echo Url::toRoute('index/captcha'); ?>"><br>
<input type="submit" value="提交">
<input name="_csrf" type="hidden" value="<?php echo \Yii::$app->request->csrfToken; ?>">
</form> <?php echo Html::jsFile('@web/js/jquery-3.3.1.min.js'); ?>
<script type="text/javascript">
$(function () {
//处理点击刷新验证码
$("#verifyImg").on("click", function () {
$.get("<?php echo Url::toRoute('index/captcha') ?>?refresh", function (data) {
$("#verifyImg").attr("src", data["url"]);
}, "json");
});
});
</script>
</body>
</html>

演示结果如下:

上面控制器中验证码的验证方式是我们手动的。我们也可以创建一个模型配置rules()来自动完成。

<?php

namespace app\models;

use yii\base\Model;

class VerifyForm extends Model
{ //变量名为你表单中输入验证码控件的name
public $verify; public function rules()
{
return [
['verify', 'required', 'message' => '请填写验证码'],
//注意captchaAction的设置,指向你显示验证码的action,这里我们的是index/captcha
['verify', 'captcha', 'captchaAction' => 'index/captcha', 'caseSensitive' => false, 'message' => '验证码错误'],
];
}
}

控制器代码修改如下:

<?php

namespace app\controllers;

use YII;
use app\models\VerifyForm;
use yii\web\Controller; class IndexController extends Controller
{
public function actionIndex()
{
if (YII::$app->request->isPost) {
$verify = new VerifyForm();
$verify->load(YII::$app->request->post(), ''); //自动验证
if ($verify->validate()) {
echo '成功';
} else {
var_dump($verify->errors);
} } else {
return $this->renderPartial('index');
}
} //actions的作用主要是共用功能相同的方法
//当用户访问index/captcha时,actions就会调用yii\captcha\CaptchaAction方法
public function actions()
{
return [
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => null,
//背景颜色
'backColor' => 0x000000,
//最大显示个数
'maxLength' => 4,
//最少显示个数
'minLength' => 4,
//间距
'padding' => 2,
//高度
'height' => 30,
//宽度
'width' => 85,
//字体颜色
'foreColor' => 0xffffff,
//设置字符偏移量
'offset' => 4,
],
];
}
}

  

YII2中验证码的使用的更多相关文章

  1. 解决Yii2中刷新网页时验证码不刷新的问题

    解决Yii2中刷新网页时验证码不刷新的问题 [ 2.0 版本 ] ljfrocky  2015-05-30 19:39:00  1304次浏览 5条评论 10110 在Yii2框架中,如果在表单中使用 ...

  2. yii2增加验证码详细步骤

    作者:白狼 出处:http://www.manks.top/article/yii2_captcha本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留 ...

  3. yii2中如何使用modal弹窗之基本使用

    作者:白狼 出处:http://www.manks.top/yii2_modal_baseuse.html 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接, ...

  4. Yii2中多表关联查询(join、joinwith)

    我们用实例来说明这一部分 表结构 现在有客户表.订单表.图书表.作者表, 客户表Customer   (id  customer_name) 订单表Order      (id  order_name ...

  5. PHP在yii2中封装SuperSlide 幻灯片编写自己的SuperSlideWidget的例子

    因为近期给朋友公司做个门户网站,把荒置了6.7年的PHP又重新拾起,发现PHP这些年兴旺多了,很多新的东西看的不明不白,研究了几个框架ZendFramework.thinkphp.Symfony.yi ...

  6. [moka同学笔记]Yii2中多表关联查询(join、joinwith) (摘录)

    表结构 现在有客户表.订单表.图书表.作者表, 客户表Customer   (id  customer_name) 订单表Order          (id  order_name       cu ...

  7. Yii2.0中文开发向导——Yii2中多表关联查询(join、joinwith)(转)

    我们用实例来说明这一部分 表结构 现在有客户表.订单表.图书表.作者表, 客户表Customer   (id  customer_name) 订单表Order          (id  order_ ...

  8. Yii2.0中文开发向导——Yii2中多表关联查询(join、joinwith)

    我们用实例来说明这一部分 表结构 现在有客户表.订单表.图书表.作者表, 客户表Customer   (id  customer_name) 订单表Order          (id  order_ ...

  9. MVC中验证码的生成

    在项目中验证码的生成通常是需要页面无刷新的,所以验证码图片实际是跟在某个input后面的img,通过控制该img来控制验证码显示的位置,例如: <div> <input id=&qu ...

随机推荐

  1. Unit 1 overview of IT Industry

    Unit 1 overview of IT IndustryConcept LearningIT Industry OutlookThe term technology commonly refers ...

  2. codes often WA

    枚举: 1.完美立方 #include<iostream> #include <cstdio> using namespace std; int main() { int N; ...

  3. elk日志平台搭建小记

    最近抽出点时间,搭建了新版本的elk日志平台 elastaicsearch 和logstash,kibana和filebeat都是5.6版本的 中间使用redis做缓存,版本为3.2 使用的系统为ce ...

  4. DELPHI 对象的本质 VMT

    http://www.cnblogs.com/little-mat/articles/2206627.html TObject是所有对象的基本类,DELPHI中的任何对象都是一个指针,这个指针指明该对 ...

  5. cpu-io.sh

    CPU-IO.SH #!/bin/bash #Edit by laozuo.org cname=$(cat /proc/cpuinfo|grep name|head -1|awk '{ $1=$2=$ ...

  6. Structs复习 OGNL

    Dominmodel只有传 User.age 类似的这种Structs才能帮创建对象 Dominmodel User里必须有空的构造方法 OGNL:OBJECT GRAPHIC NAVAGATION ...

  7. (转)如何禁用Windows 10系统的触摸屏

    https://baijiahao.baidu.com/s?id=1593890738706748667 现在许多优质的Windows 10个人电脑都配备了触摸屏,因为触摸屏的日益普及,Windows ...

  8. centos7.5下yum 安装mariadb数据库

    前言 mariadb 和mysql就像亲兄弟的关系,各种语法.驱动啥的,在mysql上能上的,在mariadb上基本都可以直接使用.更多的细节在此不多说. 1.删除旧版本 centos7下默认安装有m ...

  9. kafka清理

    由于项目原因,最近经常碰到Kafka消息队列拥堵的情况.碰到这种情况为了不影响在线系统的正常使用,需要大家手动的清理Kafka Log.但是清理Kafka Log又不能单纯的去删除中间环节产生的日志, ...

  10. php数组按值的大小排序

    array_multisort(array_column($nima,'zongfen'),SORT_DESC,$nima);