Odd PHP question

Kurt Granroth plug-discuss at granroth.org
Sun Jan 15 16:57:05 MST 2006


This isn't directly Linux related but since I know there are a lot of  
PHP folks on this list, I thought I'd ask here.

I have multiple domain and multiple subdomain support with my web  
hosting enabled mostly by using mod_rewrite.  The end result is that  
I can define my domains and subdomains by just creating the proper  
directory structure.

For instance, say I have "sub.domain.com", "other.domain.com", and  
"cool.com".  I would simply create following directory structure:

$DOCUMENT_ROOT/domain.com/sub/
$DOCUMENT_ROOT/domain.com/other/
$DOCUMENT_ROOT/cool.com/

So far, so good.  That all works just as expected.  Now say, though,  
that I have a PHP file 'index.php' in the directory 'domain.com/sub'  
that looks like so:

<?php echo $_SERVER["PHP_SELF"] ?>

I then execute the script using "http://sub.domain.com/index.php".   
The result:

/domain.com/sub/index.php

This is technically accurate... but not at all what I want.  Why?   
Because typically, PHP code uses the dirname() of this to find other  
relative scripts.  If you do that, though, then the constructed URL  
will look like so:

http://sub.domain.com/domain.com/sub/someother.php

instead of

http://sub.domain.com/someother.php

So it seems that I somehow have to "fool" PHP into thinking that  
PHP_SELF (and SCRIPT_NAME and SCRIPT_FILENAME) is "/index.php"  
instead of "/domain.com/sub/index.php"

Is that even possible?  If so, how.  If not, is there any way around  
this at all?

Thanks!
Kurt


More information about the PLUG-discuss mailing list