Click here to Skip to main content
15,912,578 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
You have a page called MyPage.php containing a form that is handled by the same page.
What is the difference when you simply define action like
HTML
action="MyPage.php"

and when you do it like
HTML
action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>"

and Which one is more secure?
Posted

1 solution

The advantage of using PHP_SELF is that if you rename your page, your action attribute also changes.

About the security: with htmlspecialchars or htmlentities, they have the same security. But if you don't escape HTML characters with PHP_SELF, there are some exploits[^].

Personally, I suggest using $_SERVER["SCRIPT_NAME"]. If you update your filename, then the action attribute will update to, and with SCRIPT_NAME, you don't have the risk of the exploits with PHP_SELF.
 
Share this answer
 
Comments
Maciej Los 16-Feb-15 12:32pm    
Nicely explained ;)
Thomas Daniels 16-Feb-15 12:51pm    
Thank you!
cs101000 16-Feb-15 12:33pm    
Do $_SERVER["SCRIPT_NAME"] and $_SERVER["PHP_SELF"] have the same return value? If yes why the latter is vulnerable to exploits?
Thomas Daniels 16-Feb-15 12:51pm    
PHP_SELF is vulnerable in case someone sends this link to someone else:
http://yoursite/MyPage.php/"><b>test</b>
In that case, people who go to the link get to see a bold piece of text. That's pretty innocent, but it can also be used for script-tags. If you use SCRIPT_NAME, the action attribute will only consist of MyPage.php, not the other things.
cs101000 16-Feb-15 13:12pm    
Thank you for the answer! So I don't need htmlspecialchars or htmlentities for $_SERVER["SCRIPT_NAME"] then, right?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900