$ _ SERVER ['path _ info'] in PHP

Source: Internet
Author: User
$ _ SERVER [PATH_INFO] global variable $ _ SERVER ['path _ info'] in PHP is a useful parameter, many CMS systems use this parameter when beautifying their URLs.

For the following URL:

Http://www.test.com/index.php/foo/bar.html? C = index & m = search

We can get $ _ SERVER ['path _ info'] = '/foo/bar.html ', in this case, $ _ SERVER ['query _ string'] = 'C = index & m = search ';

Usually, when we first started writing PHP programs, we would use http://www.test.com/index.php? C = search & m = main. this kind of URL is not only strange, but also unfriendly to search engines. When indexed by many search engines, the content after Query String is ignored. Although google does not ignore Query String, other pages without Query String are given a relatively high PR value.

The following is a simple code for parsing PATH_INFO:


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 ";

?>



References:
1. PHP Parse Pathinfo
2. CPAN PathInfo

If $ _ SERVER ['path _ info'] cannot be displayed,
Enable apache:


Options Indexes FollowSymLinks

AcceptPathInfo On

Analysis of $ _ SERVER ['path _ info'] in PHP

When I recently learned about pseudo-static data, I saw the $ _ SERVER ['request _ url'] and $ _ SERVER ['path _ info'] functions, I never need to ignore my study (by the way, I would like to remind the following students who are learning this habit), so I have never learned it well for a long time. The same is true this time. I don't know what it means after seeing the two. I haven't seen any reason after checking them online for a long time. I know what it means when I look at the code, decide to manually test the function .....

Let's talk about my testing methods and results:

First, a page a. php is created. The code is as follows:



Echo $ _ SERVER ['request_uri ']

?>

Local test address: http: // localhost/a. php. expected result:/a. php.

Test address 2: http: // localhost/a. php? Id = 123. expected result:/a. php? Id = 123

Test URL 3: http: // localhost/a. php/123.html. expected result:/a. php/123.html.

That is, $ _ SERVER ['request _ URI '] basically obtains all the content in/after the address.

Change the. php code as follows:



Echo $ _ SERVER ['path_info ']

?>

Local test address: http: // localhost/a. php cannot get any results. it is very depressing and doesn't understand what it means;

Test address 2: http: // localhost/a. php? Id = 123 does not get any results, and is even more depressing. because I don't understand it, I really don't know what this function means, and I feel quite uncomfortable. Just as I was about to give up on baidu, I accidentally did the following test, so I am lucky.

Test URL 3: http: // localhost/a. php/123.html. expected result/123.html is displayed.

Add the following content. test address 4: http: // localhost/a. php/123.html? The result of id = 2222 is/123.html.

You should understand what it means. Is there any other function that I have not tested? I don't know. I hope it will be useful to you.

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


$ _ SERVER ["PATH_INFO"] no results!

Contains the path information provided by the client following the real script name and before the query statement (query string), if any. For example, if the current script is through a URL http://www.example.com/php/path_info.php/some/stuff? If foo = bar is accessed, $ _ SERVER ['path _ info'] will contain/some/stuff.
This is introduced in the php manual, if you use rewrite, the above path in path_info.php removed, that is, the path to rewrite to http://www.example.com/php/some/stuff? Foo = bar, $ _ SERVER ['path _ info'] also has no value. $ _ SERVER ["PATH_INFO"] I am not very clear about what to do.

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.