Forum Moderators: coopster
I'm having trouble with my header include. It has links (Home, Forum, etc), but depending on what page it's on, I think I need it to change.
For example, when the header is on a page in my main directory, I just need one of the links to link to "index.php". But when it's in a subfolder, it needs to link to "../index.php". Is there a way to make it "know" where to link to or automatically changed based on the directory that it's in?
but depending on what page it's on, I think I need it to change.
Not if it's an absolute link. Unless you have thousands of links (and therefore they add up to serious page weight/bandwidth), I just think that absolute links based on server root are easiest to deal with
$href_link = $_SERVER['HTTP_HOST'] . $page;
$href_link = $_SERVER['SERVER_NAME'] . $page;
$include_path = $_SERVER['DOCUMENT_ROOT'] . $file;
depending on the situation.
Not if it's an absolute link. Unless you have thousands of links (and therefore they add up to serious page weight/bandwidth), I just think that absolute links based on server root are easiest to deal with$href_link = $_SERVER['HTTP_HOST'] . $page;
$href_link = $_SERVER['SERVER_NAME'] . $page;
$include_path = $_SERVER['DOCUMENT_ROOT'] . $file;
depending on the situation.
Thanks! It's good that there's a solution...now if only I knew what that meant.
How do I know what the situation is? I'm unsure of what to change in the code you gave me. Here's an example, so maybe we can figure out the situation that way.
My home link needs to go to [example.com...]
while my others go to [example.com...]
I'll go google what server roots are so hopefully I can understand a bit more, but is there a way to tell which code I need based on the example I just gave?
Thanks for your help.
web root - where your web documents are, wich is what I should have said rather than server root. So in other words, if your home page is
[example.com...]
and your web home dir is
/home/usr/apache/htdocs/
$_SERVER['SERVER_NAME'] will be example.com
$_SERVER['DOCUMENT_ROOT'] will be /home/usr/apache/htdocs
Anyway, in your case
<a href="http://www.example.com/index.php">Home</a>
<a href="http://www.example.com/folder/page.php">Page</a>
But I'm sure you know that, so I guess I'm still not sure what your question is.
(AFAIK, all this can be changed by the server set-up, but very rarely is.)
the thing is when you are using templates your links need to be absolutes, as do your includes. Then there are no issues with the paths.
use a fully qualified url or root relative url for your links
if i had an index page in a products directory and that products directory was located in the site root then an html link to that would be either
<a href="/products/index.php">Product1</a>
or
<a href="http://www.example.com/products/index.php">Product1</a>
both work from anywhere in the site structure. If I wanted a ph include that will work from anywhere in the site structure then I would do
include $_SERVER['DOCUMENT_ROOT'] . '/products/index.php';
is that what you meant?