What exactly is PATH_INFO in PHP?
Actually, PATH_INFO
is related to the Apache Web Server serving PHP pages and not PHP per se.
PATH_INFO
is an environment variable set by Apache when the AcceptPathInfo
directive is turned on. It will contain trailing pathname information that follows an actual filename or non-existent file in an existing directory, whether the request is accepted or rejected. Environment variables are then passed on to the Apache/CGI module in charge of rendering the page.
The variable is accessible in PHP using $_SERVER['PATH_INFO']
.
For example, assume the location /test/
points to a directory that contains only the single file here.html
. Then requests for /test/here.html/more
and /test/nothere.html/more
both collect /more
as PATH_INFO
.
Apache Core Documentation:
AcceptPathInfo
Directive
What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']?
The PATH_INFO
variable is only present if you invoke a PHP script like this:
http://www.example.com/phpinfo.php/HELLO_THERE
It's only the /HELLO_THERE
part after the .php
script. If you don't invoke the URL like that, there won't be a $_SERVER["PATH_INFO"]
environment variable.
The PORIG_
prefix is somewhat uncommon. PATH_INFO
is a standard CGI-environment variable, and should never be prefixed. Where did you read that? (There were some issues around PHP3/PHP4 if you invoked the PHP interpreter via cgi-bin/ - but hardly anyone has such setups today.)
For reference: http://www.ietf.org/rfc/rfc3875
Check in PHP if PATH_INFO is enabled on your server?
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.
PATH_INFO in PHP without having file apparent
When a webpage is requested from a server. The server looks at the path (i.e. example.com/this/is/a/path) to figure out which file to serve and from where.
As I understand it, what you want to do is have your index.php handle all the requests. The way you would do this to use URL Rewriting.
Assuming you are using an apache web server, you can use something called *mod_rewrite* to do this. See more on mod_rewrite here.
For the specific rules to use, you probably want to use something like the code in V_K's answer.
Portable and safe way to get PATH_INFO
Well, I'm (almost) sure that without making use of the $_SERVER
superglobal keys, providing a alternative way to figure out PATH_INFO
is just impossible, that being said lets first list all of the $_SERVER keys that we may possibly use:
- 'PHP_SELF'
- 'QUERY_STRING'
- 'SCRIPT_FILENAME'
- 'PATH_TRANSLATED'
- 'SCRIPT_NAME'
- 'REQUEST_URI'
- 'PATH_INFO'
- 'ORIG_PATH_INFO'
We obviously need to ignore the last two. Now we should (I don't know this for a fact, I'm just assuming because you said so) filter all the keys that exist in the link you provided (which BTW is offline ATM), that leaves us with the following keys:
- 'PHP_SELF'
- 'SCRIPT_FILENAME'
- 'REQUEST_URI'
Regarding your comment to Anthonys answer:
You are just juggling variables now.
SCRIPT_FILENAME
is a part of the CGI
spec. It will not be available if
PATH_INFO
is unavailable. As for
REQUEST_URI
, it's apache's mod_rewrite
specific. – LiraNuna
I'm running LightTPD/1.4.20-1 (Win32) with PHP 5.3.0 as CGI, cgi.fix_pathinfo = 1
and $_SERVER['REQUEST_URI']
is very available to me, I also remember using that same variable back in the days when no one used mod_rewrite
so my honest humble guess is that you're plain wrong in this point. Regarding the SCRIPT_FILENAME
key I'm unable to test that one out ATM. Still, if we close our eyes really hard and believe that you're right that leaves us with only one variable:
- 'PHP_SELF'
I'm not trying in being harsh here (and I still believe that there are more solutions) but if PHP_SELF
is the only key you want us to work with (assuming there are no impositions on PHP_SELF
itself) there is only one solution left:
function PATH_INFO()
{
if (array_key_exists('PATH_INFO', $_SERVER) === true)
{
return $_SERVER['PATH_INFO'];
}
$whatToUse = basename(__FILE__); // see below
return substr($_SERVER['PHP_SELF'], strpos($_SERVER['PHP_SELF'], $whatToUse) + strlen($whatToUse));
}
This function should work, however there may be some problems using the __FILE__
constant since it returns the path to the file where the __FILE__
constant is declared and not the path to the requested PHP script, so that's why the $whatToUse is there for: sou you can replace it with 'SCRIPT_FILENAME'
or if you really believe in what you are saying, just use '.php'
.
You should also read this regarding why not to use PHP_SELF
.
If this doesn't work for you, I'm sorry but I can think of anything else.
EDIT - Some more reading for you:
- Drupal request_uri() (why do they keep saying
REQUEST_URI
is Apache specific?) PHP_SELF
vsPATH_INFO
vsSCRIPT_NAME
vsREQUEST_URI
PHP_SELF vs PATH_INFO vs SCRIPT_NAME vs REQUEST_URI
The PHP documentation can tell you the difference:
'PHP_SELF'
The filename of the currently executing script, relative to the document root. For instance, $_SERVER['PHP_SELF'] in a script at the address http://example.com/test.php/foo.bar would be /test.php/foo.bar. The __FILE__ constant contains the full path and filename of the current (i.e. included) file. If PHP is running as a command-line processor this variable contains the script name since PHP 4.3.0. Previously it was not available.
'SCRIPT_NAME'
Contains the current script's path. This is useful for pages which need to point to themselves. The __FILE__ constant contains the full path and filename of the current (i.e. included) file.
'REQUEST_URI'
The URI which was given in order to access this page; for instance, '/index.html'.
PATH_INFO doesn't seem to be documented...
$_SERVER['PATH_INFO'] on localhost
If your url looks like this http://localhost/
then $_SERVER['PATH_INFO']
is not set.
Related Topics
How to Get Enum Possible Values in a MySQL Database
Laravel Update Model With Unique Validation Rule For Attribute
PHP 5 Strpos() Difference Between Returning 0 and False
Upload Multiple Files With PHP and Jquery
Restructure Multidimensional Array of Column Data into Multidimensional Array of Row Data
How to Know Which 'PHP.Ini' File Is Used
How to Best Configure PHP to Handle a Utf-8 Website
How to Insert an Item At the Beginning of an Array in PHP
Sort and Display Directory List Alphabetically Using Opendir() in PHP
Detect Exact Os Version from Browser
PHP, MySQL Error: Column Count Doesn't Match Value Count At Row 1
Remove All Elements from Array That Do Not Start With a Certain String
How Safe Are Pdo Prepared Statements
"Vertical" Regex Matching in an Ascii "Image"