How can PHP_SELF be correctly used in a form action attribute to prevent access permission errors in PHP scripts?

When using PHP_SELF in a form action attribute, it can expose your script to potential security vulnerabilities like cross-site scripting attacks. To prevent this, it's recommended to use htmlspecialchars() function to sanitize the input and prevent any malicious code from being executed.

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