本文共 2727 字,大约阅读时间需要 9 分钟。
$_SERVER[PHP_SELF], $_SERVER[SCRIPT_NAME], $_SERVER['REQUEST_URI'] 在用法上是非常相似的,他们返回的都是与当前正在使用的页面地址有关的信息,这里列出一些相关的例子,帮助确定哪些是在你的脚本最适合的。
$_SERVER[’PHP_SELF’]
当我们使用$_SERVER['PHP_SELF']的时候,无论访问的URL地址是否有index.php,它都会自动的返回 index.php.但是如果在文件名后面再加斜线的话,就会把后面所有的内容都返回在$_SERVER['PHP_SELF']。
$_SERVER['REQUEST_URI']
$_SERVER['REQUEST_URI']返回的是我们在URL里写的精确的地址,如果URL只写到”/”,就返回 “/”
$_SERVER['SCRIPT_NAME']
在所有的返回中都是当前的文件名/example/index.php
http://www.cnblogs.com/xiaochaohuashengmi/archive/2011/09/13/2175071.html
对于下面这个网址:
http://www.test.com/index.php/foo/bar.html?c=index&m=search
我们可以得到 $_SERVER['PATH_INFO'] = ‘/foo/bar.html’,而此时 $_SERVER['QUERY_STRING'] = 'c=index&m=search';
通 常,我们最初开始PHP程序编写的时候,都会使用诸如: http://www.test.com/index.php?c=search&m=main 这样的URL,这种URL不仅看起来非常奇怪,而且对于搜索引擎也是非常不友好的。很多搜索引擎收录的时候,都会忽略Query String之后的内容,google虽然不会忽略Query String,但是对于其他不含Query String的页面,会给于比较高的PR值。
示例说明
location /newapp/ {
root /home/users/pengzhi/odp/webroot; index mp.php; fastcgi_pass $php_upstream; #fastcgi_index mp.php;# 已经排除请求fast_cig的url是 / 则添加index.php的情况 include fastcgi.conf; rewrite ^/newapp(/[^\?]*)?((\?.*)?)$ /newapp/index.php$1$2 break; #跳出只处理 server块内容 即使其他location更加匹配重写后的uri #last 会重新处理这个server 不会出来后面的 } http://xxxxxx:8125/newapp/Main/index3 "SCRIPT_FILENAME": "/home/users/pengzhi/odp/webroot/newapp/index.php", "PATH_INFO": "/Main/index3",h "SCRIPT_NAME": "/newapp/index.php", "REQUEST_URI": "/newapp/Main/index3", "DOCUMENT_URI": "/newapp/index.php/Main/index3", "DOCUMENT_ROOT": "/home/users/pengzhi/odp/webroot", "ORIG_PATH_INFO": "/newapp/index.php/Main/index3", "ORIG_SCRIPT_NAME": "/newapp/index.php/Main/index3", "ORIG_SCRIPT_FILENAME": "/home/users/pengzhi/odp/webroot/newapp/index.php/Main/index3", "PATH_TRANSLATED": "/home/users/pengzhi/odp/webroot/Main/index3", "PHP_SELF": "/newapp/index.php/Main/index3",总结一下 SCRIPT_NAME + PATH_INFO = PHP_SELF