View Full Version : Creating URL base

01-20-2008, 06:21 AM
I am trying to create a base URL from which to build all other resource calls. The file this code is in resides in a /includes folder. I first explode the path then reassemble it leaving off the script filename and the '/include' directory.

So '//nexus/raw/include/test.php' becomes 'http://nexus/raw/

Is there a more elegant way of doing this?

// constant for the WWW base - used to assemble web paths
$temp = explode("/", $_SERVER['SCRIPT_NAME']);
$temp2 = 'http://' . $_SERVER['HTTP_HOST'];
for ($x=0; $x < count($temp)-2; $x++) {
$temp2 .= $temp[$x] . '/';
define ("URL_BASE", $temp2);

01-20-2008, 11:08 AM
why cant the include file simply have the hard coded paths ?... e.g. in my config file I always have

If I change hosts I then simply need to change the config file

01-20-2008, 01:45 PM
Yes.... I had thought about using hard coded paths but I would prefer to have dynamic ones so no one would have to edit the configuration files.

I also thought it was a better style to try to have dynamic pathing? No ?

01-20-2008, 03:55 PM
Well, the benefit of hardcoded paths is you only have to change them once, and they will work regardless of any changes you make to code/settings(provided you used them properly to begin with). For your original question, if the file is always going to be in "include" you could do something like this:

$temp = 'http://' . $_SERVER['HTTP_HOST'] . substr($_SERVER['SCRIPT_NAME'], 0, strripos($_SERVER['SCRIPT_NAME'], 'include/'));

01-20-2008, 06:52 PM
Thank you both for your responses.