Home > php教程 > php手册 > body text

PHP中PathInfo参数的使用

WBOY
Release: 2016-06-13 11:38:05
Original
1523 people have browsed it

  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的非常简单的代码:

以下为引用的内容:

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

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

require "$page.php";

//帮客之家 LieHuo.NeT
?>

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!