session_regenerate_id() vs session_id(randomString)

后端 未结 3 2191
说谎
说谎 2021-01-18 23:01

What is the distinct difference between session_id($randomString) and session_regenerate_id()? Both seem to change session id:

3条回答
  •  我在风中等你
    2021-01-18 23:03

    OK, so I did some testing to find the differences in the three different options (session_id($id) after session_start(), session_regenerate_id() and session_regenerate_id(true)). This is the result of what actually happens:


    session_id($id) after session_start

    Calling the session id function after session_start will change the session id. At the end of the page load, the current session contents will write a new session file. This will leave the old session file as well and it won't be updated with any changes. However, session_id doesn't send out a new session cookie. This is done by session_start, even when session_id is called before session_start. On the next page load, the old session id is passed and loaded with the same data as the start of the last page load (new session changes would have been saved to the new id).


    session_regenerate_id() and session_regenerate_id(true)

    session_regenerate_id() will create and change the session id, transferring the session to the new file and send out the cookie. Passing true as an argument will also delete the old session file, omitting the argument will leave it.


    As far as session fixation, both session_id($id) and session_regenerate_id() would actually be worse as you are creating new sessions while leaving the old session files around to be hijacked. The only option that might help with fixation would be to call session_regenerate_id(true) passing the argument.

提交回复
热议问题