Page 1 of 1

Get php script path in CGI, and SAPI?

Posted: Sun Jun 24, 2007 3:07 pm
by Chris Corbyn
The post only refers to PHP >= 5.1...

Say I have a PHP script at:

http://localhost/~username/script.php

I can get the "/~username/script.php" part by looking in $_SERVER["SCRIPT_NAME"] when using CGI and when using the apache module.

Now if I go to this URL:

http://localhost/~username/script.php/foo/bar

It's still loading script.php as it should be, but now under SAPI, SCRIPT_NAME is just like it was before, whereas in CGI SCRIPT_NAME has been screwed up (I can't even make sense of why it does this):

URL:

http://php5host/~d11wtq/Router/demo.php/foo/bar

SCRIPT_NAME as seen in SAPI:
[SCRIPT_NAME] => /~d11wtq/Router/demo.php
SCRIPT_NAME as seen in CGI:
[SCRIPT_NAME] => tq/Sites/Router/demo.php
It's truncated :?

I knew there were discrepencies with CGI and SAPI in terms of what $_SERVER contains, but does anyone know how I can get the "~d11wtq/Router/demo.php" part in both CGI and SAPI with a pretty high chance of success? It doesn't have to be 100% perfect since it's settable anyway.

Posted: Sun Jun 24, 2007 4:18 pm
by Chris Corbyn
After reading the CGI specification, this is certainly a PHP bug. SCRIPT_NAME must be present, and it must contain the virtual path to the script being executed.

I have nothing to go but educated guess work here, but considering the string length of the bad SCRIPT_NAME part *exactly* matches the string length of the correct version, I suspect the PHP developers have gotten mixed up and truncted the wrong environment variable (SCRIPT_FILENAME instead of SCRIPT_NAME!!).

I'm sure I've overlooked something with this but it fixes what I've tried so far:

Code: Select all

if (isset($_SERVER["SCRIPT_NAME"]))
{
  if (isset($_SERVER["ORIG_PATH_INFO"]))
  {
    $len = strlen($_SERVER["SCRIPT_NAME"]);
    if (($tmp = substr($_SERVER["ORIG_PATH_INFO"], 0, $len)) != $_SERVER["SCRIPT_NAME"])
    {
      $_SERVER["SCRIPT_NAME"] = $tmp;
    }
  }
}

Posted: Sun Jun 24, 2007 5:05 pm
by superdezign
I'm still not getting why you're using SCRIPT_NAME in a class such as yours. Why not REQUEST_URI?
Or am I missing something?

Posted: Sun Jun 24, 2007 5:11 pm
by Chris Corbyn
superdezign wrote:I'm still not getting why you're using SCRIPT_NAME in a class such as yours. Why not REQUEST_URI?
Or am I missing something?
Because REQUEST_URI contains the wrong thing. All I need is the script name, not the full URI. This is for a request router.

If I go to:

http://www.mysite.com/script.php/some/extra/path

Then SCRIPT_NAME should contain /script.php, whereas REQUEST_URI should contain /script.php/some/extra/path

Not the same at all ;)

Posted: Sun Jun 24, 2007 5:38 pm
by superdezign
I figured that's what it was for, so I though "REQUEST_URI." :P

If worst comes to worse, you could always regex it out of REQUEST_URI. It'd be a simple pattern.

Posted: Sun Jun 24, 2007 6:00 pm
by Chris Corbyn
superdezign wrote:I figured that's what it was for, so I though "REQUEST_URI." :P

If worst comes to worse, you could always regex it out of REQUEST_URI. It'd be a simple pattern.
Not really, because say I had:

http://mysite.com/script.php/some/fake/ ... hp/foo/bar

Which .php do I read? ;) My code works fine... the PHP developers have fixed this issue in CVS now :)

Posted: Sun Jun 24, 2007 6:34 pm
by superdezign
Whoa. You got them to fix it? Nice. ^_^