Jump to content


Photo

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


  • Please log in to reply
6 replies to this topic

#1 psterling

psterling
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 10 October 2006 - 04:34 PM

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.co...e.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...


#2 Hi I Am Timbo

Hi I Am Timbo
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 10 October 2006 - 04:41 PM

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.

#3 psterling

psterling
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 10 October 2006 - 05:02 PM

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)!!

#4 psterling

psterling
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 10 October 2006 - 05:08 PM

<a href="/foo/bar.php">Foo's Bar from the http root</a>


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!

#5 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 10 October 2006 - 05:12 PM

You might want to do this:

$path = empty(@getcwd()) ? @getcwd() ? str_replace($_SERVER['document_root'],'',@getcwd());


#6 psterling

psterling
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 10 October 2006 - 05:17 PM

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)!



#7 kwstephenchan

kwstephenchan
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 11 October 2006 - 07:55 PM

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!!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users