...

View Full Version : Creating URL base



Nightchild
01-20-2008, 07: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);

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

<?
DEFINE('FP_ROOT','http://blah.com');
DEFINE('FP_FILE_ROOT','/home/httpd/vhosts/blah.com');
?>
If I change hosts I then simply need to change the config file

Nightchild
01-20-2008, 02: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 ?

Inigoesdr
01-20-2008, 04: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/'));

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum