Question

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!

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top