How can the session_regenerate_id() function be used to enhance session security in PHP applications?

Session fixation is a security vulnerability where an attacker can set the session ID of a user before they log in, potentially allowing them to hijack the user's session. The session_regenerate_id() function in PHP can be used to enhance session security by generating a new session ID for the user after they log in, making it harder for an attacker to predict or steal the session ID.

// Start the session
session_start();

// Log the user in
// Check username and password, set session variables, etc.

// Regenerate the session ID
session_regenerate_id(true);