Sharing session between Symfony1 and Symfony2 using Memcache

3 June 2013

Not long ago, Theodo released it’s SessionBundle which aim is to share the session between a symfony1x and a Symfony2 website.

On a local server, very little configuration is needed to use this bundle. The sharing is available after a few minutes configuration.

When we implemented it on one website, the session sharing was working on our Vagrant box, but once it has been deployed we noticed that it was not working anymore.

The platform used by this website was configured to be able to accept more servers if needed, so the sesssion was stored on a memcache server.

We can deal with this in 3 simple steps:
1. Check the Symfony1
2. Setup the Symfony2
3. Setup the session sharing


Install the php5-memcache package using either pecl or apt-get:

apt-get install php5-memcache
pecl install memcache

Add a file in your php config directory (/etc/php5/conf.d/999.memcache.ini) to force the memcache session torage:

session.save_handler = "memcache"
session.save_path = "memcache.server:11211"

Symfony 1x

Configure your application with the following directive:

memcache_enabled: true
    server_address: memcache.server
    server_port: 11211

Symfony 2.2

Install the TheodoEvolutionSessionBundle and follow the installation directive.

By default, Symfony2 overrides the php.ini directive to store the session.
You will have to define a service in the config.yml file to store the session in memcache:

        class: Memcache
            - [ addServer, [ %memcache_host%, %memcache_port% ]]
        class: Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcacheSessionHandler
        arguments: [ @memcache, { expiretime: %memcache_expire% } ]

You will also have to specify some arguments for the session in your config.yml file:

    handler_id: %session_handler_id%
    name: frontend_session #Or put the name related to your symfony application
    save_path: %session_save_path%
    cookie_domain: %session_cookie_domain%

Put the parameters into your parameter.yml file:

memcache_host: memcache.test
memcache_port: 11211
memcache_expiretime: 86400
session_handler_id: session.handler.memcache
session_save_path: 'tcp://memcache.server:11211'

Session with subdomains

If the Symfony2 webserver is accessed by a subdomain, you will need to make your session cookie from the symfony1x accessible by it.
In the factories.yml add the following configuration:


If you look deeper into the session sharing, on the symfony1x the cookie should looks like session_number

On your local server, if you list all the files in the /var/lib/php5 directory, you will see a file prefix by sess_ and followed by the session_number.

On the memcache server, there are no prefixes, so you just have to set it in your parameters.yml file:

session_prefix: ''

You cannot set the session prefix to ~, the default prefix will be used

Filed under : Programming — by @ 16 h 02 min

1 Comment »

  1. Jesus GOnzalez say :
    16 July 2014

    I followed your instructions and i have this error:

    Warning: MemcachePool::get(): php_network_getaddresses: getaddrinfo failed: Name or service not known

    22 h 20 min

Leave an answer