Home  >  Article  >  Backend Development  >  PHP中的$_SERVER['PATH_INFO']

PHP中的$_SERVER['PATH_INFO']

WBOY
WBOYOriginal
2016-06-23 14:32:571467browse

PHP中的全局变量$_SERVER['PATH_INFO']是一个很有用的参数,众多的CMS系统在美化自己的URL的时候,都用到了这个参数。

对于下面这个网址:

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值。

下面是一段解析PATH_INFO的非常简单的代码:

php

if (  ! isset (  $_SERVER [ ' PATH_INFO ' ] ) ){
     $pathinfo   =   ' default ' ;
} else {
     $pathinfo   =    explode ( ' / ' ,   $_SERVER [ ' PATH_INFO ' ]);
}

if (  is_array ( $pathinfo ) && ! empty ( $pathinfo )  ){
     $page   =   $pathinfo [ 1 ];
} else {
     $page   =   ' a.php ' ;
}

require   " $page .php " ;

?>



参考资料:
1、 PHP Parse Pathinfo           
2、 CPAN PathInfo    

 

 

 

 

 

 

如果无法显示$_SERVER['PATH_INFO']
apache下需要开启 :


  Options Indexes FollowSymLinks

  AcceptPathInfo On

 

 

 

 

 

PHP中$_SERVER['PATH_INFO']作用解析

 

 

 

最近学习伪静态时看到$_SERVER['REQUEST_URI']和$_SERVER['PATH_INFO']这两个函数,我学习从来是不用即不理(顺便提醒以下各位正在开始学习的同学,这个习惯很不好),所以很长时间总也没学好。这次也一样,看到这两个之后完全不知道什么意思,网上查了半天也没看到个所以然,看代码的时候知道大概意思,决定自己动手测试功能。。。。。

说说我的测试方法及结果:

首先建了一个页面a.php。代码如下:


php

    echo   $_SERVER [ ' REQUEST_URI ' ]

?>

本地测试地址:http://localhost/a.php 得到结果:/a.php

测试地址二:http://localhost/a.php?id=123 得到结果:/a.php?id=123

测试地址三:http://localhost/a.php/123.html 得到结果:/a.php/123.html

即$_SERVER['REQUEST_URI']基本得到的是地址中/后的所有内容

将a.php代码改为如下:


php

     echo   $_SERVER [ ' PATH_INFO ' ]

?>

本地测试地址:http://localhost/a.php 得不到任何结果,很郁闷,不懂什么意思;

测试地址二:http://localhost/a.php?id=123 同样得不到任何结果,更郁闷,因为不懂,所以确实不晓得这个函数什么意思,感觉挺不舒服的。正当准备放弃继续baidu时,无意中做了下面的测试,挺幸运

测试地址三:http://localhost/a.php/123.html 得到结果/123.html

再在后面加东西,测试地址四:http://localhost/a.php/123.html?id=2222 结果为:/123.html

什么意思自己看就应该明白了。是不是还有我没测试到的作用,不得而知了。希望对各位有用。

 

 

 

 

 

 摘自:http://hi.baidu.com/laosu1983/blog/item/49ec6b095542e8386159f310.html

 

 

 

 

 

 

 

$_SERVER["PATH_INFO"] 没有结果!

 

 

 

包含由客户端提供的、跟在 真实脚本名称 之后 并且在 查询语句(query string) 之前 的路径信息,如果存在的话。例如,如果当前脚本是通过 URL http://www.example.com/php/path_info.php/some/stuff?foo=bar 被访问,那么 $_SERVER['PATH_INFO'] 将包含 / some/stuff 。
这是php手册上介绍的,如果使用重写,将上面路径中的path_info.php去掉,即路径改写成 http://www.example.com/php/some/stuff?foo=bar,$_SERVER['PATH_INFO']也是没值的。$_SERVER["PATH_INFO"]具体用来做什么,我也不是很清楚。

 

 

 摘自:http://bbs.houdunwang.com/thread-12376-1-1.html

 

 

 

 

 

 

 

 

 

 

 

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Php开发工具Next article:PHP漏洞详解