Is it possible that PHP running under different php5-fpm application pools could potentially (however low the probability) have session file names collide? I would like to leave the session storage path at the default location for each pool, I just wanted to make sure this wasn't a possibility.. I wasn't able to find documentation specifically addressing this concern.
1条回答 默认 最新
- douyujun0152 2016-02-13 09:21关注
Short answer: You have more of a chance of winning the lottery while being hit by 3 consecutive lighting strikes while a super nova goes off than can be seen by earth (not accounting for the time it takes the light to travel). And even if it somehow did, it would quickly be invalidated by something else (some other check failing). So it is not something you have to worry about.
Long answer: OK, so I dug into the PHP source, and first I should say, even if it is possible, the chance of it happening is literally infinitesimal. First, the random number generator is very random. So for this to happen, 2 separate processes on 2 separate CPU cores have to somehow make the exact same random session string, which involves both the persons' IP address and the microsecond time (mmmmmmm high precision clock) added into the random number. The chance of a collision happening with just those variables alone essentially make this next to impossible. But then even further, one of those processes has to create the session and verify the file does not exist, and then before it creates the file, the other process has to do the same. So it would have to happen within microseconds to nanoseconds. Unless a REALLY unlucky context switch happens, as the action of checking for the file is not even close to the action of creating the file in the code. Keep in mind, this is all done in C code, so it's fast. And disk speed is not really an issue, as file creation is essentially atomic and stored in the harddrive's L1 cache.
So here is the important function in ./ext/session/mod_files.c
/* * Create session ID. * PARAMETERS: PS_CREATE_SID_ARGS in php_session.h * RETURN VALUE: Valid session ID(zend_string *) or NULL for FAILURE. * * PS_CREATE_SID_FUNC() must check collision. i.e. Check session data if * new sid exists already. * *mod_data is guaranteed to have non-NULL value. * NOTE: Default php_session_create_id() does not check collision. If * NULL is returned, session module create new ID by using php_session_create_id(). * If php_session_create_id() fails due to invalid configuration, it raises E_ERROR. * NULL return value checks from php_session_create_id() is not required generally. */ PS_CREATE_SID_FUNC(files)
So first, it tries REALLY hard to make sure there is no collision by creating the SID and then checking for the file. If this function somehow fails 3 times in a row, and some other crazy virtually impossible circumstances happen, it might attempt to create an SID and NOT check for a similar session already existing. For this to happen, you have to have php strict mode turned on and the check w/ the file has to fail 2 more times.
I'd rather not have to go into more detail than that :-)
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
- ¥15 vscode程序一直报同样的错,如何解决?
- ¥15 关于使用unity中遇到的问题
- ¥15 开放世界如何写线性关卡的用例(类似原神)
- ¥15 关于并联谐振电磁感应加热
- ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
- ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
- ¥66 如何制作支付宝扫码跳转到发红包界面
- ¥15 pnpm 下载element-plus
- ¥15 解决编写PyDracula时遇到的问题