What are potential reasons for a user's session to remain the same while their IP address changes within a short timeframe?

The potential reasons for a user's session to remain the same while their IP address changes within a short timeframe could be due to the use of a load balancer or proxy server that changes the user's IP address but maintains the same session. To solve this issue, you can use a combination of session cookies and user agent string to create a more robust session validation process.

// Set session cookie parameters
session_set_cookie_params(0, '/', '', false, true);

// Validate session using user agent string
if ($_SESSION['user_agent'] !== $_SERVER['HTTP_USER_AGENT']) {
    // Destroy session if user agent does not match
    session_unset();
    session_destroy();
    session_start();
}

// Update user agent string in session
$_SESSION['user_agent'] = $_SERVER['HTTP_USER_AGENT'];