Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't access Session variables on different servers

I have dedicated a server to maintain Memcached and store sessions, so that all my servers can work on the same session without difficulties.

But somehow I think I may have misunderstood the meaning of Memcached possibilities about PHP sessions.

I thought that I would be able to stand on Apache 1 a.domain.com and create a session e.g. $_SESSION['test'] = "This string is saved in the session" and then go to Apache 2 b.domain.com or c.domain.com and simply continue the session and type echo $_SESSION['test']; and it would output the string.

It doesn't, but i am sure that I was told that memcached would be a great tool if you have multiple webservers to share the same session.

What have I done wrong?

enter image description here

By the way. We seriously need a fully detailed tutorial or ebook to describe how to set up the server, using php, building clusters etc. based on Memcached.

In my php.ini file it says:

session.save_path = "192.168.100.228:11211"

Tutorials told me not to define a protocol, and the ip address has been given to the Apache 3 - memcached Server

Here is an image of phpinfo() enter image description here

The domain in session.cookie_domain is not called domain but it is a .local. It has been changed for this image.

EDIT:

Just for information. When I am using a simple Memcached based PHP command - everything works perfectly. But somehow when I am trying to save a session, the memcached server doesn't store the item.

This works:

<?php
$m = new Memcached();
$m->addServer('192.168.100.228', 11211);

$m->set('int', 99);
$m->set('string', 'a simple string');
$m->set('array', array(11, 12));
/* expire 'object' key in 5 minutes */
$m->set('object', new stdclass, time() + 300);


var_dump($m->get('int'));
var_dump($m->get('string'));
var_dump($m->get('array'));
var_dump($m->get('object'));
?>

This doesn't work

<?php
session_start();
$_SESSION['name'] = "This is a simple string.";
?>

EDIT 2: THE SOLUTION

I noticed that after deleting the cache history including cookies etc. the browser didn't finish the job. The problem continued due to the fact, that it hang on to the original individual session id, which made each subdomain separated from each other.

Everything defined here is correct, just make sure your browser resets its cookies when you ask it to. >.<

like image 698
Corfitz. Avatar asked Apr 24 '13 11:04

Corfitz.


1 Answers

By default (session) cookies are domain specific, so set the cookie domain in your php.ini

session.cookie_domain = ".domain.com"

Also see here Allow php sessions to carry over to subdomains

Make sure to restart your webserver and clear all of your browser cookies after making the change. Your browser could get confused if you have cookies with the same name but different subdomains.

Other things to check:

That the sessions work fine on each individual server.

Make sure the session handler is set properly by using phpinfo() if you are working with a large codebase especially inherited / 3rd party stuff there may be something overriding it.

If you are using 3rd party code - like phpbb for instance - check that the cookie settings are correct in there too.

(please note this answer tidied to remove brainstorming, kept all relevant info)

like image 159
CodeMonkey Avatar answered Oct 22 '22 15:10

CodeMonkey