简体   繁体   中英

CakePHP doesn't set any cookies and session after changing server

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.

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');

Check in your Conroller code should be in src/Controller/AppController.php for below points

1) have you loaded cookie component properly in initialize() or beforeFilter() method?

2) do you have configured domain and path in your cookie configuration using $this->Cookie->configKey() , if Yes, then change domain to new domain or remove domain configuration.

3) delete cache folders for model and persistence from tmp folder

For more information about Cookie refer Document

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM