2011-08-08 39 views
0

我正在使用CakePHP 1.3最新版本进行管理员/用户登录。CakePHP会话,在第一次刷新时不显示

我面临一个非常奇怪的问题,每当我尝试使用身份验证登录时,第一次尝试时它不会向我显示任何信息到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 
} 

最早响应,将不胜感激。

谢谢!

+0

认为你成功登录后重定向?在设置相同的请求期间,您无法访问会话数据(在$ _SESSION中)。 – deizel

+0

是的我重定向到欢迎页面,一旦登录,但是当用户第一次登录时,它不会重定向,并且当我再次刷新页面时,它将重定向到应该去的地方。 –

+0

好的,在这种情况下,它可能会有所帮助您可以发布正在执行的操作来登录/重定向,以便我们可以仔细查看。 – deizel

回答

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

+0

非常简单,因为您已经设置了'$ this-> Auth-> autoRedirect = false',您需要在UsersController :: login()操作中手动重定向(如[cookbook](http:// book .cakephp.org /视图/ 1274/autoRedirect))。通常会把'$ this->重定向($ this-> Auth-> redirect());',但由于您的重定向URL不同,上面的代码应该可以满足您的需求。 – deizel

相关问题