dpa55065 2016-08-31 08:48
浏览 36

更改服务器后,CakePHP不会设置任何cookie和会话

I have an application based on CakePHP version 3.2.10. I'm totally new in CakePHP so sorry if it is a banal problem. In my application I use CSRF component and Auth component configured in this way:

$this->loadComponent('Auth', [
            'authorize'=> 'Controller',
            'authenticate' => [
                    'Form' => [
                            'fields' => [
                                    'username' => 'email',
                                    'password' => 'password'
                            ],
                            'scope' => [
                                    'Users.active' => 1,
                            ]
                    ]
            ],
            'loginAction' => [
                    'controller' => 'Users',
                    'action' => 'login'
            ],
            'logoutAction' => [
                    'controller' => 'Users',
                    'action' => 'logout'
            ],
            'logoutRedirect' => [
                    'controller' => 'Pages',
                    'action' => 'index'
            ],
            'unauthorizedRedirect' => '/', // $this->referer()
    ]);

and login action like

public function login()
{
    $this->set('title', 'Logowanie');
    $this->set('bodyclass', 'main-page');

    if ($this->request->is('post')) {
        $user = $this->Auth->identify();
        if ($user) {
            if($user['blocked'] == 0) {
                $this->Auth->setUser($user);
                if ($this->Auth->user('role') == 'admin')
                    return $this->redirect(['controller' => 'Admin', 'action' => 'index']);
                return $this->redirect($this->Auth->redirectUrl());
            }
            else{
                $this->Flash->error('Konto zostało zablokowane przez administratora serwisu. Skontaktuj się z Biurem Obsługi.');
            }
        } else $this->Flash->error('Błędne dane logowania. Spróbuj ponownie.');
    }
}

Now the problem:

Few days ago I changed server where application is running, and after changing it logging in stopped working. After clicking login there is a message CSRF Token cookie is missing. To test if the component is the problem i disabled csrf and try again then white screen appears and nothing happen if i refresh page i'm not logged in. I checked the working version and not working version and realized that Cake not store any cookies on new server, while on old there is everything ok and cookies are set.

After few researches i found out that not only cookies not work but all sessions. I try to dump $_SEESION but it shows something only right after calling $this->Auth->setUser($user), and nowhere else. So i look through some solutions and find that there is a setting in config/app.php to set up the session:

'Session' => [
    'defaults' => 'php',
],

And read that when set in that way the session is stored in default php dir. So i changed it to cake(even make a dir sessions in tmp folder and added 777 permissions). But the problem was not solved. I have no idea why it not work. I tried setting cookiePath and other settings i Session config, but it still not work.

I think that this may be the server problem(permissions). So few words about servers: Old server where everything was working was my private server(i have full access), new server(or maybe virtual server/host) is on one of hosting companies(home.pl) where i have almost no privileges to configure.

  • 写回答

2条回答 默认 最新

  • doutuanxiao4619 2016-08-31 08:52
    关注

    Make sure you follow these steps:

    //For Set
    var $var = array('Cookie');
    
    //For Write
    $this->Cookie->write('YOUR DESIRED NAME', cookieData, $expires = null);
    
    //For Read
    $this->Cookie->read('YOUR DESIRED NAME');
    
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么