【问题标题】:Yii Captcha errorYii 验证码错误
【发布时间】:2013-05-18 19:10:07
【问题描述】:

我在 Yii 工作,我只是一个初学者,正在努力学习这个框架,这就是我陷入困境的地方:

我已经创建了一个用户模型和所需的表单,我正在尝试为它实现验证码:

这是我在用户模型中的验证规则:

$public verifyCode

public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('username, password, email', 'required'),
            array('username','unique'),
            array('email','email'),
            array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),
            array('username, password', 'length', 'max'=>45),
            array('email', 'length', 'max'=>100),
            array('active', 'length', 'max'=>1),
            array('created_on, updated_on', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, username, password, email, created_on, updated_on, active', 'safe', 'on'=>'search'),
        );
    }

这是我在 userController 中覆盖的 action() :

public function actions(){
        return array(
        'captcha'=>array(
            'class' => 'CCaptchaAction',
            )
            );
    }

这是我的视图文件:

<?php if(CCaptcha::checkRequirements()): ?>
    <div class="row">
        <?php echo $form->labelEx($model,'verifyCode'); ?>
        <div>
        <?php $this->widget('CCaptcha'); ?>
        <?php echo $form->textField($model,'verifyCode'); ?>
        </div>
        <div class="hint">Please enter the letters as they are shown in the image above.
        <br/>Letters are not case-sensitive.</div>
        <?php echo $form->error($model,'verifyCode'); ?>
    </div>
    <?php endif; ?>

据我说,我认为我做的一切都是正确的,但是验证码图像没有生成。哦,是的,GD 库已安装,如果我导航到站点/联系人,则验证码生成良好。

我好像不明白,我哪里弄错了。

这是我看到的:

表单似乎工作正常,但是我看不到验证码图像。

任何帮助将不胜感激。

问候,

【问题讨论】:

  • CCaptchaAction 问题可能有访问权限。你看过this solution吗?
  • 同时检查 php 中是否有 gd 库。尝试直接获取图片,看看有没有错误,将图片下载到电脑上查看内容,也可以有一些提示。
  • gd 没问题,因为CCapcha::checkRequirements() 返回true
  • 我得到了答案,因为我没有修改我的控制器代码。
  • 修改我的控制器 accessRules 但验证码图像未显示后

标签: php yii validation captcha


【解决方案1】:

我得到了答案,这是因为控制器中定义的访问规则,我不得不像这样修改控制器 accessControl:

public function accessRules()
    {
        return array(
            array('allow',  // allow all users to perform 'index' and 'view' actions
                'actions'=>array('index','view','captcha'),
                'users'=>array('*'),
            ),
            array('allow', // allow authenticated user to perform every action
                'actions'=>array('create','update','admin','delete'),
                'users'=>array('@'),
            ),

            array('deny',  // deny all users
                'users'=>array('*'),
            ),
        );
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-11
    • 1970-01-01
    • 2014-01-08
    • 2016-03-05
    • 2011-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多