Forum Moderators: coopster

Message Too Old, No Replies

$_SERVER['SCRIPT_NAME'] not independent of URI

         

heisters

6:31 pm on May 2, 2006 (gmt 0)

10+ Year Member



I'm doing some mod_rewrite voodoo to get clean urls to a script that uses $_GET. The rule basically looks like this:


RewriteRule ^/foo/(\d{4})/(\d{1,2})/?$ /foo?year=$1&month=$2

So I'm masking year and month $_GET variables as path elements. Simple.

However, when I refer to $_SERVER['SCRIPT_NAME'] I get the URI, as it appears to the user, eg. "foo/2006/5", which doesn't really exist. However, $_SERVER['SCRIPT_FILENAME'] will always show the right script name, eg. "/foo" BUT it prepends the document root. So, it seems like functionally SCRIPT_NAME is the same as REQUEST_URI, whereas I'd like it to be inbetween REQUEST_URI and SCRIPT_FILENAME: showing the actual script that's executing, but without the document root.

Right now I'm working around it by just using SCRIPT_FILENAME and doing a str_replace() to remove the DOCUMENT_ROOT. But that seems suboptimal and potentially buggy; the real issue here is that SCRIPT_NAME isn't yielding what I'd expect from the PHP docs. Is there something I'm missing? Does anyone know of another $_SERVER variable that accomplishes what I'm looking for.

eelixduppy

2:15 am on May 3, 2006 (gmt 0)



Hello....

I'm not sure, but...will this work?:

$name = ".".$_SERVER['SCRIPT_NAME'];

Hope this helps...and if not you can blame it on my lack of sleep :)

eelix

eelixduppy

11:15 am on May 3, 2006 (gmt 0)



To answer my own question, no. This is another way to do it though instead of using str_replace:

$page = substr('$_SERVER[SCRIPT_NAME]', 1);

Next time i won't write random things that have nothing to do with your problem :)

eelix

heisters

7:29 pm on May 3, 2006 (gmt 0)

10+ Year Member



That would give me "oo/2006/5", which is still incorrect. I could do something like

preg_replace('/\/*/', '', $_SERVER['SCRIPT_NAME']);

Which would crop the first slash and everything after it, but that's less efficient that what I'm currently doing, namely

str_replace($_SERVER['DOCUMENT_ROOT'], '', $_SERVER['SCRIPT_FILENAME']);

But finding a workaround isn't really my question. I'm mostly just looking for the "best practices" solution.

Thanks anyhow.

barns101

10:03 am on May 4, 2006 (gmt 0)

10+ Year Member



I think that what you're doing sounds similar to something I am working on at the moment. I use $variables = explode("/",$REQUEST_URI);