Auth注销在CakePHP 2.x中不起作用 [英] Auth logout is not working in CakePHP 2.x

查看:200
本文介绍了Auth注销在CakePHP 2.x中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 当我从一个用户帐户登录会话设置。然后在同一浏览器打开下一个选项卡,并输入登录URL,它带我到登录页面。但实际上它应该重定向到信息中心页面(在我的情况下)。它不能重定向到 Auth 中提及的 loginRedirect (信息中心)页面。

  2. 当我注销时,根据我的代码会话,cookie和缓存被删除。但它不会重定向到 logoutRedirect 页面。

  1. When I login from one user account session is set.Then opening the next tab on same browser and enter login url it takes me to the login page.But actually it should redirect to the "dashboard" page(in my case). It can't redirect to loginRedirect(dashboard) page as mentioned in my Auth.
  2. When i logout, as per my code session,cookie and cache are deleted. but it's not redirect to logoutRedirect page.

我的代码:

应用程式控制器

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

此代码在本地服务器中正常工作,但在生产服务器中不工作。

This code is working fine in "local server" but not working in production server.

推荐答案

您的重定向语句应该具有 return 在它们前面,以便代码执行停止。例如:

Your redirect statements should have return in front of them so that code execution will stop there. For example:

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

这篇关于Auth注销在CakePHP 2.x中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆