详解 $_SERVER 函数中QUERY_STRING和REQUEST_URI区别

时间:2023-08-06 13:13:14

详解 $_SERVER 函数中QUERY_STRING和REQUEST_URI区别

实例:

1,http://localhost/aaa/ (打开aaa中的index.php)
结果:
$_SERVER['QUERY_STRING'] = "";
$_SERVER['REQUEST_URI']  = "/aaa/";
$_SERVER['SCRIPT_NAME']  = "/aaa/index.php";
$_SERVER['PHP_SELF']     = "/aaa/index.php";

2,http://localhost/aaa/?p=222 (附带查询)
结果:
$_SERVER['QUERY_STRING'] = "p=222";
$_SERVER['REQUEST_URI']  = "/aaa/?p=222";
$_SERVER['SCRIPT_NAME']  = "/aaa/index.php";
$_SERVER['PHP_SELF']     = "/aaa/index.php";

3,http://localhost/aaa/index.php?p=222&q=333
结果:
$_SERVER['QUERY_STRING'] = "p=222&q=333";
$_SERVER['REQUEST_URI']  = "/aaa/index.php?p=222&q=333";
$_SERVER['SCRIPT_NAME']  = "/aaa/index.php";
$_SERVER['PHP_SELF']     = "/aaa/index.php";

由实例可知:
$_SERVER["QUERY_STRING"]  获取查询 语句,实例中可知,获取的是?后面的值
$_SERVER["REQUEST_URI"]   获取 http://localhost 后面的值,包括/
$_SERVER["SCRIPT_NAME"]   获取当前脚本的路径,如:index.php
$_SERVER["PHP_SELF"]      当前正在执行脚本的文件名

WordPress通过301重定向实现非首先域(非www)跳转向本来是一个很简单事情,由于没弄清楚$_SERVER['HTTP_X_REWRITE_URL'] 和$_SERVER['REQUEST_URI']的区别导致了循环重定向问题。
现在来具体研究下$_SERVER['HTTP_X_REWRITE_URL'] 和$_SERVER['REQUEST_URI']的区别
首先要纠正一部分的观点就是IIS+PHP不支持$_SERVER['REQUEST_URI']
不是,不支持是PHP Version 版本的问题,php4.4.0不支持 $_SERVER['REQUEST_URI'],php5.2.5则增加了支持;

$_SERVER['REQUEST_URI']常用来获当前URL
例如:http://www.hujuntao.com/index.php?p=3
$_SERVER['REQUEST_URI']获得的就是/index.php?p=3这部分

$_SERVER["HTTP_X_REWRITE_URL"]则在IIS下获得的是当前URL,在apache下的值为空

例如:
当前URL:http://www.hujuntao.com/
IIS环境下:
$_SERVER['REQUEST_URI'] = /index.php
$_SERVER["HTTP_X_REWRITE_URL"] =/
apache环境下:
$_SERVER['REQUEST_URI'] = /
$_SERVER["HTTP_X_REWRITE_URL"] =”

当前URL:http://www.hujuntao.com/index.php
IIS环境下:
$_SERVER['REQUEST_URI'] = /index.php
$_SERVER["HTTP_X_REWRITE_URL"] =/index.php
apache环境下:
$_SERVER['REQUEST_URI'] = /index.php
$_SERVER["HTTP_X_REWRITE_URL"] =”