身份验证注销在 CakePHP 中不起作用 2.x
Auth logout is not working in CakePHP 2.x
- 当我从一个用户帐户会话登录时 set.Then 在同一浏览器上打开下一个选项卡并输入登录名 url 它会将我带到登录名 page.But 实际上它应该重定向到"dashboard" 页面(在我的例子中)。它无法重定向到我的
Auth
. 中提到的 loginRedirect
(dashboard) 页面
- 当我注销时,根据我的代码会话,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());
}
此代码在 "local server" 中运行良好,但在生产服务器中不起作用。
您的 redirect
语句前面应该有 return
,这样代码执行就会停在那里。例如:
return $this->redirect(array('controller' => 'users', 'action' => 'dashboard'));
- 当我从一个用户帐户会话登录时 set.Then 在同一浏览器上打开下一个选项卡并输入登录名 url 它会将我带到登录名 page.But 实际上它应该重定向到"dashboard" 页面(在我的例子中)。它无法重定向到我的
Auth
. 中提到的 - 当我注销时,根据我的代码会话,cookie 和缓存被删除。但它不会重定向到
logoutRedirect
页面。
loginRedirect
(dashboard) 页面
我的代码:
应用程序控制器
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());
}
此代码在 "local server" 中运行良好,但在生产服务器中不起作用。
您的 redirect
语句前面应该有 return
,这样代码执行就会停在那里。例如:
return $this->redirect(array('controller' => 'users', 'action' => 'dashboard'));