2015-11-18 35 views
3
  1. 當我從一個用戶帳戶登錄會話設置。然後打開同一瀏覽器上的下一個標籤,並輸入登錄URL它會帶我到登錄頁面。但實際上它應該重定向到「儀表板」頁面(在我的情況)。它不能重定向到我的Auth中提到的loginRedirect(儀表板)頁面。
  2. 當我註銷時,根據我的代碼會話,cookie和緩存被刪除。但它不是重定向到logoutRedirect頁面。

我的代碼:Auth註銷在CakePHP 2.x中不起作用

應用控制器

public $components = array(
    'Session', 'RequestHandler', 'Email', 'Cookie', 
    'Auth' => array(
    'authenticate' => array(
     'Form' => array(
     'fields' => array(
      'username' => 'email', 
      'password' => 'password') 
     ) 
    ), 
     'loginRedirect' => array(
     'controller' => 'users', 
     'action' => 'login' 
     ), 
     'logoutRedirect' => array(
     'controller' => 'users', 
     'action' => 'login' 
     ) 
    ) 
    ); 

用戶控制器

登錄動作:

public function login() { 
    $this->layout = 'admin';  
    if ($this->Session->check('Auth.User')) { 
     $this->redirect(array('controller' => 'users', 'action' => 'dashboard'));  
    } 
    if (isset($this->data['User'])) { 
     if (!empty($this->data['User']['email']) && !empty($this->data['User']['password'])) { 
     if ($this->Auth->login()) { 
      $this->redirect(array('controller' => 'users', 'action' => 'dashboard')); 
     } else { 
      $this->set('error', "Email or Password mismatch."); 
     } 
     } 
    } else { 
     if ($this->Auth->loggedIn()) {     
     $this->redirect(array('controller' => 'users', 'action' => 'dashboard')); 
     } 
    } 
    } 

註銷操作:

public function logout() {  
    header('pragma: no-cache'); 
    header('Cache-Control: no-cache, must-revalidate'); 
    $this->response->disableCache();   
    $this->Session->delete('Auth.User'); 
    $this->Session->delete('User'); 
    $this->Session->destroy(); 
    $this->Cookie->destroy(); 
    return $this->redirect($this->Auth->logout()); 
} 

此代碼工作在「本地服務器」不錯,但在生產服務器無法正常工作。

+0

在上面的代碼段中重定向到儀表板的代碼在哪裏? – user221931

+0

對不起我的錯誤。請檢閱我的編輯。 –

+0

嘗試在返回之前刪除刪除和銷燬行,因爲Auth :: logout()應該爲您處理 –

回答

1

您的redirect語句應該在它們前面有return,以便代碼執行將停止在那裏。例如:

return $this->redirect(array('controller' => 'users', 'action' => 'dashboard'));