Yii2验证码图像未显示

时间:2016-10-19 03:51:45

标签: yii2 captcha

我想在登录页面上添加验证码验证。但是有一个错误,验证码图像没有显示,当我尝试查看验证码图像http://xxx.yii2/site/captcha?v=5806eb0c3aa05时,错误显示下面

无法显示图像“http://xxx.yii2/site/captcha?v=5806ce094fa84”,因为它包含错误。

以下是我的SiteController

EWOULDBLOCK

LoginForm控制器

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

登录表单视图

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'],

    ];
  } 

1 个答案:

答案 0 :(得分:1)

检查一下。

  

请注意,CaptchaAction需要GD2扩展或ImageMagick PHP扩展。

请参阅:Class yii\captcha\CaptchaAction

问题的另一个可能原因是指定了错误的操作。要解决它,您需要在模型和小部件中手动指定。

型号:

  if(isset($_POST['push'])) //tried it commenting also!
  {
      foreach($_POST['data'] as $d){
         echo $d;
      }
  }

窗口小部件:

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