Yii2下点击验证码的切换实例代码

所属分类: 网络编程 / PHP编程 阅读数: 1336
收藏 0 赞 0 分享

最近需要用到验证码,搜索了很多关于Yii2验证码的切换的介绍,下面我来记录一下,有需要了解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>
    <h4 class="login-title">博客后台管理系统</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="用户名">
      </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))

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

php实现的美国50个州选择列表实例

这篇文章主要介绍了php实现的美国50个州选择列表实例,可实现让当前州为选中状态的功能,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP使用递归生成文章树

写递归函数,可考虑缓存,定义一些静态变量来存上一次运行的结果,多程序运行效率很有帮助.大概步骤如下:首先到数据库取数据,放到一个数组,然后把数据转化为一个树型状的数组,最后把这个树型状的数组转为html代码。下面我们来看个实例
收藏 0 赞 0 分享

wordpress安装过程中遇到中文乱码的处理方法

这篇文章主要介绍了wordpress安装过程中遇到中文乱码的处理方法,是个人项目中遇到的一个奇葩事件,经过一番研究,终于解决,这里记录下来分享给大家,有需要的小伙伴可以参考下。
收藏 0 赞 0 分享

php的crc32函数使用时需要注意的问题(不然就是坑)

这篇文章主要介绍了php的crc32函数使用时需要注意的问题(不然就是坑) ,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP中把对象转换为关联数组代码分享

这篇文章主要介绍了PHP中把对象转换为关联数组代码分享,本文直接给出实现代码,需要的朋友可以参考下
收藏 0 赞 0 分享

php检测url是否存在的方法

这篇文章主要介绍了php检测url是否存在的方法,涉及php中get_headers及正则匹配的技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

php获取twitter最新消息的方法

这篇文章主要介绍了php获取twitter最新消息的方法,涉及php操作curl及正则替换的技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

php遍历CSV类实例

这篇文章主要介绍了php遍历CSV类,实例分析了php针对csv文件的打开、读取及遍历的技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP使用mysqldump命令导出数据库

最近用php写一个数据备份的功能。做法是使用php的system函数执行mysqldump命令,进行备份,这里分享给大家,有需要的小伙伴可以参考下。
收藏 0 赞 0 分享

PHP用反撇号执行外部命令

shell_exec() 命令行实际上仅是反撇号 ` 操作符的变体,如果您编写过 shell 或 Perl 脚本,您就知道可以在反撇号操作符内部捕捉其他命令的输出。
收藏 0 赞 0 分享
查看更多