views:

88

answers:

1

What is the benefit of using the super global $_SERVER['PHP_SELF']?

+11  A: 

$_SERVER['PHP_SELF'] doesn't (or shouldn't) include the domain name. It includes the path component of the url that the script was called from.

Its use is primarily to introduce cross site scripting vulnerabilities.

you can use it to fill in the action attribute of a form tag:

<form method="post" action="<?=$_SERVER['PHP_SELF']?>"></form> 

If I then call your page with:

your-file-that-uses-php-self.php/("><script>eval-javascript-here</script>)

where everything in parens is urlencoded then I can inject the code into your page. If I send that link to somebody else, then I'm executing that code in their browser from your site.

aaronasterling
I love that part *Its use is largely to introduce cross site scripting vulnerabilities*... Have my last upvote for today!
alex
This is part of why I switched to .NET for web development. :)
Jake Petroules
Clearly .Net is fool proof.
Justin Johnson
That’s why you should use proper encoding for your output.
Gumbo
@alex thanks. @Jake I totally understand. PHP makes it super easy to shoot yourself in the foot. It is possible to secure a PHP application as long as you stick to the basics though (e.g. don't trust anything from the user). The problem is that you need to know the system well enough to know what comes from the user.
aaronasterling