2

I want to add Captcha verification on login page. But there is an error, captcha image is not showing and when I try to view the captcha image http://xxx.yii2/site/captcha?v=5806eb0c3aa05 , below error display

The image “http://xxx.yii2/site/captcha?v=5806ce094fa84” cannot be displayed because it contains errors.

Below is my SiteController

class SiteController extends Controller {
  public function behaviors()
  {
    return [
        'access' => [
            'class' => AccessControl::className(),
            'only' => ['logout','resetPassword'],
            'rules' => [
                [
                    'actions' => ['logout','resetpassword'],
                    'allow' => true,
                    'roles' => ['@'],
                ],
            ],
        ],
        'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
                'logout' => ['post'],
            ],
        ],
    ];
}

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

LoginForm controller

class LoginForm extends Model {
  public $username;
  public $password;
  public $rememberMe = true;
  public $verifyCode;

  private $_user = false;

  const ERROR_NONE=0;
  const ERROR_USERNAME_INVALID=1;
  const ERROR_USERNAME_LOCKED = 3;
  const ERROR_USERNAME_INACTIVE = 4;
  const ERROR_PASSWORD_INVALID=2;

  public $role_id;
  public $salt;
  public $_id;
  private $_identity;

  /**
   * @return array the validation rules.
   */
  public function rules()
  {
    return [

        [['username', 'password'], 'required'],
        ['rememberMe', 'boolean'],
        ['username','validateMember'],
        ['password', 'validatePassword'],
        ['verifyCode', 'captcha'],

    ];
  } 

Login form view

<?php $form = ActiveForm::begin([
    'id' => 'login-form',
    'options' => ['class' => 'form-horizontal'],
    'fieldConfig' => [
        'template' => "{label}\n<div class=\"col-lg-3\">{input}</div>\n<div class=\"col-lg-8\">{error}</div>",
        'labelOptions' => ['class' => 'col-lg-1 control-label'],
    ],
]); ?>

    <?= $form->field($model, 'username') ?>

    <?= $form->field($model, 'password')->passwordInput() ?>

    <?= $form->field($model, 'verifyCode')->widget(Captcha::className()) ?>

    <?= $form->field($model, 'rememberMe')->checkbox([
        'template' => "<div class=\"col-lg-offset-1 col-lg-3\">{input} {label}</div>\n<div class=\"col-lg-8\">{error}</div>",
    ]) ?>

    <div class="form-group">
        <div class="col-lg-offset-1 col-lg-11">
            <?= Html::submitButton('<i class="fa fa-sign-in fa-fw"></i> Login', ['class' => 'btn btn-primary', 'name' => 'login-button']) ?>
        </div>
    </div>

<?php ActiveForm::end(); ?>
kasmawati
  • 51
  • 2
  • 9

1 Answers1

3

Check this.

Note that CaptchaAction requires either GD2 extension or ImageMagick PHP extension.

See: Class yii\captcha\CaptchaAction

Another possible reason of the problem is wrong action specified. To resolve it you need to specify if manually both in model and widget.

Model:

['verifyCode', 'captcha', 'captchaAction' => 'site/captcha']

Widget:

<?= $form->field($model, 'verifyCode')->widget(Captcha::className(), [
    'captchaAction' => 'site/captcha'
]) ?>
Sergey Okatov
  • 1,270
  • 16
  • 19
  • I have imagick, checking using Captcha::checkRequirements(); – kasmawati Oct 19 '16 at 06:10
  • If your retrieve what the particular errors are, it will be much easier to understand the problem. You need to explore the debug panel. There is a selector at the upper left corner where you can select other request than that currently shown. – Sergey Okatov Oct 19 '16 at 06:14
  • The only error the I get is - Failed to load the given url – kasmawati Oct 19 '16 at 06:29
  • What is the failed url? Your captcha action is at route 'site/captcha'. You have to ensure it is accessible (1) and (2) your widget and model point out to that. – Sergey Okatov Oct 19 '16 at 13:06
  • I have change the model and form based on your suggestion and – kasmawati Oct 20 '16 at 03:10
  • Update SiteController and added captcha on behaviors function but still got error - Error message 'Reload the page to get source for: http://xxx.yii2/site/captcha?v=5808353638cbb' – kasmawati Oct 20 '16 at 03:14
  • > 'Reload the page to get source for: xxx.yii2/site/captcha?v=5808353638cbb' This is the very important thing which leads to the solution of your problem. The problem looks to be not related to Yii. Check [these answers](http://stackoverflow.com/questions/8486165/what-can-cause-a-persistent-reload-the-page-to-get-source-for-error-in-firebug). – Sergey Okatov Oct 20 '16 at 05:52
  • I have found the error , on SiteController I have extend it from another Controller class which is at app\components\Controller and this controller is extends from \yii\web\Controller. But on the page I only rewrite the afterAction function. Captcha image is displaying if class SiteController extends \yii\web\Controller – kasmawati Oct 20 '16 at 09:08