CakePHP 2.x sessions behaving inconsistently between local dev and production
Asked Answered
B

2

1

I have a CakePHP 2.x site I'm working on which performs as intended locally. Login works, session flash messages work, etc. When I push the code to my staging/prod server it's breaking.

Logins no longer work, no session flash messages appear, some controller actions that should be redirecting to /user/login are displaying nothing (empty document), etc.

I'm at a loss as to what the problem would be. Based on the issues I'm experiencing and some searching I've done I believe I've ruled out problems like whitespace after the closing ?> in a code-only PHP file (controllers). I'm using DB sessions, and I see session records being created in the DB on my local instance, but not on the remote staging/prod instance.

Any assistance would be much appreciated. Thanks.

Bearer answered 6/3, 2013 at 20:24 Comment(2)
You are able to connect to the database from your production servers via the same user creds?Granulocyte
Different DBs and different creds, but yes the DB connection is working. I just did a test adding a new user record and got similarly strange behavior. The add form loaded/displayed, clicking submit (which posted to the same /users/add URL) I got a completely blank page/empty document, going back to the /users/index view I could see the new record had been added. Bizarre.Bearer
B
2

One of the recommendations I came across frequently was to ensure that there was no whitespace after the closing PHP tag in a code-only file (or preferably to not actually have a closing PHP tag). Checking all my files showed that to be the case. Somehow, however, I managed to put a single line break before the opening PHP tag in AppController.php and that was the issue. My apologies to anyone who wasted time on this. I just hope this helps someone in the future who clumsily makes the same mistake.

Bearer answered 11/3, 2013 at 20:1 Comment(1)
I do still wonder why things were working fine on my local instance (Mac OS) and a coworker's (Windows) but not on the staging (Linux) server.Bearer
J
2

In you app/Config/core.php check out these thing.

  1. If you are using SSL and non-SSL based protocols, make sure you have cookie_secure set as false.

    Configure::write('Session', array(
       'defaults' => 'php',
       'ini' => array(
           'session.cookie_secure' => false
       )
    ));
    
  2. Try changing Session's configuration from php defaults to cake or db as

    Configure::write('Session', array(
        'defaults' => 'php', // change 'php' to 'cake' or 'database'
        'cookie' => 'my_app',
        'timeout' => 4320 //3 days
    ));
    
  3. Also try setting Session.checkAgent to false, just for once to ensure if it is a browser issue.

  4. Try changing Session.name of your session, it defaults to 'CAKEPHP'

    Configure::write('Session', array(
        'name' => 'New-Session-name'
        'defaults' => 'php', // change 'php' to 'cake' or 'database'
        'cookie' => 'my_app',
        'timeout' => 4320 //3 days
    ));
    
  5. Remove all cache files from all sub-directories of /app/tmp

  6. Set debug level higher to 1, to do cache refresh. If you still don't see an error, try setting error_reporting to true in php.ini. (Although, this one is very obvious I am still pointing it out in case you might have missed it out)

Hope this helps

Jadwigajae answered 8/3, 2013 at 5:12 Comment(1)
I've looked into these items (at least the ones that apply in my case) and none of them seem to be the issue or at least are not revealing anything. I am noticing a more basic problem which seems to be that those actions that should be redirecting are not for some reason. Adding new records (and the records do get added successfully) is yielding a blank page with a 200 HTTP response instead of the expected 302. Why there's this inconsistency in behavior between my local and staging/prod environments is still a mystery. Thanks for your suggestions though.Bearer
B
2

One of the recommendations I came across frequently was to ensure that there was no whitespace after the closing PHP tag in a code-only file (or preferably to not actually have a closing PHP tag). Checking all my files showed that to be the case. Somehow, however, I managed to put a single line break before the opening PHP tag in AppController.php and that was the issue. My apologies to anyone who wasted time on this. I just hope this helps someone in the future who clumsily makes the same mistake.

Bearer answered 11/3, 2013 at 20:1 Comment(1)
I do still wonder why things were working fine on my local instance (Mac OS) and a coworker's (Windows) but not on the staging (Linux) server.Bearer

© 2022 - 2024 — McMap. All rights reserved.