欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

YII2框架中驗(yàn)證碼的簡(jiǎn)單使用方法示例

 更新時(shí)間:2020年03月12日 11:58:45   作者:懷素真  
這篇文章主要介紹了YII2框架中驗(yàn)證碼的簡(jiǎn)單使用方法,結(jié)合實(shí)例形式分析了Yii2框架驗(yàn)證碼的基本創(chuàng)建、使用方法及操作注意事項(xiàng),需要的朋友可以參考下

本文實(shí)例講述了YII2框架中驗(yàn)證碼的簡(jiǎn)單使用方法。分享給大家供大家參考,具體如下:

驗(yàn)證碼的使用是比較頻繁的。YII2中已經(jīng)幫我們做好了封裝。

首先我們?cè)诳刂破骼飫?chuàng)建一個(gè)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過來的驗(yàn)證碼
      $verify = YII::$app->request->post('verify');

      //我們手動(dòng)進(jìn)行驗(yàn)證,第二個(gè)參數(shù)表示是否區(qū)分大小寫
      if ($this->createAction('captcha')->validate($verify, false)) {
        echo '成功';
      } else {
        echo '失敗';
      }

    } else {
      return $this->renderPartial('index');
    }
  }

  //actions的作用主要是共用功能相同的方法
  //當(dāng)用戶訪問index/captcha時(shí),actions就會(huì)調(diào)用yii\captcha\CaptchaAction方法
  public function actions()
  {
    return [
      'captcha' => [
        'class' => 'yii\captcha\CaptchaAction',
        'fixedVerifyCode' => null,
        //背景顏色
        'backColor' => 0x000000,
        //最大顯示個(gè)數(shù)
        'maxLength' => 4,
        //最少顯示個(gè)數(shù)
        'minLength' => 4,
        //間距
        'padding' => 2,
        //高度
        'height' => 30,
        //寬度
        'width' => 85,
        //字體顏色
        'foreColor' => 0xffffff,
        //設(shè)置字符偏移量
        '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">
    驗(yàn)證碼:<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 () {
      //處理點(diǎn)擊刷新驗(yàn)證碼
      $("#verifyImg").on("click", function () {
        $.get("<?php echo Url::toRoute('index/captcha') ?>?refresh", function (data) {
          $("#verifyImg").attr("src", data["url"]);
        }, "json");
      });
    });
  </script>
</body>
</html>

演示結(jié)果如下:

上面控制器中驗(yàn)證碼的驗(yàn)證方式是我們手動(dòng)的。我們也可以創(chuàng)建一個(gè)模型配置rules()來自動(dòng)完成。

<?php

namespace app\models;

use yii\base\Model;

class VerifyForm extends Model
{

  //變量名為你表單中輸入驗(yàn)證碼控件的name
  public $verify;

  public function rules()
  {
    return [
      ['verify', 'required', 'message' => '請(qǐng)?zhí)顚戲?yàn)證碼'],
      //注意captchaAction的設(shè)置,指向你顯示驗(yàn)證碼的action,這里我們的是index/captcha
      ['verify', 'captcha', 'captchaAction' => 'index/captcha', 'caseSensitive' => false, 'message' => '驗(yàn)證碼錯(cuò)誤'],
    ];
  }
}

控制器代碼修改如下:

<?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(), '');

      //自動(dòng)驗(yàn)證
      if ($verify->validate()) {
        echo '成功';
      } else {
        var_dump($verify->errors);
      }

    } else {
      return $this->renderPartial('index');
    }
  }

  //actions的作用主要是共用功能相同的方法
  //當(dāng)用戶訪問index/captcha時(shí),actions就會(huì)調(diào)用yii\captcha\CaptchaAction方法
  public function actions()
  {
    return [
      'captcha' => [
        'class' => 'yii\captcha\CaptchaAction',
        'fixedVerifyCode' => null,
        //背景顏色
        'backColor' => 0x000000,
        //最大顯示個(gè)數(shù)
        'maxLength' => 4,
        //最少顯示個(gè)數(shù)
        'minLength' => 4,
        //間距
        'padding' => 2,
        //高度
        'height' => 30,
        //寬度
        'width' => 85,
        //字體顏色
        'foreColor' => 0xffffff,
        //設(shè)置字符偏移量
        'offset' => 4,
      ],
    ];
  }
}

更多關(guān)于Yii相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結(jié)》、《php優(yōu)秀開發(fā)框架總結(jié)》、《smarty模板入門基礎(chǔ)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總

希望本文所述對(duì)大家基于Yii框架的PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論