How can PHP_SELF be replaced with $_SERVER to avoid security vulnerabilities in PHP scripts?

Using $_SERVER['PHP_SELF'] in PHP scripts can lead to security vulnerabilities such as XSS attacks. To avoid this, it is recommended to replace $_SERVER['PHP_SELF'] with $_SERVER['SCRIPT_NAME'] or $_SERVER['REQUEST_URI']. This helps to prevent malicious users from injecting harmful code into the script.

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