Forum Moderators: coopster

Message Too Old, No Replies

Changing a link based on directory it's in?

Ah, the troubles of PHP

         

TheRookie

7:06 am on Apr 3, 2005 (gmt 0)

10+ Year Member



My latest problem:

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?

ergophobe

6:13 pm on Apr 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




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.

TheRookie

6:20 pm on Apr 3, 2005 (gmt 0)

10+ Year Member



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.

ergophobe

4:51 pm on Apr 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I thought you were referring to a problem resolving relative links.

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.

EBear

5:06 pm on Apr 6, 2005 (gmt 0)

10+ Year Member



I may be completely misreading the problem here, because I actually know very little PHP. But the example you gave can be solved by always linking to /index.php, assuming index.php is in your web root. Likewise /subfolder/topic1/filename.php will work cosistently from any file/folder in the site.

(AFAIK, all this can be changed by the server set-up, but very rarely is.)

TheRookie

7:01 pm on Apr 6, 2005 (gmt 0)

10+ Year Member



I believe you're missing the problem. See, I am inserting the same include on all of my pages, which links to the index. However, based on the page it's on, it's not always just ../index.php - sometimes it might be ../../index.php, and I'm seeing if it can always point to the right place without actually putting in the entire URL.

jatar_k

8:51 pm on Apr 6, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



is the index.php page in your root

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?

TheRookie

11:34 pm on Apr 6, 2005 (gmt 0)

10+ Year Member



include $_SERVER['DOCUMENT_ROOT'] . '/products/index.php';

is that what you meant?

Yes! :-)

Thanks to everyone who helped.