Problems with pointing to files on local server versus remote
I'm having a really silly issue... basically I'm working on a site on a localhost, and a remote host... I am including files by doing the following:
<script type="text/javascript" src="/js/jquery-1.4.2.min.js"></script>
This works a treat locally. However on the remote, while its not live, the site address is (pseudo) xxx.xxx.xxx.xxx/~user and so obviously, the js folder is xxx.xxx.xxx.xxx/~user/js/ which means my /js/jquery... include isn't going to work.
Obviously you're thinking I just remove the forward slash at the start of the /js and simply do a relative src - the problem with this is that I'm using mod_rewrite with clean urls, so sometimes when the file which includes the above js src is being called from a few directories deep (or so the computer thinks) ie url.com/blah/blah/blah/ so with a relative src, its going to try url.com/blah/blah/blah/js.
I'm on a php environment and I'开发者_如何学Cm just wondering what the least complex approach to all this is. ie $_SERVER['document_root'] . '/js/jquery....' is something my feeble mind tried but as you the expert probably know - its going to bring up a bunch of irrelevant unix directories and not just go to the current site root.
Any ideas?
When I'm working on various hosts with the same source code, I usually define constants for this sort of thing.
define('SITE_ROOT', '/');
or
define('SITE_ROOT', '/~user/');
Or, you could use this function. I haven't tested it extensively, but it works in the test I just performed. It returns the root relative url with a trailing slash.
function get_root_relative_dir()
{
$root_dir = $_SERVER['DOCUMENT_ROOT'];
$script_dir = dirname($_SERVER['SCRIPT_FILENAME']);
return substr($script_dir, strlen($root_dir)) . '/';
}
精彩评论