So I'm trying to create one central file for my navigation which is used across my site; however, some of the pages are in different directories. So, for example , let's say I have the following directories --

domain.com/
domain.com/dir1/
domain.com/dir2/

If I wanted to use the same include between all three of these options, is there a way for it to figure out how many "../" need to be included based on where it is?

<? define('SITE_ROOT_PATH',$_SERVER['DOCUMENT_ROOT'].'testproject/')
include(SITE_ROOT_PATH.'config.php');
?>

You can add above code in any file of any directory of testproject.

<? define('SITE_ROOT_PATH',$_SERVER['DOCUMENT_ROOT'].'testproject/')
include(SITE_ROOT_PATH.'config.php');
?>

You can add above code in any file of any directory of testproject.

vibhadevit, thanks for the response. This code; however, won't work for what I'm trying to do since it's going to the root of whatever directory its currently in. Let me give you an example that may be a little better than the earlier one. I have the following files:

index.php
newdirectory/index.php
include/navigation.php

So I want to be able to use the navigation.php on both the index.php in the root as well as the one in 'newdirectory' So you're code works for the former but not for the latter since it's looking for the navigation file in 'newdirectory' as opposed to the root of the site. Does that make sense?

Can't you just use:

include '/include/navigation.php';

That is what I'm doing however there are a few files which have includes within another include. So it's causing an issue linking back to the correct place. I've found the answer though, instead of using relative paths, I'm going to use absolute paths (e.g. /home/user/public_html/etc) this is working perfectly! Thanks everyone for the help :)

That is what I'm doing however there are a few files which have includes within another include. So it's causing an issue linking back to the correct place. I've found the answer though, instead of using relative paths, I'm going to use absolute paths (e.g. /home/user/public_html/etc) this is working perfectly! Thanks everyone for the help :)

Actually here's another sort of related question, so using the absolute paths is great for the includes but they don't work obviously in a standard link. So, going on my previous example, let's say I was on index.php (in the root) and click on page 2. That takes me /page2/index.php. Now when I click on home in this directory it takes me back to /page/index.php.

Is there a way to make these nav links where they are always poitining to the correct directory?

In this case Home should point to /index.php and not to index.php

Right, but the issue is how do you define that when using one central navigation file. So if I say in the navigation include file

<a href="index.php">Home</a>

Then it will work for part of the site but as soon as you go into another directory it won't go to the correct place.

Not quite sure now what you are trying to achieve.

<a href="/index.php">Home</a>

The above will not solve your problem, because ?

Not quite sure now what you are trying to achieve.

<a href="/index.php">Home</a>

The above will not solve your problem, because ?

Maybe I am missing something but is that not going to generate errors on the root since it's trying to go into a recursive directory? :-/

Member Avatar for diafol

As mentioned in the above posts:

<?php
$ROOT = $_SERVER['DOCUMENT_ROOT'];
//OR define into constant ROOT - however, should a constant be a variable??
//constants have the advantage of being global.
define('ROOT',$_SERVER['DOCUMENT_ROOT']);

//so includes and requires use the $ROOT or ROOT prefix
?>

As pritaeas says, making html links and references absolute ('/.....'), should solve your 'many locations' problem.

My 2p.

Thank you all for your help, I went ahead and tested out pritaeas's code and it fixed the issue. I didn't realize that using '../' or '/' will only recurse into the root regardless of how many you use. Sorry for the confusion :$

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.