CakePHP 会话,首次刷新时不显示

发布于 2024-11-28 14:12:17 字数 1383 浏览 1 评论 0原文

我使用 CakePHP 1.3 最新版本进行管理员/用户登录。

我面临一个非常奇怪的问题,每当我尝试使用身份验证登录时,第一次尝试时它不会向我显示任何信息到 SESSION 但每当我再次刷新页面时,所有信息都会进入会话。

任何想法,为什么会发生这种情况?

代码来自 app_controller.php 文件

function beforeFilter()
{   
    $this->Auth->autoRedirect = false;
    $this->Auth->fields = array('username' => 'email_address', 'password' => 'password');
    $this->Auth->loginAction = array('admin' => false, 'controller' => 'users', 'action' => 'login');
    $this->Auth->userScope = array('User.status' => '1','User.paymentstatus' => '0');
    if($this->Auth->User())
    {
        if($this->Session->read('Auth.User.user_type') == 3) {
            $this->layout = 'admin';
            $this->Auth->loginRedirect = array('controller' => 'admins', 'action' => 'welcome');
        }
        else 
        {
            $this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'display', 'home');
        }
    }
}

function beforeRender(){
    $this->set('auth', $this->Auth->user());
    header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0"); // // HTTP/1.1
    header("Pragma: no-cache");
    header("Expires: Mon, 17 Dec 2007 00:00:00 GMT"); // Date in the past
}

最早的回复将不胜感激。

谢谢 !

I am using CakePHP 1.3 latest version for my Administrator / User login.

I am facing a very weird issue, whenever I am trying to login using Auth, on first attempt it is not displaying me any information into SESSION but whenever I refresh the page again, all information is coming to Session.

Any ideas, why this happends?

Code is here from app_controller.php file

function beforeFilter()
{   
    $this->Auth->autoRedirect = false;
    $this->Auth->fields = array('username' => 'email_address', 'password' => 'password');
    $this->Auth->loginAction = array('admin' => false, 'controller' => 'users', 'action' => 'login');
    $this->Auth->userScope = array('User.status' => '1','User.paymentstatus' => '0');
    if($this->Auth->User())
    {
        if($this->Session->read('Auth.User.user_type') == 3) {
            $this->layout = 'admin';
            $this->Auth->loginRedirect = array('controller' => 'admins', 'action' => 'welcome');
        }
        else 
        {
            $this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'display', 'home');
        }
    }
}

function beforeRender(){
    $this->set('auth', $this->Auth->user());
    header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0"); // // HTTP/1.1
    header("Pragma: no-cache");
    header("Expires: Mon, 17 Dec 2007 00:00:00 GMT"); // Date in the past
}

Earliest response would be appreciated.

Thanks !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

因为看清所以看轻 2024-12-05 14:12:17
函数 beforeFilter(){   
  父级::beforeFilter();
  $this->Auth->autoRedirect = false;
  $this->Auth->fields = array('用户名' => '电子邮件地址', '密码' => '密码');
  $this->Auth->loginAction = array('admin' => false, 'controller' => '用户', 'action' => '登录');
  $this->Auth->userScope = array('User.status' => '1','User. paymentstatus' => '0');
  if($this->Auth->user() && $this->Auth->user('user_type') == 3)
        $this->layout = 'admin';
}
函数登录(){
  if($this->Auth->user()){
    if($this->Auth->user('user_type') == 3) {
        $this->redirect(array('controller' => 'admins', 'action' => '欢迎'));
    } 别的 {
        $this->redirect(array('controller' => 'pages', 'action' => 'display', 'home'));
    }
  }
 }

您不必在 beforeRender 中设置 auth,您仍然可以在视图中使用 $this->Session->read('Auth.User'); 访问它

function beforeFilter(){   
  parent::beforeFilter();
  $this->Auth->autoRedirect = false;
  $this->Auth->fields = array('username' => 'email_address', 'password' => 'password');
  $this->Auth->loginAction = array('admin' => false, 'controller' => 'users', 'action' => 'login');
  $this->Auth->userScope = array('User.status' => '1','User.paymentstatus' => '0');
  if($this->Auth->user() && $this->Auth->user('user_type') == 3)
        $this->layout = 'admin';
}
function login(){
  if($this->Auth->user()){
    if($this->Auth->user('user_type') == 3) {
        $this->redirect(array('controller' => 'admins', 'action' => 'welcome'));
    } else  {
        $this->redirect(array('controller' => 'pages', 'action' => 'display', 'home'));
    }
  }
 }

You don't have to set auth in beforeRender, you can still access that in the view with $this->Session->read('Auth.User');

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文