zoukankan      html  css  js  c++  java
  • yii2.0验证码的两种实现方式

    第一种方式:采用model

    1. 模型:Code.php

    <?php
    namespace appmodels;

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

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

        ];
      }
    }


    控制器:DemoController.php
    <?php
    namespace appcontrollers;
    use Yii;
    use yiiwebController;

    class DemoController extends Controller
    {
      public function actions()
      {
        return [
          'captcha' => [
          'class' => 'yiicaptchaCaptchaAction',
          'maxLength' => 4,
          'minLength' => 4,
          'width' => 80,
          'height' => 40,
          ],
        ];
      }
      public function actionCode()
      {
        $model = new appmodelsCode();
        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 yiihelpersHtml;
    ?>
    <?= Html::beginForm('', 'post', ['class' => 'form']);?>
    <?=yiicaptchaCaptcha::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 appcontrollers;
    use Yii;
    use yiiwebController;

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

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

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

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

    视图:
    <?php
    use yiihelpersHtml;
    ?>
    <?= Html::beginForm('', 'post', ['class' => 'form']);?>
    <?=yiicaptchaCaptcha::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(); ?>

    验证码显示:

  • 相关阅读:
    webpack学习04--打包图片资源
    webpack学习03--打包HTML资源
    webpack学习02--打包样式资源
    webpack学习01--基本使用
    Node.js学习02--创建express服务
    Node.js学习01--npm常用命令
    CSS3的flex布局
    PictureCleaner 官方版 v1.1.5.0607,免费的图片校正及漂白专业工具,专业去除文档图片黑底麻点杂色,规格化A4、B5尺寸输出,还你一个清晰的文本。
    python常识系列22-->jsonpath模块解析json数据
    杂七杂八的问题处理05--jmeter解决部分情况下jtl报告转html报告报错
  • 原文地址:https://www.cnblogs.com/l-zl/p/6508532.html
Copyright © 2011-2022 走看看