Yii2下點(diǎn)擊驗(yàn)證碼的切換實(shí)例代碼
最近需要用到驗(yàn)證碼,搜索了很多關(guān)于Yii2驗(yàn)證碼的切換的介紹,下面我來(lái)記錄一下,有需要了解Yii2下驗(yàn)證碼的切換的朋友可參考。希望此文章對(duì)各位有所幫助。
模型代碼如下:
<?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]); } //授權(quán)規(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ái)管理系統(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> <h4 class="login-title">博客后臺(tái)管理系統(tǒng)</h4> <?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="用戶(hù)名"> </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="驗(yàn)證碼"> <span class="login-code"> <img id="code_img" src="<?= Url::toRoute('/admin/login/captcha')?>" title="點(diǎn)擊刷新驗(yàn)證碼" 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"> //刷新驗(yàn)證碼 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()?>
這里整個(gè)代碼差不多了但是要設(shè)置一樣更重要的 app/vendor/yiisoft/yii2/captcha/的文件下的CaptchaAction.php這文件中修改getVerifyCode($regenerate = false) 的方法$regenerate參數(shù)為true (getVerifyCode($regenerate = true))
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Yii2框架實(shí)現(xiàn)登陸添加驗(yàn)證碼功能示例
- Yii2 如何在modules中添加驗(yàn)證碼的方法
- Yii2簡(jiǎn)單實(shí)現(xiàn)給表單添加驗(yàn)證碼的方法
- Yii2增加驗(yàn)證碼步驟詳解
- yii2中添加驗(yàn)證碼的實(shí)現(xiàn)方法
- Yii1.0 不同頁(yè)面多個(gè)驗(yàn)證碼的使用實(shí)現(xiàn)
- Yii 2.0自帶的驗(yàn)證碼使用經(jīng)驗(yàn)分享
- Yii輸入正確驗(yàn)證碼卻驗(yàn)證失敗的解決方法
- Yii框架實(shí)現(xiàn)的驗(yàn)證碼、登錄及退出功能示例
- Yii使用Captcha驗(yàn)證碼的方法
- yii實(shí)現(xiàn)創(chuàng)建驗(yàn)證碼實(shí)例解析
- YII2框架中驗(yàn)證碼的簡(jiǎn)單使用方法示例
相關(guān)文章
如何優(yōu)雅的使用 laravel 的 validator驗(yàn)證方法
web 開(kāi)發(fā)過(guò)程中經(jīng)常會(huì)需要進(jìn)行參數(shù)驗(yàn)證,這篇文章主要介紹了如何優(yōu)雅的使用 laravel 的 validator驗(yàn)證方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-11-11使用Entrust擴(kuò)展包在laravel 中實(shí)現(xiàn)RBAC的功能
這篇文章主要介紹了使用Entrust擴(kuò)展包在laravel 中實(shí)現(xiàn)RBAC的功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03Laravel 5 框架入門(mén)(二)構(gòu)建 Pages 的管理功能
這篇文章主要介紹了Laravel 5 框架入門(mén)的第二篇文章,給大家講解的是構(gòu)建 Pages 的管理功能,十分的詳細(xì),有需要的小伙伴可以參考下。2015-04-04