2
  1. 当我从一个用户帐户登录时设置会话。然后在同一浏览器上打开下一个选项卡并输入登录网址,它会将我带到登录页面。但实际上它应该重定向到“仪表板”页面(在我的情况下)。它无法重定向到loginRedirect我的Auth.
  2. 当我注销时,根据我的代码会话,cookie 和缓存被删除。但它不会重定向到logoutRedirect页面。

我的代码:

应用控制器

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

此代码在“本地服务器”中运行良好,但在生产服务器中无法运行。

4

1 回答 1

1

您的redirect语句应该return在它们前面,以便代码执行将在那里停止。例如:

return $this->redirect(array('controller' => 'users', 'action' => 'dashboard'));
于 2015-11-29T10:50:27.070 回答