Are there any best practices for managing PHP session IDs?

When managing PHP session IDs, it is important to ensure they are secure to prevent session hijacking attacks. One best practice is to regenerate the session ID periodically to mitigate the risk of session fixation attacks. Additionally, storing session IDs in secure HTTP cookies can help protect them from being accessed by malicious actors.

// Start or resume a session
session_start();

// Regenerate session ID periodically
if (isset($_SESSION['last_regenerated']) && $_SESSION['last_regenerated'] < (time() - 300)) {
    session_regenerate_id(true);
    $_SESSION['last_regenerated'] = time();
}

// Store session ID in a secure HTTP cookie
session_set_cookie_params([
    'httponly' => true,
    'samesite' => 'Strict'
]);