psterling Posted October 10, 2006 Share Posted October 10, 2006 Hi. I've scoured this board for the last 2.5 hours, but can't seem to find the solution to my problem.I am trying to modify my php code to make my site completely portable (domain-name independent), and so I can more easily write it on my testing server, then upload to the live server once completed. Therefore, I want to avoid using absolute links as much as possible.I have written a file (config.inc) that contains the global config settings, and included it with each php file. I tried to set the base path using two different methods, which both give me the same result:$config['root'] = '/hsphere/local/home/myaccount/mysite.com/';AND$config['docRoot'] = $_SERVER['DOCUMENT_ROOT'];However, when I try to use either in a link, I get the following address result:http://www.mysite.com/hsphere/local/home/myaccount/mysite.com/index.phpThat is clearly wrong--I wanted this instead:http://www.mysite.com/index.phpI can easily get the desired result by using $_SERVER['HTTP_HOST'], but it's my understanding that this absolute reference will make the site not portable (it's always looking for http://www.mysite.com).Anyone know how to achieve my objective? Ultimately, I would like to do something like the following, and it act like a normal relative reference (i.e. ../../docs/misc/example.html):<a href="<?php echo $_SERVER['DOCUMENT_ROOT']?>/docs/misc/example.html">Test Link</a>Thanks in advance... Quote Link to comment https://forums.phpfreaks.com/topic/23568-relative-links-with-_serverdocument_root-possible-solved/ Share on other sites More sharing options...
Hi I Am Timbo Posted October 10, 2006 Share Posted October 10, 2006 You can't use the local filesystem of the server for links. A great way of doing it is using relative http links!<a href="foo/bar.php">Foo's Bar</a><a href="/foo/bar.php">Foo's Bar from the http root</a><a href="bar.php">Bar</a>Otherwise, just set the $config['root'] = 'http://www.localdomain.com' or to $_SERVER['http_host'] -- I'm not sure that will work. Quote Link to comment https://forums.phpfreaks.com/topic/23568-relative-links-with-_serverdocument_root-possible-solved/#findComment-106976 Share on other sites More sharing options...
psterling Posted October 10, 2006 Author Share Posted October 10, 2006 I was afraid you'd say that. The full server path appears to work just fine for uploading files, however. Too bad I can't work the links this way--it seems it would be much easier! Thanks for the ultra-quick response, though (<4 minutes)!! Quote Link to comment https://forums.phpfreaks.com/topic/23568-relative-links-with-_serverdocument_root-possible-solved/#findComment-106989 Share on other sites More sharing options...
psterling Posted October 10, 2006 Author Share Posted October 10, 2006 [quote author=Hi I Am Timbo link=topic=111099.msg449991#msg449991 date=1160498516]<a href="/foo/bar.php">Foo's Bar from the http root</a>[/quote]I'm gonna do a little testing with this. I've been coding html for 5+ years now, but didn't really ever have a complete grasp on the difference between:foo/bar.phpAND/foo/bar.phpYou might have just answered my initial question (and ENLIGHTENED me in the process!). It appears that maybe I can do what I wanted without the config.inc file?Question: If I have a hyperlink in a file (www.mysite.com/a/b/c/d/test.html), and I want to relatively reference (www.mysite.com/result.html), you're saying I can just have my reference as (/result.html)?If so, I've been taking the LOOOONG way around for the last 5 years! Quote Link to comment https://forums.phpfreaks.com/topic/23568-relative-links-with-_serverdocument_root-possible-solved/#findComment-106998 Share on other sites More sharing options...
Daniel0 Posted October 10, 2006 Share Posted October 10, 2006 You might want to do this:[code]$path = empty(@getcwd()) ? @getcwd() ? str_replace($_SERVER['document_root'],'',@getcwd());[/code] Quote Link to comment https://forums.phpfreaks.com/topic/23568-relative-links-with-_serverdocument_root-possible-solved/#findComment-107001 Share on other sites More sharing options...
psterling Posted October 10, 2006 Author Share Posted October 10, 2006 The basic html WORKS!!! I can't believe the solution has been so simple, and I've been wondering what the difference was for years (when something didn't work, I just did it the other way, but never really understood)! Quote Link to comment https://forums.phpfreaks.com/topic/23568-relative-links-with-_serverdocument_root-possible-solved/#findComment-107008 Share on other sites More sharing options...
kwstephenchan Posted October 11, 2006 Share Posted October 11, 2006 interesting topic, I am new to both PHP and HTML and I may need something similar in the future. I got lost! Wonder if you guys can explain a little more? What does this foo/ or /foo/ mean and what is this [/url]. Thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/23568-relative-links-with-_serverdocument_root-possible-solved/#findComment-107564 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.