Reputation: 1304
I have problem with a session in Symfony 2.1 for a site inside Symfony (in one controller). I set the session like this:
$this->get("session")
->set('UserMongoId', $this->get('security.context')->getToken()->getUser()->getId());
After that in another file (inside the web folder of Symfony) outside symfony 2.1, I do this:
use Symfony\Component\HttpFoundation\Request;
$loader = require_once __DIR__.'/../app/bootstrap.php.cache';
require_once __DIR__.'/../app/AppKernel.php';
$kernel = new AppKernel('rpod', true);
$kernel->loadClassCache();
$request = Request::createFromGlobals();
$session = new \Symfony\Component\HttpFoundation\Session\Session();
$session->start();
var_dump ($session);
$user_id = $session->get("UserMongoId");
However I received null, and I dont know how to make it work, any ideas?
now I trying MongoDbSessionHandler and storage the session in db with mongo
Upvotes: 0
Views: 2888
Reputation: 1304
I get it store the session in db with mongo:
1.- service.yml
parameters:
mongo.session.options:
database: quorra_session
collection: session
services:
mongo.connection:
class: MongoDoctrine\MongoDB\Connection
factory_service: doctrine.odm.mongodb.document_manager
factory_method: getConnection
calls:
- [initialize, []]
mongo:
class: Mongo
factory_service: mongo.connection
factory_method: getMongo
session.handler.mongo:
class: Symfony\Component\HttpFoundation\Session\Storage\Handler\MongoDbSessionHandler
arguments: [@mongo, %mongo.session.options%]
2.- confing.yml add :
framework:
session:
handler_id: session.handler.mongo
3.- after that doctrinemongo db with MongoDbSessionHandler will save all the session automatically in db perfect if we are going to work with direferents servers. will be something like that:
{ "_id" : ObjectId("50e60da9893ad520a11a7fd7"), "sess_data" : BinData(2,"pAMAAF9zZjJfYXR0cmlidXRlc3xhOjM6e3M6MTI6ImFjY2Vzc190b2tlbiI7czo1MDoiMTYzNDQzODUtYW52OVF6WmFQVDF0bHJ1ejk5OWdlU0w4U2Q1THdMQUxUSnA2V1lFaE0iO3M6MTk6ImFjY2Vzc190b2tlbl9zZWNyZXQiO3M6NDI6IlB4MDg1SWFEWktaUFBucHQ0UkRNbDRtV29VdGtQeFc0YkF4dmZzdkI2NCI7czoxNDoiX3NlY3VyaXR5X21haW4iO3M6NjMxOiJDOjY0OiJGT1NcVHdpdHRlckJ1bmRsZVxTZWN1cml0eVxBdXRoZW50aWNhdGlvblxUb2tlblxUd2l0dGVyVXNlclRva2VuIjo1NTM6e2E6Mjp7aTowO047aToxO3M6NTI4OiJhOjQ6e2k6MDtDOjMxOiJRdW9ycmFcQ29yZUJ1bmRsZVxEb2N1bWVudFxVc2VyIjoxODU6e2E6Mjp7aTowO047aToxO3M6MTYwOiJhOjk6e2k6MDtzOjA6IiI7aToxO3M6MzE6ImpkZWRyYnY4bmxzMDRvYzh3d2M0c29zb2MwazhzZzAiO2k6MjtzOjY6ImZhbmp1bCI7aTozO3M6NjoiZmFuanVsIjtpOjQ7YjowO2k6NTtiOjA7aTo2O047aTo3O2I6MTtpOjg7czoyNDoiNTBlNjBkYTlkNGMzOGVlOWMyMDAwMDAwIjt9Ijt9fWk6MTtiOjE7aToyO2E6Mjp7aTowO086NDE6IlN5bWZvbnlcQ29tcG9uZW50XFNlY3VyaXR5XENvcmVcUm9sZVxSb2xlIjoxOntzOjQ3OiIAU3ltZm9ueVxDb21wb25lbnRcU2VjdXJpdHlcQ29yZVxSb2xlXFJvbGUAcm9sZSI7czoxMjoiUk9MRV9UV0lUVEVSIjt9aToxO086NDE6IlN5bWZvbnlcQ29tcG9uZW50XFNlY3VyaXR5XENvcmVcUm9sZVxSb2xlIjoxOntzOjQ3OiIAU3ltZm9ueVxDb21wb25lbnRcU2VjdXJpdHlcQ29yZVxSb2xlXFJvbGUAcm9sZSI7czo5OiJST0xFX1VTRVIiO319aTozO2E6MDp7fX0iO319Ijt9X3NmMl9mbGFzaGVzfGE6MDp7fV9zZjJfbWV0YXxhOjM6e3M6MToidSI7aToxMzU3MjU0MTU0O3M6MToiYyI7aToxMzU3MjUzOTgzO3M6MToibCI7czoxOiIwIjt9"), "sess_id" : "f9a98866cd99a33d61204413b68d6ca5", "sess_time" : { "t" : 1357254155000, "i" : 3 } }
4.- In one file outside symfony 2.1 ( i have the file now in web for not load the components )
use Symfony\Component\ClassLoader\ApcClassLoader;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\MongoDbSessionHandler;
$loader = require_once __DIR__.'/../app/bootstrap.php.cache';
require_once __DIR__.'/../app/AppKernel.php';
//$kernel = new AppKernel('prod', false);
$kernel = new AppKernel('dev', true);
$kernel->loadClassCache();
$mongo = new Mongo();
$options["database"] = "quorra_session";
$options["collection"] = "session";
$mongoSession = new MongoDbSessionHandler($mongo,$options);
$data = $mongoSession->read($_COOKIE["PHPSESSID"]);
and the variable data has all the session for this user.
5.- Now, the next step is send to this last file the access_token_secret and check it in the result of $data = $mongoSession->read($_COOKIE["PHPSESSID"]);
works for me, I'm waiting for your comments
Upvotes: -1
Reputation: 4704
Try to replace
$session = new \Symfony\Component\HttpFoundation\Session\Session();
$session->start();
with
$session = $request->getSession();
Because you need to take the session object you created with Request::createFromGlobals();, and not a new one.
Upvotes: 2
Reputation: 3656
Your second block of code creates a new $session variable. Obviously its values will be null. You need to assign $session in the second block by retrieving it somehow.
Upvotes: 0