loginRedirect
中提到的Auth
(信息中心)页面。logoutRedirect
页。我的代码:
App控制器
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 :(得分:1)
您的redirect
语句应该在return
前面,以便代码执行停止。例如:
return $this->redirect(array('controller' => 'users', 'action' => 'dashboard'));