201408220150PHP _SERVER相關變數檔案名稱的差異
PHP _SERVER相關變數檔案名稱的差異
PHP $_SERVER中可以取得PHP檔案名稱的變數有:$_SERVER['PHP_SELF']、$_SERVER['SCRIPT_NAME']、$_SERVER["REQUEST_URI"] 等,其中這三個變數雖然都是根據URL來決定值的,但接收不同的URL會有不同的結果
PHP 不是以 CGI 方式執行,$_SERVER 變數在不同URL結果的差異
URL |
$_SERVER['PHP_SELF'] | $_SERVER['REQUEST_URI'] | $_SERVER['SCRIPT_NAME'] |
http://localhost/example/ | /example/index.php | / | /example/index.php |
http://localhost/example/index.php | /example/index.php | /example/index.php | /example/index.php |
http://localhost/example/index.php?a=test | /example/index.php | /example/index.php?a=test | /example/index.php |
http://localhost/example/index.php/dir/test | /dir/test | /example/index.php/dir/test | /example/index.php |
一、PHP中支援 INCLUDE/INCLUDE_ONCE/REQUEST/REQUEST_ONCE 功能,傳入的URL並不一定是實際執行的PHP SCRIPT,因此如果想取得目前真正被執行PHP SCRIPT檔案名稱應該使用 __FILE__ 這個變數。
二、$_SERVER['PHP_SELF']有跨站腳本攻擊的問題,不應直接顯示在網頁上,改以下方式
1.以 htmlentities($_SERVER['PHP_SELF']) 取代 $_SERVER['PHP_SELF']。
2.以 $_SERVER['REQUEST_URI']來替代$_SERVER['PHP_SELF']。
回應