So I have this weird problem where PHP sessions are saving to the database (MongoDB) multiple times, all with the same ID. The first record has the correct session data, while the second has the wrong session data. This is causing my sessions to fail immediately on the next page (or next AJAX call) since it's grabbing the latest record.
所以我有一个奇怪的问题,PHP会话被多次保存到数据库(MongoDB)中,所有的都是相同的ID,第一个记录有正确的会话数据,而第二个记录有错误的会话数据。这导致我的会话在下一页(或下一个AJAX调用)上立即失败,因为它正在获取最新的记录。
Here is my session ini settings:
以下是我的会话ini设置:
array(
'session.auto_start' => '0',
'session.bug_compat_42' => '',
'session.bug_compat_warn' => '',
'session.cache_expire' => '180',
'session.cache_limiter' => 'must-revalidate',
'session.cookie_domain' => '',
'session.cookie_httponly' => '1',
'session.cookie_lifetime' => '0',
'session.cookie_path' => '/',
'session.cookie_secure' => '1',
'session.entropy_file' => '',
'session.entropy_length' => '0',
'session.gc_divisor' => '1000',
'session.gc_maxlifetime' => '14400',
'session.gc_probability' => '1',
'session.hash_bits_per_character' => '5',
'session.hash_function' => '0',
'session.name' => 'Game',
'session.referer_check' => '',
'session.save_handler' => 'user',
'session.save_path' => '/var/lib/php/session',
'session.serialize_handler' => 'php',
'session.use_cookies' => '1',
'session.use_only_cookies' => '1',
'session.use_trans_sid' => '0'
)
And my CakePHP settings:
和我的CakePHP设置:
Configure::write('Session', array(
'cookie' => PROJECT,
'checkAgent' => false,
'autoRegenerate' => false, // We don't want it to regenerate
'defaults' => 'database',
'handler' => array(
'engine' => 'DatabaseSession',
'model' => 'Session'
),
'ini' => array(
'session.cookie_lifetime' => 0, // Until the browser is closed
'session.cookie_httponly' => true,
'session.referer_check' => '',
'session.cache_limiter' => 'nocache'
),
));
And heres an example of multiple record in the database (notice they have the same ID but different content):
这是数据库中多个记录的示例(注意它们有相同的ID,但内容不同):
{ "_id" : ObjectId("5011b8c9564d444d04000083"), "id" : "mnor9aspiacp3g41qq8g0fotk2", "data" : "Config|a:3:{s:9:\"userAgent\";s:0:\"\";s:4:\"time\";i:1343353096;s:9:\"countdown\";i:10;}fb_466806330012492_code|s:74:\"2.AQC3yKQXi2e-6_VjI.3600.1343343600.1-672639577|pkfXkSypomU_3RY2-xM3A93rGrVo\";fb_4668063X30012492_access_token|s:110:\"AAAGojtF6Xs0wBAIoUwOoMU3WIt4ZBAih0NUvwtGePBxYv6oOT1QvbEZAb77j2h8ZBXKnpHrXktxHmz1WXCyr5VR9RwdaiGzrxfNRgEPr06gAZDZD\";fb_466806330012492_user_id|s:9:\"xxxx\";Game|a:6:{s:7:\"user_id\";s:24:\"4ff60631ec0f92c5690002a6\";s:9:\"player_id\";s:24:\"4ff60631ec0f92c5690002a7\";s:7:\"game_id\";s:24:\"5009ce57d09b3efe03000000\";s:11:\"facebook_id\";s:9:\"xxx\";s:11:\"accessToken\";s:110:\"AAAGoXjtF6s0wBAIoUwOoMXU3WIt4ZBAih0NUvwXtGePBxYv6oOT1QvbEZAb77j2h8ZBKnpHrXktxHmz1WCyr5VR9RwdaiGzrxfNRgEXPr06gAZDZD\";s:5:\"round\";i:1;}", "expires" : NumberLong(1343338697), "modified" : ISODate("2012-07-26T21:38:17.083Z"), "created" : ISODate("2012-07-26T21:38:17.083Z") }
{ "_id" : ObjectId("5011b8ca564d445c0400008f"), "id" : "mnor9aspiacp3g41qq8g0fotk2", "data" : "Config|a:3:{s:9:\"userAgent\";s:0:\"\";s:4:\"time\";i:1343353098;s:9:\"countdown\";i:10;}", "expires" : NumberLong(1343338698), "modified" : ISODate("2012-07-26T21:38:18.689Z"), "created" : ISODate("2012-07-26T21:38:18.689Z") }
I can't seem to figure out this problem. I tried switching to memcache storage, but the same thing happens. A few more things to note:
我似乎解决不了这个问题。我尝试切换到memcache存储,但同样的事情发生了。还有几点需要注意:
- The site is using HTTPS
- 该网站正在使用HTTPS
- I am using the Facebook API which adds to the session, however I am creating the app session first
- 我正在使用Facebook API来添加会话,但是我首先创建的是应用程序会话
- The cookie with the session ID does exist and is being passed in all headers for every request
- 具有会话ID的cookie确实存在,并在每个请求的所有头部中传递。
- I am running on Amazon EC2
- 我正在Amazon EC2上运行
I am running out of ideas here as my ini settings look correct, and I've placed debug statements all through the app to ensure the order of operations is correct. Everything looks good so far.
当我的ini设置看起来是正确的时候,我的想法就没有了,我把调试语句都放到了应用程序中,以确保操作的顺序是正确的。目前看来一切都很好。
1 个解决方案
#1
0
On the mongodb side, you can use ensureIndex({id: 1}, {unique: true}) to prevent the insert from happening.
在mongodb端,您可以使用ensureIndex({id: 1}, {unique: true})来防止插入发生。
#1
0
On the mongodb side, you can use ensureIndex({id: 1}, {unique: true}) to prevent the insert from happening.
在mongodb端,您可以使用ensureIndex({id: 1}, {unique: true})来防止插入发生。