Yii2.x 中captcha 验证码使用的一些心得

为了自我学习和交流PHP(jquery,linux,lamp,shell,javascript,服务器)等一系列的知识,希望光临本博客的人可以进来交流。寻求共同发展。搭建平台。本人博客也有许多的技术文档,希望可以为你提供一些帮助。


QQ群: 191848169   点击链接加入群【PHP技术交流(总群)】


首先对应控制器中定义captcha,对应模型中声明captcha变量。

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

设置一些简单属性,也可以不设。

对应视图中添加表单:

  <?= $form->field($user_login,'captcha')->widget(yii\captcha\Captcha::className()
                                        ,['captchaAction'=>'user/captcha',
                                        'imageOptions'=>['alt'=>'点击换图','title'=>'点击换图', 'style'=>'cursor:pointer']]);?>

captchaAction 指定captcha所在的控制器路径,默认是‘site/captcha’,不换到指定位置的话,很容易,验证码就显示不出来。
imageOptions设定一些参数,例如 手势,提示等等。

对应布局中,如下:以确保你在点击验证码可以自动刷新

<?php $this->beginPage() ?>
<?php $this->beginBody() ?>
//your codes...
<?= $contents; ?>
//your codes...
<?php $this->endBody() ?>
<?php $this->endPage() ?>

最后,控制器中调用render而非renderPartial:
return $this->render('login',['user_login'=>$user_login]);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值