0

I'm having problems with PHP sessions that only occur on my testing server (everything works fine on my localhost). I'm developing a custom Wordpress theme, based on Roots. My localhost is OS X (PHP 5.4.4) and the testing server is Ubuntu (5.3.10-1ubuntu3.8).

The problems include:

  • New sessions created each time I refresh the page (which I can see by rendering session_id() in the footer and checking /var/lib/php5/session)
  • Functions called through an AJAX request unable to access the correct session, even though session_name() and session_start() are called before they try

Other details:

I'm trying to save variables into a named session, so each time I call session_start() I'm currently doing it like this:

session_name('my_session'); //Not sure if this line strictly required
if (!session_id()) {
    session_name('my_session');
    session_start();
}

The above is first called in a function init_sessions, hooked into Wordpress like this: add_action('init', 'init_sessions');, then also used in the other files that need access to session variables (e.g. those requested via AJAX).

On localhost, I can see the session file created in /Applications/MAMP/tmp/php and also see a session appear under the Cookies tab in Firebug. However on my testing server, although (too many) session files are created in /var/lib/php5/session, I don't see the session appear in Firebug.

Running phpinfo() doesn't show any significant difference between the PHP directives on my localhost and those on my testing server.

The testing server is really two (Rackspace) servers with a load balancer, but I don't think this is an issue as session persistence is set up.

The testing server is set up as a subdomain e.g. test.my-domain.com.

I've got PHP error reporting turned on but haven't noticed any.

I've deactivated all other Wordpress plugins.

I'm sure it's more likely to be a problem with my script than Rackspace's set-up, but I'm a bit stumped at the moment. I'm particularly curious about why I can see session files created on the testing server in /var/lib/php5/session, but don't see them appear in Firebug's Cookies tab.

Any ideas very welcome. Thanks!

user2950747
  • 695
  • 1
  • 6
  • 19
  • Side-note: Developing on DIFFERENT environments is ALWAYS bad. Dev+production should always be exactly the same setup. – Sliq Nov 28 '13 at 13:17
  • 1
    @Panique as long as you test your application on a production like system before going live, everything should be ok. – peaceman Nov 28 '13 at 13:23
  • 1
    You have to use session_start in every script/page – don’t make it depend on dubious conditions, but just _do_ it. – CBroe Nov 28 '13 at 13:39
  • @CBroe Thanks for the suggestion, but would that explain the session working on localhost and not on the testing server? There are quite a few PHP files and scripts in my theme, but I've just removed the `if`, and added `session_name()` and `session_start()` to the majority without seeing any change. – user2950747 Nov 28 '13 at 14:34

1 Answers1

0

Ok - think I've identified what's going on (though not resolved it yet). It looks as though the problem is down to Varnish. When a user is logged-in, the session functions perfectly. Thanks to everyone that suggested a fix.

user2950747
  • 695
  • 1
  • 6
  • 19