Page 1 of 1

Absolute and relative includes

Posted: Thu Feb 10, 2005 12:31 pm
by sonofslim
I have an include that references $PHP_SELF. This script it wasn't working as expected on certain pages, and I discovered that these pages all called the include with an absolute URL, like so: include("http://blabla/include.php")

In these cases, $PHP_SELF was equal to include.php. When a relative URL is used [ include("../include.php") ], $PHP_SELF is equal to the script that calls the include.

It's no hassle to change the few pages which use an absolute URL, but I'm curious as to why this happens. Can anyone explain?

Thanks.

Posted: Thu Feb 10, 2005 4:33 pm
by djot
-
I have trouble with that all day. Each server returns different values for php_self, document_root or script_name.

Would like to hear a solution that fits all servers also.

Currently I have a class that checks all of them and combines them to what I need, including adding missing slashes or removing slashes and so on.

It really get's funny when you have a url, with differnt path than the document_root, which also is different from the users_root :)

djot
-

Posted: Thu Feb 10, 2005 5:17 pm
by timvw
actually, it's quite simple....

if you include on the same server, that text (or code) is handled by the current instance of the php-engine -> $_SERVER['PHP_SELF'] has the value of the script that is requested by the user




if you include through url, php requests apache the _output_ of the script -> a new php-engine instance is initialised to execute the code (explains the different $_SERVER['PHP_SELF'] value), and then the output is returned to apache -> and to the requesting script...

Posted: Thu Feb 10, 2005 5:29 pm
by djot
-
actually, it's quite simple...
Just including a php file is not the problem. But when handling includes, directories, paths and urls, and trying to read them from the server variables, there is always trouble ... and PHP_SELF does not lead to anything in this case...

djot
-

Posted: Thu Feb 10, 2005 5:32 pm
by timvw
i got a quite simple solution for that too :)



@localhost -> i have the include_path set in php.ini
@server -> i have the include_path set in .htaccess

this way, i always know from where i'm referencing to files :)

sometimes a constant/environment variables BASE_PATH or something like can be useful too :)