Yii2 驗證碼


注冊時候增加驗證碼,  首先 在 SignupForm.php 增加:

 1 namespace app\modules\XXX\models;//這個你們寫自己的命名空間,我以我的modules項目路徑為例
 2 
 3 use Yii;
 4 
 5 use yii\base\Model;
 6 
 7 use yii\captcha\Captcha;
 8 
 9 class LoginForm extends Model
10 { 
11     public $name; 
12 
13     public $email; 
14 
15     public $subject; 
16 
17     public $body; 
18 
19     public $verifyCode;//驗證碼這個變量是必須建的,因為要儲存驗證碼的值` /** * @return array the validation rules. */
20 
21     public function rules() 
22     { 
23            return [ 
24                     // name, email, subject and body are required 
25                     [['name', 'email', 'subject', 'body'], 'required'], 
26                     // email has to be a valid email 
27                     ['email', 'email'], 
28                     // verifyCode needs to be entered correctly 
29                     ['verifyCode', 'captcha'],//注意這里,在百度中查到很多教程,這里寫的都不一樣,最 簡單的寫法就像我這種寫法,當然還有其它各種寫法 
30                     //['verifyCode', 'captcha','captchaAction'=>'admin/index/captcha','message'=>'驗 證碼不正確!'], 這種寫法在官網自帶的LoginForm.php中有寫到,大家可以沒事看看 ]; 
31     }
32     /*
33     * * @return array customized attribute labels 
34     */ 
35     public function attributeLabels() 
36     { 
37          return [ 
38                    // 'verifyCode' => 'Verification Code', 
39                    'verifyCode' => '',//在官網的教程里是加上了英文字母,我這里先給去掉了,這里去 掉會不會產生影響因為我還沒做接收驗證,只做了驗證碼顯示的功能,你們可以自己測試下 
40             ]; 
41     } 

 

在SiteController.php

 1   /**
 2      * @驗證碼獨立操作  下面這個actions注意一點,驗證碼調試出來的樣式也許你並不滿意,這里就可
 3 以需修改,這些個參數對應的類是@app\vendor\yiisoft\yii2\captcha\CaptchaAction.php,可以參照這個
 4 類里的參數去修改,也可以直接修改這個類的默認參數,這樣這里就不需要改了
 5      */
 6     public function actions()
 7     {        
 8         return  [    
 9 //                 'captcha' => 
10 //                    [
11 //                        'class' => 'yii\captcha\CaptchaAction',
12 //                        'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
13 //                    ],  //默認的寫法
14                         'captcha' => [
15                                     'class' => 'yii\captcha\CaptchaAction',
16                                     'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
17                                     'backColor'=>0x000000,//背景顏色
18                                     'maxLength' => 6, //最大顯示個數
19                                     'minLength' => 5,//最少顯示個數
20                                     'padding' => 5,//間距
21                                     'height'=>40,//高度
22                                     'width' => 130,  //寬度  
23                                     'foreColor'=>0xffffff,     //字體顏色
24                                     'offset'=>4,        //設置字符偏移量 有效果
25                                     //'controller'=>'login',        //擁有這個動作的controller
26                             ],
27         ];
28     }
View Code

 

在signup.php
<?= $form->field($model, 'verifyCode')->widget(\yii\captcha\Captcha::className(),[
                    
                ]) ?>
 
<?php 
          $form = ActiveForm::begin([
                                  'id' => 'login-form',                                   
                                      ]); 
  ?>
<?php 
    echo Captcha::widget(['name'=>'captchaimg','captchaAction'=>'login/captcha','imageOptions'=>['id'=>'captchaimg', 'title'=>'換一個', 'alt'=>'換一個', 'style'=>'cursor:pointer;margin-left:25px;'],'template'=>'{image}']);//我這里寫的跟官方的不一樣,因為我這里加了一個參數(login/captcha),這個參數指向你當前控制器名,如果不加這句,就會找到默認的site控制器上去,驗證碼會一直出不來,在style里是可以寫css代碼的,可以調試樣式 ?>
<?php 
ActiveForm::end(); 
?>

 

 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM