在YII框架中,Captcha不显示(与accessControl冲突)

时间:2013-12-11 01:04:52

标签: php yii

在我的网站中,只有当我删除filters()方法时,才会显示验证码。其他时间验证码不起作用。我的php gd支持是启用。

现在我正在使用一个客户WebUser,如果我从配置中删除它,验证码也可以正常工作。

顺便说一句,如果我直接访问用户/验证码,它只显示一个图片框,但不是内容,可能无法加载图片..

这是我的UserController中的一些代码段:

动作();

public function actions()
{
    return array(
        // captcha action renders the CAPTCHA image displayed on the contact page
        'captcha'=>array(
            'class'=>'CCaptchaAction',
            'backColor'=>0xFFFFFF,
            'minLength' => 4,
            'maxLength' => 4,
            'testLimit' => 99999
            )
    );
}

过滤器():

public function filters()
{
    // return the filter configuration for this controller, e.g.:
    return array(
        "accessControl",
    );
}

accessRulse():

public function accessRules()
{
    return array(
        array('allow',  // allow all users to perform 'index' and 'view' actions
            'actions'=>array('captcha'),
            'users'=>array('*'),
        ),
        array('allow',  // allow all users to perform 'index' and 'view' actions
            'actions'=>array('index','login','signup'),
            'expression'=>'Yii::app()->user->isGuest',
        ),
        array('allow', // allow authenticated user to perform 'create' and 'update' actions
            'actions'=>array('cpassword','info','logout'),
            'expression'=>'!Yii::app()->user->isGuest',
        ),
        array('allow', // allow admin user to perform 'admin' and 'delete' actions
            'users'=>array('admin@example.com'),
        ),
        array('deny',  // deny all users
            'users'=>array('*'),
            'message'=>'Access Denied.',
        ),
    );
}

我的WebUsers.php

<?php 

// this file must be stored in: 
// protected/components/WebUser.php 

class WebUser extends CWebUser { 

  // Store model to not repeat query. 
  private $_model; 

  // Return first name. 
  // access it by Yii::app()->user->first_name 
  public function getDisplayName(){ 
    $user = $this->loadUser(Yii::app()->user->id);
    if($user)
        return $user->display_name; 
  } 

  public function getGroupId(){
      $user = $this->loadUser(Yii::app()->user->id); 
      return $user->group_id;
  }

  // This is a function that checks the field 'role' 
  // in the User model to be equal to 1, that means it's admin 
  // access it by Yii::app()->user->isAdmin() 
  public function isAdmin(){ 
      $user = $this->loadUser(Yii::app()->user->id); 
      return intval($user->group_id) == 1; 
  } 

  public function isGroupAAS(){
      $user = $this->loadUser(Yii::app()->user->id); 
      return intval($user->group_id) == 1001;
  }


  // Load user model. 
  protected function loadUser($id=null) 
    { 
        if($this->_model===null) 
        { 
            if($id!==null) 
                $this->_model=User::model()->findByPk($id); 
        } 
        return $this->_model; 
    }

  protected function afterLogin($fromCookie){
      $user = $this->loadUser($this->id);
      $user->last_login_ip = Yii::app()->request->userHostAddress;
      $user->last_login_time = new CDbExpression('NOW()');
      $user->save();
  }

} 
?> 

4 个答案:

答案 0 :(得分:1)

在您的控制器中,确保已定义。

    // captcha action renders the CAPTCHA image displayed on the contact page
    'captcha'=>array(
        'class'=>'CCaptchaAction',
        'backColor'=>0xFFFFFF,
    ),

然后,允许以下操作。

public function accessRules()
{
    return array(
        array('allow',  // allow all users to perform 'index' and 'view' actions
            'actions'=>array('captcha'),
            'users'=>array('*'),
        ),

        array('deny',  // deny all users
            'users'=>array('*'),
            'message'=>'Access Denied.',
        ),
    );
}

并以表格形式

<?php $this->widget('CCaptcha'); ?><br>
<?php echo CHtml::textField('captcha'); ?>

如果这不起作用,请尝试这种方式..

<?php $this->widget('CCaptcha', array('captchaAction' => 'site/captcha')); ?>

验证capthca,在行动中将其定义为以下

$captcha=Yii::app()->getController()->createAction("captcha");
$code = $captcha->verifyCode;
if($code === $_REQUEST['captcha']){
}

您的代码看起来很好,并将您的代码与此答案进行比较,或者请提供源代码以便查看。

答案 1 :(得分:0)

允许访问您的验证码显示方法,即actions

public function accessRules()
{
    return array(
        array('allow',  // allow all users to perform 'index' and 'view' actions
            'actions'=>array('actions'),
            'users'=>array('*'),
        ),

答案 2 :(得分:0)

我已经使用我的SiteController&amp;它(验证码行动)在联系行动下运作良好。你能否发布完整的UserController代码来审核&amp;找出确切的原因?

答案 3 :(得分:0)

只需添加&#39;验证码&#39;在像这样的动作数组中

public function accessRules()
{
    return array(array('allow',  // allow admin user to perform these actions
            'actions'=>array('index','view','add','captcha'),
            'users'=>array('admin'),
        ), ...