当前位置: 移动技术网 > IT编程>开发语言>PHP > YII2框架中验证码的简单使用方法示例

YII2框架中验证码的简单使用方法示例

2020年06月23日  | 移动技术网IT编程  | 我要评论

kiss忍耐大考验,田敏虎,二套房房贷政策

本文实例讲述了yii2框架中验证码的简单使用方法。分享给大家供大家参考,具体如下:

验证码的使用是比较频繁的。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,
      ],
    ];
  }
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网