I got most of them code easily but in the first line they used <?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>
for passing data to self php file. But if there is a matter just to send data to self php file so why they used echo before the htmlspecialchars I couldn’t get it clearly?
The reason for using htmlspecialchars there is to prevent the possibility of a Cross Site Scripting (XSS) attack, as $_SERVER[“PHP_SELF”] can be manipulated by the user. See this article for more detail.
Plus, even if we were confident that the URL value was safe (which we’re not), we would still want to escape in order to avoid any special characters being misinterpreted. For example:
<a href="http://something.com/?test<">test</a>
Browsers will interpret this as:
http://something.com/?test<
Because – and I hope this is obvious – ampersand is a special character in HTML. So yes, using htmlspecialchars makes sense, and you should definitely do it.
The only reason you would ever not want to use htmlspecialchars is if the thing you’re outputting is already pre-rendered, pre-escaped HTML.
That would probably avoid injection issues, but you should still escape nonetheless. Even file names can contain characters that are special to HTML. Ampersands, quotes, semicolons are all possible. You’re creating a lot of edge case bugs by avoiding htmlspecialchars and you’re not gaining anything for it.
Only if you insist on naming them that way. You avoid that issue if you only use letters and numbers in your file names (plus the dot before the extension).
Why create a problem where you need to escape filenames in HTML by using characters in those filenames that causes the issue in the first place.