如何在 Yii-2 应用程序中添加验证码?
How to add captcha in Yii-2 application?
我正在尝试将验证码添加到登录表单。
我的环境:
- Yii 2
- php 5.4.45 TS
- IIS 10.0
- Windows 10
在 login.php
、LoginForm.php
和 SiteController.php
中我添加了以下内容(仅显示相关部分):
backend\views\site\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'],
];
}
backend\controllers\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
我在某处读到最新版本正在 Windows10 上运行,但需要安装 Visual C++ 2013 Redistributable Package。我检查了这个包是否已安装。
如何在Yii-2应用中添加验证码?我尝试了 ImageMagick 和 php_imagick.dll 的不同组合,但没有任何效果。
我认为这应该可行
<?= $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); ?>
我所做的是,添加了一个模板,其中包含验证码图像、刷新验证码图像(提供合适的路径)、字段标签和文本输入字段。
我刚刚为我的 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) ?>
我用了这个link,我的问题就解决了。 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
...
我正在尝试将验证码添加到登录表单。
我的环境:
- Yii 2
- php 5.4.45 TS
- IIS 10.0
- Windows 10
在 login.php
、LoginForm.php
和 SiteController.php
中我添加了以下内容(仅显示相关部分):
backend\views\site\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'],
];
}
backend\controllers\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.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
我在某处读到最新版本正在 Windows10 上运行,但需要安装 Visual C++ 2013 Redistributable Package。我检查了这个包是否已安装。
如何在Yii-2应用中添加验证码?我尝试了 ImageMagick 和 php_imagick.dll 的不同组合,但没有任何效果。
我认为这应该可行
<?= $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); ?>
我所做的是,添加了一个模板,其中包含验证码图像、刷新验证码图像(提供合适的路径)、字段标签和文本输入字段。
我刚刚为我的 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) ?>
我用了这个link,我的问题就解决了。 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 ...