donglengyuan6826 2016-02-10 01:18
浏览 46
已采纳

php5-fpm池会话文件冲突有可能吗?

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 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入