如何在Yii-2应用程序中添加验证码? [英] How to add captcha in Yii-2 application?

查看:81
本文介绍了如何在Yii-2应用程序中添加验证码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将验证码添加到登录表单。

I'm trying to add the captcha to the login form.

我的环境:


  • Yii 2

  • php 5.4.45 TS

  • IIS 10.0

  • Windows 10

  • Yii 2
  • php 5.4.45 TS
  • IIS 10.0
  • Windows 10

login.php LoginForm.php中 SiteController.php 我添加了以下内容(仅显示了相关部分):

In login.php, LoginForm.php and SiteController.php I added the following (shown only the relevant parts):

backend\views\site\login.php:

backend\views\site\login.php:

use yii\captcha\Captcha;
...
<?= $form->field($model, 'captcha')->widget(Captcha::className()) ?>
...

common\models\LoginForm.php:

common\models\LoginForm.php:

...
public $captcha;
...
public function rules()
{
    return [
        ... 
        [['username', 'password', 'captcha'], 'required'],           
        ['captcha', 'captcha'],
    ];
}

后端\控制器lerSiteController.php:

backend\controllers\SiteController.php:

public function actions()
{
    return [
        ...
        'captcha' => [
            'class' => 'yii\captcha\CaptchaAction',
//                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
        ],
    ];
}

我已经下载并安装了 ImageMagick-7.0.2-Q16

此处我已经下载了php_imagick-3.4.1-5.4-ts-vc9-x86 .zip并从那里解压缩php_imagick.dll。然后在 / php / ext /

As described here I have downloaded php_imagick-3.4.1-5.4-ts-vc9-x86.zip and extract php_imagick.dll from there. Then added php_imagick.dll in /php/ext/

php.ini中添加php_imagick.dll 我添加了以下内容:

...
[PHP_IMAGICK]
extension=php_imagick.dll
...

然后重新启动IIS,但不会显示验证码,并且我在日志中看到以下内容:

Then restart IIS, but captcha is not displayed and I see the following in the logs:

2016-08-10 07:28:21 [127.0.0.1][-][h1a65krn8scqc9auk56flmesi6][error][yii\base\InvalidConfigException] exception 'yii\base\InvalidConfigException' with message 'Either GD PHP extension with FreeType support or ImageMagick PHP extension with PNG support is required.' in C:\projects\aisnew\vendor\yiisoft\yii2\captcha\Captcha.php:180

重新启动操作系统后:

2016-08-10 07:01:22 [127.0.0.1][-][h1a65krn8scqc9auk56flmesi6][error][yii\base\ErrorException:32] exception 'yii\base\ErrorException' with message 'PHP Startup: ' in Unknown:0

我在某处阅读到最新版本正在Windows 10上运行,但需要安装Visual C ++ 2013 Redistributable Package。我检查是否已安装此软件包。

I read somewhere that the most recent version is working on Windows 10 but need to install the Visual C++ 2013 Redistributable Package. I checked that this package is installed.

如何在Yii-2应用程序中添加验证码?我尝试了ImageMagick和php_imagick.dll的不同组合,但没有任何效果。

How to add captcha in Yii-2 application? I tried different combinations of the ImageMagick and php_imagick.dll, but nothing is working.

推荐答案

我认为这应该可行

<?= $form->field($model, 'captcha')->widget(Captcha::className(), 
            ['template' => '<div class="captcha_img">{image}</div>'
                . '<a class="refreshcaptcha" href="#">'
                . Html::img('/images/imageName.png',[]).'</a>'
                . 'Verification Code{input}',
            ])->label(FALSE); ?> 

我所做的是,添加了一个包含验证码图像,刷新验证码图像的模板(提供适当的路径),该字段的标签和文本的输入字段。

what i have done is, added a template which contains a captcha image, a refresh captcha image(provide suitable path), a label to the field and an input field for text.

这篇关于如何在Yii-2应用程序中添加验证码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆