SCRIPT_NAME PATH_INFO PHP_SELF

All about questions, hints and tricks about the PHP language and its use.
Post Reply
User avatar
Lakys
Site Admin
Site Admin
Posts: 5189
Joined: Sun Dec 12, 2004 3:30 am
Contact:

SCRIPT_NAME PATH_INFO PHP_SELF

Post by Lakys » Thu Mar 12, 2009 6:39 pm

Well, in some rare cases, the REQUEST_URI server var does not contain anything. So the information must be sought somewhere else in other server vars.

I found this note from the PHP bug tracking site.

It seems obvious that it has not always worked the same, because of various web server servers, PHP revisions, and so forth.

Now I came to some various functions handling things with SCRIPT_NAME and PATH_INFO for IIS. Well that's interesting, but I have not (yet) seen any universal script.

And I wonder if those scripts were not because of some bugs.

Good thing is I have a IIS6 server. I use to run it with PHP/ISAPI, but I can run some test with CGI or FastCGI to establish and confirm a few things.

I think it would be interesting to also check in Apache and lighthttpd server.

I shall assume that we use some recent PHP 5.x revision, say the 5.2.9 to get started. Well your hosting provider might use something older, but anyway, it should be upgrade sooner or later. Anyone still using the buggy 5.2.3 version mentioned in the link? :roll:
Last edited by Lakys on Tue Feb 22, 2011 4:54 pm, edited 3 times in total.

User avatar
Lakys
Site Admin
Site Admin
Posts: 5189
Joined: Sun Dec 12, 2004 3:30 am
Contact:

Methodology

Post by Lakys » Thu Mar 12, 2009 9:21 pm

Ok to test things with IIS, I set three different hostings one with PHP FastCGI, one with CGI and the last with ISAPI. The three of them have a subfolder t with symlinks. In this folder, there is an index.php file. Note that there is no caps in the name.

Here is the source of /t/index.php:

Code: Select all

<?php
    function show_var($var)
    {
        echo $var . ': ' . $_SERVER[$var];
        echo '<br />';
    }
    echo 'PHP VERSION: ' . phpversion() . '<br /><br />';
    show_var('PATH_TRANSLATED');
    show_var('PATH_INFO');
    show_var('SCRIPT_FILENAME');
    show_var('SCRIPT_NAME');
    show_var('PHP_SELF');
    show_var('REQUEST_URI');
    show_var('QUERY_STRING');
    echo '<br />';
    echo 'ISAPI Rewrite is installed, otherwise this var doesn\'t exist<br />';
    show_var('HTTP_X_REWRITE_URL');
    echo '<br />';
?>

User avatar
Lakys
Site Admin
Site Admin
Posts: 5189
Joined: Sun Dec 12, 2004 3:30 am
Contact:

Results

Post by Lakys » Thu Mar 12, 2009 9:30 pm

CGI

Code: Select all

PHP VERSION: 5.2.9

PATH_TRANSLATED: C:/Inetpub/vhosts/lkwh.com/subdomains/cgi/httpdocs/test
PATH_INFO: /test
SCRIPT_FILENAME: C:\Inetpub\vhosts\lkwh.com\subdomains\cgi\httpdocs\t\index.php
SCRIPT_NAME: /t/index.php
PHP_SELF: /t/index.php/test
REQUEST_URI:
QUERY_STRING: query=hello&q2=3

ISAPI Rewrite is installed, otherwise this var doesn't exist
HTTP_X_REWRITE_URL: /t/index.php/test?query=hello&q2=3
FASTCGI

Code: Select all

PHP VERSION: 5.2.9

PATH_TRANSLATED: C:\Inetpub\vhosts\lkwh.com\subdomains\fastcgi\httpdocs\t\index.php\test
PATH_INFO: /test
SCRIPT_FILENAME: C:\Inetpub\vhosts\lkwh.com\subdomains\fastcgi\httpdocs\t\index.php
SCRIPT_NAME: /t/index.php
PHP_SELF: /t/index.php/test
REQUEST_URI: /t/index.php/test?query=hello&q2=3
QUERY_STRING: query=hello&q2=3

ISAPI Rewrite is installed, otherwise this var doesn't exist
HTTP_X_REWRITE_URL: /t/index.php/test?query=hello&q2=3
ISAPI

Code: Select all

PHP VERSION: 5.2.9

PATH_TRANSLATED: C:\Inetpub\vhosts\lkwh.com\subdomains\isapi\httpdocs\test
PATH_INFO: /test
SCRIPT_FILENAME: C:\Inetpub\vhosts\lkwh.com\subdomains\isapi\httpdocs\t\index.php
SCRIPT_NAME: /t/index.php
PHP_SELF: /t/index.php
REQUEST_URI: /t/index.php/test?query=hello&q2=3
QUERY_STRING: query=hello&q2=3

ISAPI Rewrite is installed, otherwise this var doesn't exist
HTTP_X_REWRITE_URL: /t/index.php/test?query=hello&q2=3

User avatar
Lakys
Site Admin
Site Admin
Posts: 5189
Joined: Sun Dec 12, 2004 3:30 am
Contact:

Result Analysis and conclusion

Post by Lakys » Thu Mar 12, 2009 9:48 pm

Well, doing this test was not innocent, but I am glad I made as it enlightens me about many things:

First PHP_SELF is not accurate and should not be used. But it seems that most serious functions are avoiding it. This function returns the script name (ISAPI) and potentially the extra path just after (FastCGI / CGI).

REQUEST_URI works on most web servers (Apache, lighthttpd, ISAPI/IIS, FastCGI/IIS) but not on CGI/IIS.

Except PHP_SELF, ISAPI and FastCGI returns the same values. REQUEST_URI is available. Else, it can be determined by using

Code: Select all

$_SERVER['SCRIPT_NAME'] . $_SERVER['PATH_INFO'] . '?' . $_SERVER['QUERY_STRING']
providing it has a query string.

This can be useful especially if REQUEST_URI is not the true server one but a rewritten one, which occurs with Apache.

chrisadam
Visitor
Visitor
Posts: 2
Joined: Mon Sep 13, 2010 11:14 am

Post by chrisadam » Mon Sep 13, 2010 11:18 am

It seems obvious that it has not always worked the same, because of various web server servers, PHP revisions, and so forth.

Now I came to some various functions handling things with SCRIPT_NAME and PATH_INFO for IIS. Well that's interesting, but I have not (yet) seen any universal script.

And I wonder if those scripts were not because of some bugs.

Good thing is I have a IIS6 server. I use to run it with PHP/ISAPI, but I can run some test with CGI or FastCGI to establish and confirm a few things.

____________________________________________________________________________


Want to get-on Google's first page and loads of traffic to your website? Hire a SEO specialist from Ocean Groups[url=http://oceangroups.org/] seo specialist [/url]

User avatar
Lakys
Site Admin
Site Admin
Posts: 5189
Joined: Sun Dec 12, 2004 3:30 am
Contact:

Post by Lakys » Thu Sep 16, 2010 8:50 am

chrisadam wrote:It seems obvious that it has not always worked the same, because of various web server servers, PHP revisions, and so forth.
Indeed.
Now I came to some various functions handling things with SCRIPT_NAME and PATH_INFO for IIS. Well that's interesting, but I have not (yet) seen any universal script.
Hard to find, but not impossible. Most open source web applications (CMS, galleries, wikis, e-commerce, forums,...) deal with it with their own method. I came to adapt the code in one of them, as it seems that the IIS part was not appropriate anymore.
And I wonder if those scripts were not because of some bugs.
Hard to say. As you said there are just too many various web servers working in different ways. At least with a small script you can cover most web servers and variations (Apache, IIS, lighthttpd)
Good thing is I have a IIS6 server. I use to run it with PHP/ISAPI, but I can run some test with CGI or FastCGI to establish and confirm a few things.
I also used to work with PHP with IIS/ISAPI. As FastCGI has been integrated much better with IIS (especially in Windows Server 2008), I have ended to move most of my websites to FastCGI.
As it seems that Microsoft's support for PHP ISAPI will come to an end, I guess we have no real choice.

Post Reply

Return to “PHP Development”

Who is online

Users browsing this forum: No registered users and 1 guest