2

From PHP, is there a cross-platform, cross-web server way of determining if PATH_INFO is enabled on the server you are running on?

It appears $_SERVER['PATH_INFO'] is only populated if there are extra path segments after the script, so you can't reliably tell if PATH_INFO is enabled if the request is for /index.php, for example.

Brad Bell
  • 368
  • 1
  • 11
  • 27

2 Answers2

2

I don't think there is a defined way to get hold of an Apache configuration value like that.

One idea that comes to mind is making a request using file_get_contents() to

http://current_site_domain/check.php/test

check.php would output $_SERVER['PATH_INFO'].

If the result of the request is "test", PATH_INFO works.

Of course, this might fail because opening URLs is disabled, because you don't know the local domain, because there's a firewall in place, etc. etc.

Another way that is less prone to failing is using an iframe:

<iframe src="/check.php/It%20works!"></iframe>

If you see "it works" inside the ifrane, PATH_INFO works. Possibly useful for an installation procedure.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • fiel_get_contents() seems to fail with either a 404 or 500 if PATH_INFO is disabled and you append an extra segment after check.php. – Brad Bell Jan 03 '12 at 18:37
  • And it might just be my setup, but I can't seem to catch the error either, or I would just use that as a sign it isn't enabled. – Brad Bell Jan 03 '12 at 19:07
  • Adding an @ to file_get_contents suppresses the error and the result is false, so for now I'm going with this. – Brad Bell Jan 03 '12 at 20:23
0

$_SERVER["PATH_INFO"] should always be available. If it's not, it just means there was no extra information between the file name and the query string.

benesch
  • 5,239
  • 1
  • 22
  • 36
  • Exactly. "A request will only be accepted if it maps to a literal path that exists. Therefore a request with trailing pathname information after the true filename such as /test/here.html/more in the above example will return a 404 NOT FOUND error." The script won't execute if AcceptPathInfo is off. (Right?) – benesch Jan 03 '12 at 01:04
  • True and good point, but the OP's question is whether they can *detect* `AcceptPathInfo on`, isn't it? Presumably for some installation procedure or something. – Pekka Jan 03 '12 at 01:06
  • Though your solution might be better, actually. Who knows how lighttpd and other servers handle path info? – benesch Jan 03 '12 at 01:07
  • @Pekka, yup, you're right. This question is more complex than it looks. – benesch Jan 03 '12 at 01:09
  • Yeah an installation procedure is one use case. Generating URLs is another. If the url is 'path/to/file', should that be 'script.php/path/to/file' or 'script.php?path=path/to/file'. – Brad Bell Jan 03 '12 at 02:00
  • Why not just use mod_rewrite to rewrite URLs appropriately? I'll post some code in a few minutes. – benesch Jan 03 '12 at 02:19
  • Well, actually, it depends on your use case. Do you want *every* URL (besides images/actual files) to be processed by script.php? Or is this just one PHP script among many on your site where you want "nicer-looking" parameter passing? – benesch Jan 03 '12 at 06:14
  • Most every URL will go through script.php. I don't think I can be guaranteed that mod_rewrite will be installed or enabled (app has to run in a shared hosting environment). – Brad Bell Jan 03 '12 at 18:39
  • Most hosts, even shared, will allow mod_rewrite. Use Pekka's method for detection, I suppose. – benesch Jan 03 '12 at 23:14
  • Not it is [not always true](https://stackoverflow.com/questions/1884041/portable-and-safe-way-to-get-path-info). Because in some configurations it does not exist! In my case I am using OS wtih apache 2.4 and it does not exist. Even if I add to **vhost.conf** `...AcceptPathInfo On..`. So please be careful to use it in your project. I would suggest to use this: `function getPath() { $path = $_SERVER['REQUEST_URI'] ?? '/'; $position = strpos($path, '?'); if($position === false) { return $path; } return substr($path, 0, $position); }` – Utmost Creator Jan 30 '21 at 16:03