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

岁酱吖の 提交于 2019-12-23 05:43:08

问题


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


回答1:


$_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.

Edit: To make it safe against XSS attacks, use htmlspecialchars:

<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">...</form> 

Edit 2: As this $_SERVER variable has been misused so often out there in examples across the internets, don't miss reading your HTML reference: As that URI is the shortest possible relative URI, you can just leave the action attribute empty:

<form action="" method="post" >...</form>


来源:https://stackoverflow.com/questions/3446459/what-is-the-benefit-of-using-the-super-global-serverphp-self-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!