Are there alternative methods or functions in PHP to securely handle form action paths without using $_SERVER['PHP_SELF']?

Using $_SERVER['PHP_SELF'] in form action paths can introduce security vulnerabilities such as cross-site scripting (XSS) attacks. To securely handle form action paths in PHP, you can use the htmlspecialchars() function to sanitize the output and prevent any malicious code from being injected. This function will escape special characters and prevent XSS attacks.

<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
  <!-- Form fields go here -->
</form>