0

我正在使用 CakePHP 1.3 最新版本进行管理员/用户登录。

我面临一个非常奇怪的问题,每当我尝试使用 Auth 登录时,第一次尝试时它不会向我显示任何信息,SESSION但每当我再次刷新页面时,所有信息都会进入 Session。

任何想法,为什么会发生这种情况?

代码来自app_controller.php文件

function beforeFilter()
{   
    $this->Auth->autoRedirect = false;
    $this->Auth->fields = array('username' => 'email_address', 'password' => 'password');
    $this->Auth->loginAction = array('admin' => false, 'controller' => 'users', 'action' => 'login');
    $this->Auth->userScope = array('User.status' => '1','User.paymentstatus' => '0');
    if($this->Auth->User())
    {
        if($this->Session->read('Auth.User.user_type') == 3) {
            $this->layout = 'admin';
            $this->Auth->loginRedirect = array('controller' => 'admins', 'action' => 'welcome');
        }
        else 
        {
            $this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'display', 'home');
        }
    }
}

function beforeRender(){
    $this->set('auth', $this->Auth->user());
    header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0"); // // HTTP/1.1
    header("Pragma: no-cache");
    header("Expires: Mon, 17 Dec 2007 00:00:00 GMT"); // Date in the past
}

尽早回复将不胜感激。

谢谢 !

4

1 回答 1

1
function beforeFilter(){   
  parent::beforeFilter();
  $this->Auth->autoRedirect = false;
  $this->Auth->fields = array('username' => 'email_address', 'password' => 'password');
  $this->Auth->loginAction = array('admin' => false, 'controller' => 'users', 'action' => 'login');
  $this->Auth->userScope = array('User.status' => '1','User.paymentstatus' => '0');
  if($this->Auth->user() && $this->Auth->user('user_type') == 3)
        $this->layout = 'admin';
}
function login(){
  if($this->Auth->user()){
    if($this->Auth->user('user_type') == 3) {
        $this->redirect(array('controller' => 'admins', 'action' => 'welcome'));
    } else  {
        $this->redirect(array('controller' => 'pages', 'action' => 'display', 'home'));
    }
  }
 }

您不必在 beforeRender 中设置身份验证,您仍然可以在视图中访问它$this->Session->read('Auth.User');

于 2011-08-09T09:44:35.207 回答