PHP's $_SERVER['path_info'] does not work when there is a GET parameter. Simple solution.

Публикувано / posted 2008-03-30 в категория / in category: Web development
  

logo-bug1.gifToday, during testing of a site for a client of mine I found that for some reason $_SERVER['PATH_INFO'] is not populated although there is path_info parameters in the requested URL. Initially I suspected that fault is in my Tangra Framework for PHP but after a quick phpinfo() I found that the problem is with the PHP itselft. Examples that describe the situation:

  • With requested URI http://test.myhost/index.php/_lang-en/ everything works as expected, i.e. $_SERVER['PATH_INFO'] contains /_lang-en/.
  • With requested URI http://test.myhost/index.php?someparam=1/_lang-en/ $_SERVER['PATH_INFO'] is not set at all. The problem is that when you have GET parameter(s) in your URI, PATH_INFO for some reason is not populated.

The solution:

We have to extract "manually" path_info string. Here is simple function to do that:

function get_path_info() {
	$path_info_str = false;

	// geting everything after domain/page, e.g. param1=1/_lang=en/
	$params = substr($_SERVER['REQUEST_URI]), strlen($_SERVER['SCRIPT_NAME']));

	if ($params) {
		// looking for first slash to cut out the GET parameters
		$first_slash = strpos($params, '/');
		if ($first_slash !== false) {
			// cutting out GET parameters
			$path_info_str = substr($params, $first_slash);
		}
	}

	return $path_info_str;
}

Leave a Reply

Notify me of followup comments via e-mail. You can also subscribe without commenting.

Внимание: Моля, въведете само ПЪРВИТЕ ТРИ цифри от картинката
Important: Please enter just the first three digits from the image