oowowaee
oowowaee

Reputation: 1695

Session cookie not being written in CakePHP 2

Last weekend I was trying to troubleshoot a bug on a website where the Session was not being preserved in IE - today I went to do further work on the site on my laptop, and I could no longer log in -invariably I have done something incredibly stupid.

I'm using xampp on a Windows laptop, and working on localhost, and this occurs in all browsers. I am not very experienced with troubleshooting these kinds of problems - I have been able to ascertain the following:

So, it would appear to me that the Session cookie is not being set, but I have run out of ideas as to why this might be occurring. I haven't changed any cookie related browser settings (outside of enabling cookies in IE), and I have double checked my Chrome cookie settings. I have also, as I mentioned, written some junk cookies in AppController, and I can see them created, and their data persists.

If I call $_SESSION after login(), everything looks great, but if I print $_SESSION before logging in, it's empty.

I am quite sure I have managed to do something retarded, but I have run out of ideas as to what it might be. I have restored my /app/core.php to be the Cake defaults:

    Configure::write('Session', array(
    'defaults' => 'php'
));

My login() function looks essentially as follows:

    public function login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            return $this->redirect($this->Auth->redirect());        
        } else { 
            $this->Session->setFlash(__('Invalid username or password, try again.')); 
        }

Auth settings in AppController:

class AppController extends Controller {
public $components = array(
    'Session',
    'Cookie',
    'Acl',
    'Email',
    'Auth' => array(
        'authenticate' => array(
            'Form' => array(
                'fields' => array('username' => 'email', 'password' => 'password')
        )),
        'authorize' => array(
            'Actions' => array('actionPath' => 'controllers')
        ),
        'loginRedirect' => array('controller' => 'users', 'action' => 'dashboard'),
    ),
);

And example output from printing $this->Auth->user(), $_SESSION before the redirect in login():

\app\Controller\UsersController.php (line 203)
array(
    'id' => '10',
    'name' => 'super',
    'is_active' => '1',
    'email' => '[email protected]',
    'group_id' => '3',
    'address' => '3',
    'phone' => 'xxxxx',
    'category' => 'P',
    'communication_in' => 'E',
    'created' => '2014-11-29 16:27:19',
    'modified' => '2014-11-29 16:27:19',
    'Group' => array(
        'id' => '3',
        'name' => 'Administrators',
        'created' => '2014-11-16 21:01:35',
        'modified' => '2014-11-16 21:01:35'
    )
)
\app\Controller\UsersController.php (line 204)
array(
    'Config' => array(
        'userAgent' => '4af162a3a94462226b6e93c6806203aa',
        'time' => (int) 1417317929,
        'countdown' => (int) 10,
        'language' => 'eng'
    ),
    'Auth' => array(
        'User' => array(
            'id' => '10',
            'name' => 'super',
            'is_active' => '1',
            'email' => '[email protected]',
            'group_id' => '3',
            'address' => '3',
            'phone' => 'xxxx',
            'category' => 'P',
            'communication_in' => 'E',
            'created' => '2014-11-29 16:27:19',
            'modified' => '2014-11-29 16:27:19',
            'Group' => array(
                'id' => '3',
                'name' => 'Administrators',
                'created' => '2014-11-16 21:01:35',
                'modified' => '2014-11-16 21:01:35'
            )
        )
    )
)

Last created session file:

Config|a:4:{s:9:"userAgent";s:32:"4af162a3a94462226b6e93c6806203aa";s:4:"time";i:1417317929;s:9:"countdown";i:10;s:8:"language";s:3:"eng";}Auth|a:1:{s:4:"User";a:12:{s:2:"id";s:2:"10";s:4:"name";s:5:"super";s:9:"is_active";s:1:"1";s:5:"email";s:14:"[email protected]";s:8:"group_id";s:1:"3";s:7:"address";s:1:"3";s:5:"phone";s:10:"xxxxx";s:8:"category";s:1:"P";s:16:"communication_in";s:1:"E";s:7:"created";s:19:"2014-11-29 16:27:19";s:8:"modified";s:19:"2014-11-29 16:27:19";s:5:"Group";a:4:{s:2:"id";s:1:"3";s:4:"name";s:14:"Administrators";s:7:"created";s:19:"2014-11-16 21:01:35";s:8:"modified";s:19:"2014-11-16 21:01:35";}}}

Upvotes: 0

Views: 997

Answers (1)

oowowaee
oowowaee

Reputation: 1695

Facepalm of the day: Many hours later, I finally thought to check phpinfo(), and of course, the session.cookie-domain was set to the remote site. I suppose at some point last week I edited the wrong PHP ini file.

Upvotes: 1

Related Questions