What is the difference between session_create_id and session_regenerate_id()?

前端 未结 2 1135
清酒与你
清酒与你 2021-01-22 09:41

Doc says

session_create_id() is used to create new session id for the current session. session_regenerate_id() Update the current session id wi

2条回答
  •  庸人自扰
    2021-01-22 09:45

    session_create_id

    Create new session id

    session_regenerate_id

    Update the current session id with a newly generated one

    Usage example from the manual:

    $old_sessionid = session_id();
    
    // Set destroyed timestamp
    $_SESSION['destroyed'] = time(); // Since PHP 7.0.0 and up, session_regenerate_id() saves old session data
    
    // Simply calling session_regenerate_id() may result in lost session, etc.
    // See next example.
    session_regenerate_id();
    
    // New session does not need destroyed timestamp
    unset($_SESSION['destroyed']);
    
    $new_sessionid = session_id();
    
    echo "Old Session: $old_sessionid
    "; echo "New Session: $new_sessionid
    "; print_r($_SESSION);

    Which leads us to the following question - why and when you should use it, there's a detailed answer in this link.

提交回复
热议问题