How can you prevent session variables from being changed after they have been set in PHP?

To prevent session variables from being changed after they have been set in PHP, you can use the session_regenerate_id() function to regenerate the session ID after setting the variables. This will invalidate the old session ID and prevent any unauthorized access or modification of the session data.

session_start();

// Set session variables
$_SESSION['username'] = 'john_doe';
$_SESSION['email'] = 'john.doe@example.com';

// Regenerate session ID to prevent unauthorized access
session_regenerate_id();