我用 mongo 将会话存储在 db 中:
1.-服务.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 添加:
framework:
session:
handler_id: session.handler.mongo
3.- 之后,如果我们要使用不同的服务器,带有 MongoDbSessionHandler 的学说mongo db 将自动将所有会话保存在完美的数据库中。会是这样的:
{ "_id" : ObjectId("50e60da9893ad520a11a7fd7"), "sess_data" : BinData(2,"pAMAAF9zZjJfYXR0cmlidXRlc3xhOjM6e3M6MTI6ImFjY2Vzc190b2tlbiI7czo1MDoiMTYzNDQzODUtYW52OVF6WmFQVDF0bHJ1ejk5OWdlU0w4U2Q1THdMQUxUSnA2V1lFaE0iO3M6MTk6ImFjY2Vzc190b2tlbl9zZWNyZXQiO3M6NDI6IlB4MDg1SWFEWktaUFBucHQ0UkRNbDRtV29VdGtQeFc0YkF4dmZzdkI2NCI7czoxNDoiX3NlY3VyaXR5X21haW4iO3M6NjMxOiJDOjY0OiJGT1NcVHdpdHRlckJ1bmRsZVxTZWN1cml0eVxBdXRoZW50aWNhdGlvblxUb2tlblxUd2l0dGVyVXNlclRva2VuIjo1NTM6e2E6Mjp7aTowO047aToxO3M6NTI4OiJhOjQ6e2k6MDtDOjMxOiJRdW9ycmFcQ29yZUJ1bmRsZVxEb2N1bWVudFxVc2VyIjoxODU6e2E6Mjp7aTowO047aToxO3M6MTYwOiJhOjk6e2k6MDtzOjA6IiI7aToxO3M6MzE6ImpkZWRyYnY4bmxzMDRvYzh3d2M0c29zb2MwazhzZzAiO2k6MjtzOjY6ImZhbmp1bCI7aTozO3M6NjoiZmFuanVsIjtpOjQ7YjowO2k6NTtiOjA7aTo2O047aTo3O2I6MTtpOjg7czoyNDoiNTBlNjBkYTlkNGMzOGVlOWMyMDAwMDAwIjt9Ijt9fWk6MTtiOjE7aToyO2E6Mjp7aTowO086NDE6IlN5bWZvbnlcQ29tcG9uZW50XFNlY3VyaXR5XENvcmVcUm9sZVxSb2xlIjoxOntzOjQ3OiIAU3ltZm9ueVxDb21wb25lbnRcU2VjdXJpdHlcQ29yZVxSb2xlXFJvbGUAcm9sZSI7czoxMjoiUk9MRV9UV0lUVEVSIjt9aToxO086NDE6IlN5bWZvbnlcQ29tcG9uZW50XFNlY3VyaXR5XENvcmVcUm9sZVxSb2xlIjoxOntzOjQ3OiIAU3ltZm9ueVxDb21wb25lbnRcU2VjdXJpdHlcQ29yZVxSb2xlXFJvbGUAcm9sZSI7czo5OiJST0xFX1VTRVIiO319aTozO2E6MDp7fX0iO319Ijt9X3NmMl9mbGFzaGVzfGE6MDp7fV9zZjJfbWV0YXxhOjM6e3M6MToidSI7aToxMzU3MjU0MTU0O3M6MToiYyI7aToxMzU3MjUzOTgzO3M6MToibCI7czoxOiIwIjt9"), "sess_id" : "f9a98866cd99a33d61204413b68d6ca5", "sess_time" : { "t" : 1357254155000, "i" : 3 } }
4.- 在 symfony 2.1 之外的一个文件中(我现在在 web 中有该文件,因为不加载组件)
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"]);
并且变量数据具有该用户的所有会话。
5.- 现在,下一步是将 access_token_secret 发送到最后一个文件,并在 $data = $mongoSession->read($_COOKIE["PHPSESSID"]); 的结果中检查它
为我工作,我正在等待您的评论