세션 처리 session_regenerate_id

(PHP 4 >= 4.3.2, PHP 5, PHP 7, PHP 8)

session_regenerate_id — 새로 생성된 세션 ID로 현재 세션 ID 업데이트


설명

session_regenerate_id(bool $delete_old_session = false): bool

session_regenerate_id()는 현재 세션 ID를 새 ID로 바꾸고 현재 세션 정보를 유지합니다.

session.use_trans_sid가 활성화되면 session_regenerate_id() 호출 후에 출력을 시작해야 합니다. 그렇지 않으면 이전 세션 ID가 사용됩니다.

경고 현재 session_regenerate_id는 불안정한 네트워크를 잘 처리하지 못합니다. 모바일 및 WiFi 네트워크. 따라서 session_regenerate_id를 호출하여 세션이 손실될 수 있습니다.

이전 세션 데이터를 즉시 폐기해서는 안 되지만 폐기 타임스탬프를 사용하고 이전 세션 ID에 대한 액세스를 제어해야 합니다. 그렇지 않으면 페이지에 대한 동시 액세스로 인해 일관성이 없는 상태가 발생하거나 세션이 손실될 수 있으며 클라이언트(브라우저) 측 경쟁 조건이 발생하고 불필요하게 많은 세션 ID가 생성될 수 있습니다. 즉각적인 세션 데이터 삭제는 세션 하이재킹 공격 탐지 및 방지도 비활성화합니다.


매개변수

delete_old_session
이전 관련 세션 파일을 삭제할지 여부입니다. 삭제로 인한 경합을 피하거나 세션 하이재킹 공격을 탐지/회피해야 하는 경우 이전 세션을 삭제하면 안 됩니다.

반환 값

성공하면 true를, 실패하면 false를 반환합니다.


Examples

예제 #1 session_regenerate_id() 예제

                  
<?php
// NOTE: This code is not fully working code, but an example!

session_start();

// Check destroyed time-stamp
if (isset($_SESSION['destroyed'])
    && $_SESSION['destroyed'] < time() - 300) {
    // Should not happen usually. This could be attack or due to unstable network.
    // Remove all authentication status of this users session.
    remove_all_authentication_flag_from_active_sessions($_SESSION['userid']);
    throw(new DestroyedSessionAccessException);
}

$old_sessionid = session_id();

// Set destroyed timestamp
$_SESSION['destroyed'] = time(); // 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<br />";
echo "New Session: $new_sessionid<br />";

print_r($_SESSION);
?>
                  
                

현재 세션 모듈은 불안정한 네트워크를 잘 처리하지 못합니다. session_regenerate_id로 세션이 손실되지 않도록 세션 ID를 관리해야 합니다.

예제 #2 session_regenerate_id()로 세션 손실 방지

                  
<?php
// NOTE: This code is not fully working code, but an example!
// my_session_start() and my_session_regenerate_id() avoid lost sessions by
// unstable network. In addition, this code may prevent exploiting stolen
// session by attackers.

function my_session_start() {
    session_start();
    if (isset($_SESSION['destroyed'])) {
       if ($_SESSION['destroyed'] < time()-300) {
           // Should not happen usually. This could be attack or due to unstable network.
           // Remove all authentication status of this users session.
           remove_all_authentication_flag_from_active_sessions($_SESSION['userid']);
           throw(new DestroyedSessionAccessException);
       }
       if (isset($_SESSION['new_session_id'])) {
           // Not fully expired yet. Could be lost cookie by unstable network.
           // Try again to set proper session ID cookie.
           // NOTE: Do not try to set session ID again if you would like to remove
           // authentication flag.
           session_commit();
           session_id($_SESSION['new_session_id']);
           // New session ID should exist
           session_start();
           return;
       }
   }
}

function my_session_regenerate_id() {
    // New session ID is required to set proper session ID
    // when session ID is not set due to unstable network.
    $new_session_id = session_create_id();
    $_SESSION['new_session_id'] = $new_session_id;

    // Set destroy timestamp
    $_SESSION['destroyed'] = time();

    // Write and close current session;
    session_commit();

    // Start session with new session ID
    session_id($new_session_id);
    ini_set('session.use_strict_mode', 0);
    session_start();
    ini_set('session.use_strict_mode', 1);

    // New session does not need them
    unset($_SESSION['destroyed']);
    unset($_SESSION['new_session_id']);
}
?>
                  
                

기타