What are some best practices for handling $_SERVER['PHP_SELF'] and $_SERVER['SCRIPT_NAME'] in PHP scripts?

When using $_SERVER['PHP_SELF'] or $_SERVER['SCRIPT_NAME'] in PHP scripts, it is important to sanitize the input to prevent potential security vulnerabilities, such as cross-site scripting attacks. One way to do this is by using the htmlspecialchars() function to escape any HTML characters in the input.

$script_name = htmlspecialchars($_SERVER['SCRIPT_NAME']);
echo $script_name;