当前位置:首页 > CMS教程 > 其它CMS > 列表

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

发布:smiling 来源: PHP粉丝网  添加日期:2022-02-20 09:24:48 浏览: 评论:0 

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

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

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

  1. <?php 
  2.  
  3. namespace app\controllers; 
  4.  
  5. use YII; 
  6. use yii\web\Controller; 
  7.  
  8. class IndexController extends Controller 
  9. { 
  10.   public function actionIndex() 
  11.   { 
  12.     if (YII::$app->request->isPost) { 
  13.       //获取post过来的验证码 
  14.       $verify = YII::$app->request->post('verify'); 
  15.  
  16.       //我们手动进行验证,第二个参数表示是否区分大小写 
  17.       if ($this->createAction('captcha')->validate($verify, false)) { 
  18.         echo '成功'; 
  19.       } else { 
  20.         echo '失败'; 
  21.       } 
  22.  
  23.     } else { 
  24.       return $this->renderPartial('index'); 
  25.     } 
  26.   } 
  27.  
  28.   //actions的作用主要是共用功能相同的方法 
  29.   //当用户访问index/captcha时,actions就会调用yii\captcha\CaptchaAction方法 
  30.   public function actions() 
  31.   { 
  32.     return [ 
  33.       'captcha' => [ 
  34.         'class' => 'yii\captcha\CaptchaAction', 
  35.         'fixedVerifyCode' => null, 
  36.         //背景颜色 
  37.         'backColor' => 0x000000, 
  38.         //最大显示个数 
  39.         'maxLength' => 4, 
  40.         //最少显示个数 
  41.         'minLength' => 4, 
  42.         //间距 
  43.         'padding' => 2, 
  44.         //高度 
  45.         'height' => 30, 
  46.         //宽度 
  47.         'width' => 85, 
  48.         //字体颜色 
  49.         'foreColor' => 0xffffff, 
  50.         //设置字符偏移量 
  51.         'offset' => 4, 
  52.       ], 
  53.     ]; 
  54.   } 
  55. } 

显示页面代码如下:

  1. <?php 
  2. use yii\helpers\Url; 
  3. use yii\helpers\Html; 
  4. ?> 
  5. <!doctype html> 
  6. <html lang="zh-CN"> 
  7. <head> 
  8.   <meta charset="UTF-8"> 
  9.   <title>分页显示</title> 
  10. </head> 
  11. <body> 
  12.   <form action="<?php echo Url::toRoute('index/index'); ?>" method="post"> 
  13.     验证码:<input type="text" name="verify"><br> 
  14.     <img id="verifyImg" src="<?php echo Url::toRoute('index/captcha'); ?>"><br> 
  15.     <input type="submit" value="提交"> 
  16.     <input name="_csrf" type="hidden" value="<?php echo \Yii::$app->request->csrfToken; ?>"> 
  17.   </form> 
  18.  
  19.   <?php echo Html::jsFile('@web/js/jquery-3.3.1.min.js'); ?> 
  20.   <script type="text/javascript"> 
  21.     $(function () { 
  22.       //处理点击刷新验证码 
  23.       $("#verifyImg").on("click", function () { 
  24.         $.get("<?php echo Url::toRoute('index/captcha') ?>?refresh", function (data) { 
  25.           $("#verifyImg").attr("src", data["url"]); 
  26.         }, "json"); 
  27.       }); 
  28.     }); 
  29.   </script> 
  30. </body> 
  31. </html> 

演示结果如下:

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

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

  1. <?php 
  2.  
  3. namespace app\models; 
  4.  
  5. use yii\base\Model; 
  6.  
  7. class VerifyForm extends Model 
  8. { 
  9.  
  10.   //变量名为你表单中输入验证码控件的name 
  11.   public $verify; 
  12.  
  13.   public function rules() 
  14.   { 
  15.     return [ 
  16.       ['verify', 'required', 'message' => '请填写验证码'], 
  17.       //注意captchaAction的设置,指向你显示验证码的action,这里我们的是index/captcha 
  18.       ['verify', 'captcha', 'captchaAction' => 'index/captcha', 'caseSensitive' => false, 'message' => '验证码错误'], 
  19.     ]; 
  20.   } 
  21. } 

控制器代码修改如下:

  1. <?php 
  2.  
  3. namespace app\controllers; 
  4.  
  5. use YII; 
  6. use app\models\VerifyForm; 
  7. use yii\web\Controller; 
  8.  
  9. class IndexController extends Controller 
  10. { 
  11.   public function actionIndex() 
  12.   { 
  13.     if (YII::$app->request->isPost) { 
  14.       $verify = new VerifyForm(); 
  15.       $verify->load(YII::$app->request->post(), ''); 
  16.  
  17.       //自动验证 
  18.       if ($verify->validate()) { 
  19.         echo '成功'; 
  20.       } else { 
  21.         var_dump($verify->errors); 
  22.       } 
  23.  
  24.     } else { 
  25.       return $this->renderPartial('index'); 
  26.     } 
  27.   } 
  28.  
  29.   //actions的作用主要是共用功能相同的方法 
  30.   //当用户访问index/captcha时,actions就会调用yii\captcha\CaptchaAction方法 
  31.   public function actions() 
  32.   { 
  33.     return [ 
  34.       'captcha' => [ 
  35.         'class' => 'yii\captcha\CaptchaAction', 
  36.         'fixedVerifyCode' => null, 
  37.         //背景颜色 
  38.         'backColor' => 0x000000, 
  39.         //最大显示个数 
  40.         'maxLength' => 4, 
  41.         //最少显示个数 
  42.         'minLength' => 4, 
  43.         //间距 
  44.         'padding' => 2, 
  45.         //高度 
  46.         'height' => 30, 
  47.         //宽度 
  48.         'width' => 85, 
  49.         //字体颜色 
  50.         'foreColor' => 0xffffff, 
  51.         //设置字符偏移量 
  52.         'offset' => 4, 
  53.       ], 
  54.     ]; 
  55.   } 
  56. }

Tags: YII2验证码

分享到: