gpt4 book ai didi

php - 如何在 Yii-2 应用程序中添加验证码?

转载 作者:行者123 更新时间:2023-12-04 02:10:36 25 4
gpt4 key购买 nike

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

我的环境:

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

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

后端\views\site\login.php:

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

通用\模型\LoginForm.php:

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

后端\ Controller \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 的不同组合,但没有任何效果。

最佳答案

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

public $captcha;
public $recaptcha;

……

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

在此行的 Controller 中,在定义 $model 之后创建和更新操作:-

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

然后在 View 部分添加这些行:-

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

关于php - 如何在 Yii-2 应用程序中添加验证码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38865501/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com