CakePHP Session, не отображается при первом обновлении - PullRequest
0 голосов
/ 08 августа 2011

Я использую последнюю версию CakePHP 1.3 для входа в систему моего Администратора / Пользователя.

Я сталкиваюсь с очень странной проблемой: всякий раз, когда я пытаюсь войти в систему с использованием Auth, с первой попытки он не отображает мне какую-либо информацию в 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
}

Самый ранний ответ приветствуется.

Спасибо!

1 Ответ

1 голос
/ 09 августа 2011
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');

...