Jump to content

Relative links with $_SERVER['DOCUMENT_ROOT'] possible? [SOLVED]


psterling

Recommended Posts

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.php

That is clearly wrong--I wanted this instead:
http://www.mysite.com/index.php

I 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...
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

[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.php
AND
/foo/bar.php

You 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!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.