您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
小编给大家分享一下Yii2下如何实现点击验证码的切换,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
模型代码如下:
<?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]); } //授权规则 }
视图代码如下:
<?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 创建 use yii\helpers\Url; AppAsset::register($this); ?> <?php $this->beginPage()?> <!DOCTYPE html> <html> <head> <title>博客后台管理系统</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">博客后台管理系统</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参数为true (getVerifyCode($regenerate = true))
看完了这篇文章,相信你对“Yii2下如何实现点击验证码的切换”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。