How to add captcha in Yii-2 application?

2019-07-30 04:55发布

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

My environment:

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

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

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,
        ],
    ];
}

I have downloaded and installed ImageMagick-7.0.2-Q16

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/

In php.ini I added the following:

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

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

After restarting the operating system:

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

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.

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

I would be very grateful for the information. Thanks to all.

2条回答
2楼-- · 2019-07-30 05:22

I think this should work

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

查看更多
迷人小祖宗
3楼-- · 2019-07-30 05:42

I have just made a custom code for my Yii2 website. It is a text based captcha. Check it if it can helps you. In model:-

public $captcha;
public $recaptcha;

......

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

In controller at this line to create and update action after define $model:-

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

And in view section add these lines:-

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

查看更多
登录 后发表回答