dongtao9887 2013-12-11 01:04
浏览 80
已采纳

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

In my site, only when I remove the filters() method, the captcha can show up. other time the captcha doesn't work. and my php gd support is enable.

now I am using a custome WebUser, if I remove it from config, the captcha also works well.

by the way, if I access user/captcha directly, it only show a picture box, but not content, maybe can not load the picture..

here are some code segments in my UserController:

actions();

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
            )
    );
}

filters():

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.',
        ),
    );
}

My 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条回答 默认 最新

  • dooid3005 2013-12-13 04:13
    关注

    In your controller, make sure this is defined.

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

    Then, allow the action as following.

    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.',
            ),
        );
    }
    

    and in the form,

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

    if this doesnt work, try this way..

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

    to validate the capthca, define it as following in your action

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

    Your code looks fine and compare your code with this answer or please provide the source code to take a look at.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号