How to add captcha in Yii-2 application?

断了今生、忘了曾经 提交于 2019-12-23 22:58:32

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/38865501/how-to-add-captcha-in-yii-2-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!