PHP_SELF,SCRIPT_NAME,REQUEST_URI区别
$_SERVER['PHP_SELF']、$_SERVER['SCRIPT_NAME'] 与 $_SERVER['REQUEST_URI'] 三者非常相似,返回的都是与当前 URL 或 PHP 程序文件相关的信息:
- $_SERVER['PHP_SELF']:相对于网站根目录的路径及 PHP 程序名称。
- $_SERVER['SCRIPT_NAME']:相对于网站根目录的路径及 PHP 程序文件名称。
- $_SERVER['REQUEST_URI']:访问此页面所需的 URI 。
DOCUMENT_ROOT:即apache配置文件中指定的DocumentRoot (E:\www\)
SCRIPT_NAME:从DocumentRoot(根目录)到本文件的路径+当前文件名(\test\test.php)
PHP_SELF:从DocumentRoot(根目录)到本文件的路径+当前文件名(\test\test.php)//不常用
__FILE__:从磁盘开始到本文件的绝对路径(E:\www\test\test.php)
--SCRIPT_NAME与__FILE__不同 是指当前执行的文件
而__FILE__是指当前文件(如a.php中有__FILE__,但被b.php
include且执行b.php,此时__FILE__为a.php, SCRIPT_NAME为b.php)
实测
$_SERVER['SCRIPT_NAME']:主要到.后缀(只保留到.php)就结束
http://www.yoursite.com/example/index.php — – — /example/index.php
http://www.yoursite.com/example/index.php — – — /example/index.php
http://www.yoursite.com/example/index.php/dir/test — – — /example/index.php
在所有的返回中都是当前的文件名/example/index.php
$_SERVER['PHP_SELF']:取问号之前(.php/abc?abc则到/abc结束)----从DocumentRoot(根目录)到本文件的路径
http://www.yoursite.com/example/index.php — – — /example/index.php
http://www.yoursite.com/example/index.php?a=test — – — /example/index.php
http://www.yoursite.com/example/index.php/dir/test — – — /dir/test
QUERY_STRING:取问号之后(.php/abc?abc则为abc)
$_SERVER['REQUEST_URI':各种带
http://www.yoursite.com/example/index.php — – — /example/index.php
http://www.yoursite.com/example/index.php?a=test — – — /example/index.php?a=test
http://www.yoursite.com/example/index.php/dir/test — – — /example/index.php/dir/test
PATH_INFO:保留.php后到问号之前的/abc/bcd格式的字符串
HTTP_HOST:域名(只含域名 不含http等杂物)
以上都是基于DOCUMENT_ROOT