views:

3194

answers:

5

Hi All,

I am building a PHP app in CodeIgniter. CI sends all requests to the main controller, /index.php.

However, I don't like to see the index.php in the URL, so I've used mod_rewrite, as per the CI documentation.

The rule is as follows:

RewriteEngine on
RewriteCond $1 !^(images|inc|favicon\.ico|index\.php|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

So for example,

http://www.example.com/faq/whatever

will route to

http://www.example.com/index.php/faq/whatever

I need a reliable way for a script to know what it's address is, so it will know what to do with the navigation.

Normally, I would just check php_self, but in this case it's always index.php. I can get it from REQUEST_URI, PATH_INFO, etc, but trying to decide which will be most reliable.

Does anyone know, or know where to find, the real difference between PHP_SELF, PATH_INFO, SCRIPT_NAME, and REQUEST_URI?

Thanks!

+1  A: 

Personally I use the $REQUEST_URI as it references the URI entered and not the location on the server's disc.

Xenph Yan
Is it always the complete URI?
Eli
Typically, you can run into issues with apache on windows, but it's only for URI's that don't resolve.
Xenph Yan
+1  A: 

You may want to look into the URI Class and make use of $this->uri->uri_string()

Returns a string with the complete URI.

For example, if this is your full URL:

http://example.com/index.php/news/local/345

The function would return this:

/news/local/345

Or you could make use of the segments to drill down specific areas without having to come up with parsing/regex values

Adam
Thank you - this is a good idea, but I am using these in a pre-system hook that will need to run before the controller is up and running.
Eli
+6  A: 

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...

yjerem
+9  A: 

Some practical examples of the differences between theese variables:
Example 1. PHP_SELF is different from SCRIPT_NAME only when requested url is in form:
http://example.com/test.php/foo/bar

[PHP_SELF] => /test.php/foo/bar
[SCRIPT_NAME] => /test.php/

(this seems to be the only case when PATH_INFO contains sensible information [PATH_INFO] => /foo/bar) Note: this used to be different in some older PHP versions (<= 5.0 ?).

Example 2. REQUEST_URI is different from SCRIPT_NAME when a non-empty query string is entered:
http://example.com/test.php?foo=bar

[SCRIPT_NAME] => /test.php
[REQUEST_URI] => /test.php?foo=bar

Example 3. REQUEST_URI is different from SCRIPT_NAME when server-side redirecton is in effect (for example mod_rewrite on apache):

http://example.com/test.php

[REQUEST_URI] => /test.php [SCRIPT_NAME] => /test2.php

Example 4. REQUEST_URI is different from SCRIPT_NAME when handling HTTP errors with scripts.
Using apache directive ErrorDocument 404 /404error.php
http://example.com/test.php

[REQUEST\_URI] => /test.php
[SCRIPT\_NAME] => /404error.php

On IIS server using custom error pages
http://example.com/test.php

[SCRIPT\_NAME] => /404error.php
[REQUEST\_URI] => /404error.php?404;http://example.com/test.php
Odin
+1, "An example is not a way to learn, it's the only way to learn." - I always have to recheck this stuff, very nice research on the 404 errors. =)
Alix Axel
A: 

Backup a second, you've taken the wrong approach to begin with. Why not just do this

RewriteEngine on RewriteCond $1 !^(images|inc|favicon.ico|index.php|robots.txt) RewriteRule ^(.*)$ /index.php?url=$1 [L]

instead? Then grab it with $_GET['url'];