Problem
I’m using CodeIgniter to create a PHP application. All requests are sent to the main controller, index.php, by CodeIgniter. The presence of index.php in the URI, on the other hand, irritates me. http://www.example.com/faq/whatever, for example, will redirect to http://www.example.com/index.php/faq/whatever. I need a dependable mechanism for a script to figure out what its address is so it can figure out how to navigate. According to the CodeIgniter manual, I used mod rewrite.
The following is the rule:
RewriteEngine on
RewriteCond $1 !^(images|inc|favicon\.ico|index\.php|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Normally, I’d check php self, but index.php is always the case in this scenario. I can get it from REQUEST URI, PATH INFO, and other sources, but I’m not sure which is the most dependable. Does anyone know what the real difference is between PHP SELF, PATH INFO, SCRIPT NAME, and REQUEST URI (or where to look for it)? Thank you for your assistance!
Note that I had to add spaces because SO interprets the underscore as italic for some reason.
The spaces have been fixed.
Asked by Eli
Solution #1
Here are some instances of how these variables differ in practice: Example 1: When the requested url is in form, PHP SELF differs from SCRIPT NAME: http://example.com/test.php/foo/bar
[PHP_SELF] => /test.php/foo/bar
[SCRIPT_NAME] => /test.php
([PATH INFO] => /foo/bar) appears to be the only scenario where PATH INFO offers useful information.) Note that in some prior PHP versions (= 5.0? ), this was not the case.
Example 2: When a non-empty query string is submitted, REQUEST URI differs from SCRIPT NAME: http://example.com/test.php?foo=bar
[SCRIPT_NAME] => /test.php
[REQUEST_URI] => /test.php?foo=bar
Example 3: When server-side redirection is enabled (for example, mod rewrite on Apache), REQUEST URI differs from SCRIPT NAME:
http://example.com/test.php
[REQUEST_URI] => /test.php
[SCRIPT_NAME] => /test2.php
When dealing with HTTP failures with scripts, REQUEST URI differs from SCRIPT NAME. Using the directive apache 404 /404error.php ErrorDocument http://example.com/test.php
[REQUEST_URI] => /test.php
[SCRIPT_NAME] => /404error.php
Using custom error pages on an IIS server http://example.com/test.php
[SCRIPT_NAME] => /404error.php
[REQUEST_URI] => /404error.php?404;http://example.com/test.php
Answered by Odin
Solution #2
You can detect the difference by looking at the PHP documentation:
PATH INFO appears to be undocumented…
Answered by Paige Ruten
Solution #3
PATH INFO is only available when htaccess is used in the following way:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^(favicon\.ico|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
[SCRIPT_NAME] => /index.php
http://domain.com/
[PHP_SELF] => /index.php
[PATH_INFO] IS NOT AVAILABLE (fallback to REQUEST_URI in your script)
[REQUEST_URI] => /
[QUERY_STRING] =>
http://domain.com/test
[PHP_SELF] => /index.php/test
[PATH_INFO] => /test
[REQUEST_URI] => /test
[QUERY_STRING] =>
http://domain.com/test?123
[PHP_SELF] => /index.php/test
[PATH_INFO] => /test
[REQUEST_URI] => /test?123
[QUERY_STRING] => 123
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^(favicon\.ico|robots\.txt)
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
[SCRIPT_NAME] => /index.php
[PHP_SELF] => /index.php
[PATH_INFO] IS NOT AVAILABLE (fallback to REQUEST_URI in your script)
http://domain.com/
[REQUEST_URI] => /
[QUERY_STRING] =>
http://domain.com/test
[REQUEST_URI] => /test
[QUERY_STRING] => url=test
http://domain.com/test?123
[REQUEST_URI] => /test?123
[QUERY_STRING] => url=test&123
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^(favicon\.ico|robots\.txt)
RewriteRule ^(([a-z]{2})|(([a-z]{2})/)?(.*))$ index.php/$5 [NC,L,E=LANGUAGE:$2$4]
or
RewriteRule ^([a-z]{2})(/(.*))?$ $3 [NC,L,E=LANGUAGE:$1]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^(favicon\.ico|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
[SCRIPT_NAME] => /index.php
http://domain.com/
[PHP_SELF] => /index.php
[PATH_INFO] IS NOT AVAILABLE (fallback to REQUEST_URI in your script)
[REQUEST_URI] => /
[QUERY_STRING] =>
[REDIRECT_LANGUAGE] IS NOT AVAILABLE
http://domain.com/test
[PHP_SELF] => /index.php/test
[PATH_INFO] => /test
[REQUEST_URI] => /test
[QUERY_STRING] =>
[REDIRECT_LANGUAGE] =>
http://domain.com/en
[PHP_SELF] => /index.php/
[PATH_INFO] => /
[REQUEST_URI] => /en
[QUERY_STRING] =>
[REDIRECT_LANGUAGE] => en
http://domain.com/en/test
[PHP_SELF] => /index.php/test
[PATH_INFO] => /test
[REQUEST_URI] => /en/test
[REDIRECT_LANGUAGE] => en
http://domain.com/en/test?123
[PHP_SELF] => /index.php/test
[PATH_INFO] => /test
[REQUEST_URI] => /en/test?123
[QUERY_STRING] => 123
[REDIRECT_LANGUAGE] => en
Answered by Mike
Solution #4
$_SERVER[‘REQUEST URI’] = Web path, URI request $_SERVER[‘PHP SELF’] = Web path, requested file + path info $_SERVER[‘PHP SELF’] = $_SERVER[‘PHP SELF’] = $ $_SERVER[‘SCRIPT NAME’] = Web path to requested file $_SERVER[‘SCRIPT NAME’] = $_SERVER[‘SCRIPT FILENAME’] = Requested file path __FILE__ __FILE__ __FILE__ __FILE__ __FILE
Answered by Beejor
Solution #5
You might investigate the URI Class and use $this->uri->uri string ()
Returns a string containing the entire URI.
For instance, if this is your complete UR,
http://example.com/index.php/news/local/345
This is what the function would return:
/news/local/345
Alternatively, you may use the segments to dig down into specific places without having to create parsing/regex values.
Answered by Adam
Post is based on https://stackoverflow.com/questions/279966/php-self-vs-path-info-vs-script-name-vs-request-uri