溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Yii2下如何實現(xiàn)點擊驗證碼的切換

發(fā)布時間:2021-07-20 14:36:08 來源:億速云 閱讀:137 作者:小新 欄目:開發(fā)技術

小編給大家分享一下Yii2下如何實現(xiàn)點擊驗證碼的切換,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

模型代碼如下:

<?php
namespace app\admin\models;
use yii;
use yii\base\model;
use yii\captcha\Captcha;
class LoginForm extends Model{
  public $verifyCode;
  public $password;
  public $username;
  public function rules(){
    return [
      ['verifyCode','captcha','captchaAction'=>'/admin/login/captcha','message'=>'{attribute}'],
      [['password','username'],'required'],
    ];

  }
}

控制器代碼如下:

<?php
namespace app\admin\controllers;
use Yii;
use yii\web\Controller;
use app\admin\models\LoginForm;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\captcha\CaptchaAction;
class LoginController extends Controller{

 public function actions(){
   return [
     'captcha'=>'yii\captcha\CaptchaAction',
     'maxLength'=>4,
     'minLength'=>3,
     'width'=>10,
     'height'=>10
   ];
  }

  public function actionIndex(){
    $log = new LoginForm();
    
     return $this->renderPartial("index",['model'=>$log]);
  }
  //授權規(guī)則
}

視圖代碼如下:

<?php
use yii\helpers\Html;
use yii\bootstrap\Nav;
use yii\bootstrap\NavBar;
use yii\widgets\Breadcrumbs;
use app\assets\AppAsset;
use yii\widgets\ActiveFiel;
use yii\widgets\ActiveForm;
use yii\captcha\Captcha;
use app\components\HelloWidget;
//url 創(chuàng)建
use yii\helpers\Url;
AppAsset::register($this);
?>
<?php $this->beginPage()?>
<!DOCTYPE html>
<html>
<head>
  <title>博客后臺管理系統(tǒng)</title>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  <?=Html::cssFile('@web/css/bootstrap.min.css')?>
  <?=Html::cssFile('@web/css/style.css')?>
  <?=Html::jsFile("@web/js/jquery-1.11.3.min.js");?>
  <?=Html::jsFile("@web/js/bootstrap.min.js");?>
<?php ?>

</head>
  <?php $this->beginBody()?>
<body>
<div class="login-box">
  <div class="login">
    <div class="login-icon">

    </div>
    <h5 class="login-title">博客后臺管理系統(tǒng)</h5>
    <?php $form=ActiveForm::begin(['method'=>'post','action'=> \Yii::$app->urlManager->createUrl('/admin/login/index')])?>
      <div class="login-input-box mb10">
       <input type="text" class="form-control" name="username" placeholder="用戶名">
      </div>
      <div class="login-input-box mb10">

        <input class="form-control" type="password" name="password" placeholder="密碼">
      </div>
      <div class="login-input-box mb30">
        <input class="form-control" type="text" name="code" placeholder="驗證碼">

        <span class="login-code">
          <img id="code_img" src="<?= Url::toRoute('/admin/login/captcha')?>" title="點擊刷新驗證碼" onClick="get_code(this);">
        </span>
      </div>
      <input class="inputbtn bg-success btn-block" type="submit" value="登 錄">
    <?php ActiveForm::end();?>
  </div>
</div>
<script type="text/javascript">
  //刷新驗證碼
  function get_code(obj)
  {

    if(!obj)
    {
      obj = document.getElementById('code_img');
    }
    obj.src = obj.src + "&t="+Date.parse(new Date());
  }
</script>
</body>
  <?php $this->beginBody()?>
</html>

<?php $this->endPage()?>

這里整個代碼差不多了但是要設置一樣更重要的 app/vendor/yiisoft/yii2/captcha/的文件下的CaptchaAction.php這文件中修改getVerifyCode($regenerate = false) 的方法$regenerate參數(shù)為true (getVerifyCode($regenerate = true))

看完了這篇文章,相信你對“Yii2下如何實現(xiàn)點擊驗證碼的切換”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

yii
AI