Are there best practices for utilizing global variables in PHP to store user referral information?

When storing user referral information in global variables in PHP, it is important to ensure that the data is sanitized to prevent security vulnerabilities. One best practice is to store the referral information in a session variable instead of a global variable to maintain user-specific data securely. Additionally, using PHP functions like htmlentities or htmlspecialchars can help prevent cross-site scripting attacks.

// Start the session
session_start();

// Store the user referral information in a session variable
$_SESSION['referral_code'] = htmlentities($_GET['referral_code']);