How to exchange session data between Symfony controllers and classic standalone PHP script where sessions are managed in memcache?
I have a site based on Symfony framework and I had to add a legacy php script running directly in the web directory.
I also need to split the session data between Symfony platform controllers and a standalone php script.
Sessions on my server and Symfony framework are configured to memcached server (127.0.0.1 11211).
Here are my Symfony config files:
parameters.yml
session_memcached_host: 127.0.0.1
session_memcached_port: 11211
session_memcached_prefix: 'BPY_'
session_memcached_expire: 3600
config.yml
imports:
- { resource: session.yml }
...
session:
handler_id: session.handler.memcached
session.yml
services:
session.memcached:
class: Memcache
arguments:
persistent_id: %session_memcached_prefix%
calls:
- [ addServer, [ %session_memcached_host%, %session_memcached_port% ]]
session.handler.memcached:
class: Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcacheSessionHandler
arguments: [@session.memcached, { prefix: %session_memcached_prefix%, expiretime: %session_memcached_expire% }]
A session in Symfony controllers is set up and read using:
$request->getSession->get('key');
$request->getSession->set('key', $value);
In a standalone old php script, I am using a global array of sessions to set and retrieve data. And in theory, to allow the use of Symfony data and my script data that I have set, and get the data in:
$_SESSION['_sf2_attributes']
So finally, with this configuration, when I set data in symfony controllers, I cannot see it in the stand-alone php script, and when I set data in PHP script, I cannot see it in Symfony Controllers.
What's wrong with my configuration or my code?
source to share
You should check Legacy Application Bridge with Symfony Sessions but in general you can configure it like this:
framework:
session:
storage_id: session.storage.php_bridge
handler_id: ~
source to share
I finally solved the problem.
In the parameters.yml file, I put:
session_memcached_prefix : ''
instead:
session_memcached_prefix : 'BPY_'
and the two resources can share session data.
In Symfony controllers, I am still using
$request->getSession->get('key');
$request->getSession->set('key', $value);
And in a standalone php script, I use set and get data in this array:
$_SESSION['_sf2_attributes']
So,
$_SESSION['_sf2_attributes']['key'] = $value
source to share