【问题标题】:How to add captcha in Yii-2 application?如何在 Yii-2 应用程序中添加验证码?
【发布时间】:2016-08-10 06:11:28
【问题描述】:

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

我的环境:

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

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

后端\视图\站点\login.php:

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

common\models\LoginForm.php:

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

后端\控制器\SiteController.php:

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

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

here 所述,我已经下载了 php_imagick-3.4.1-5.4-ts-vc9-x86.zip 并从那里提取 php_imagick.dll。然后在/php/ext/中添加php_imagick.dll

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

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

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

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。我检查了这个包是否已安装。

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

【问题讨论】:

  • 如果您愿意接受替代解决方案,请查看google.com/recaptcha/intro/index.html
  • @AlekseyBykov 你有没有像 Jason 提到的那样实现 recaptcha?如果你有,你能发布答案吗?
  • @AlekseyBykov 感谢您的及时回复。即使我正在研究相同的 github 链接并尝试实施。谢谢。

标签: php yii yii2 captcha imagick


【解决方案1】:

我刚刚为我的 Yii2 网站制作了一个自定义代码。它是基于文本的验证码。检查它是否可以帮助您。 在模型中:-

public $captcha;
public $recaptcha;

......

[['name', 'captcha','recaptcha'], 'required'],
['recaptcha', 'compare', 'compareAttribute' => 'captcha', 'operator' => '=='],

在此行的控制器中定义 $model 后创建和更新操作:-

$model->captcha = rand(11111,99999);

并在视图部分添加这些行:-

<?= $form->field($model, 'captcha')->hiddenInput()->label(false) ?>
<div class="form-group">
     <mark><b><?= $model->captcha ?></b></mark>                    
</div>
<?= $form->field($model, 'recaptcha')->textInput(['placeholder' => 'Enter Captcha'])->label(false) ?>

【讨论】:

    【解决方案2】:

    我认为这应该可行

    <?= $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); ?> 
    

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

    【讨论】:

    • 一些解释可以改善答案并帮助未来的读者
    • 代码很简单。我只是提供了一个模板,其中包含一个验证码图像、一个刷新验证码图像(该图像的路径由开发者端提供)、一个标签(验证码)和一个文本输入字段。
    【解决方案3】:

    我使用了这个链接,我的问题就解决了。 https://github.com/yiisoft/yii2/issues/6392 您无需执行以下操作。 我已经下载并安装了 ImageMagick-7.0.2-Q16

    如此处所述,我已下载 php_imagick-3.4.1-5.4-ts-vc9-x86.zip 并从那里提取 php_imagick.dll。然后在/php/ext/中添加php_imagick.dll

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

    ... [PHP_IMAGICK] 扩展 = php_imagick.dll ...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-18
      • 2019-11-18
      相关资源
      最近更新 更多