Php include () for navigation uses absolute paths, but then


I recently started using more and more php include () for several small sites with no directories and am now working on a large site with several directories and sub-directories.

I also use php include () for the navigation where my problem starts.

With a small site and no sub-directories I can use a relative path and preview the whole site locally using the testing server, but with directories and sub-directories, I have to use an absolute path to get the links working, but then I can’t check my site locally on my computer (without uploading at first), because the browser tries to load absolute paths from the server and not from my testing server (Inetpub\wwwroot\).

Is there any work-around for this (Win XP Pro, DW MX 2004)?
How do you handle this?

include handles all types of paths…

include ("../included_files/include.php");  //outside the root dir
include ("../../included_files/include.php");  // two folders up outside the root...
include ("");  // remote files
include ("inc/my_included_files.php"); // sub directories
include ("file_name.php"); // same directory...

i would make sure that the directory structure is the same on the server as it is on your local machine, that way it should work locally and on the server.

You’re of course right, I don’t know what I was thinking when I posted this:blush:, maybe I should take a break…

But there’s still a problem when I use relative paths within the included file (the navigation) which is called from different directories or sub-directories.

You need to remember that any paths in the include file are relative to the script that calls the include and NOT the include its self.
You have to be aware of that when structuring your site.
An include almost literally pastes the entire content of its code into the calling script.

if your still struggling i recommend posting your code and a set up of your directory structure…

Thanks a lot for your help!

  My directory structure is like this:

[/list]My included files are all in “root/include/” and the my included navigation looks like this

      <a href="file-a.php">file-a</a>
      <a href="file-b.php">file-b</a>
      <a href="file-c.php">file-c</a>
  The files "file-a.php", "file-b.php" and "file-c.php" are all in "root/"
  I would like to call the included navigation from any of the above directories. 

If I have a file “myfile.php” in “root/” and call the included navigation, then the included navigation works, if I click on the link “file-a”.

If I have a file “myfile.php” in “root/dir-a/sub-1/” and call the included navigation, then the included navigation does not work, if I click on the link “file-a”, because the path should be “…/…/file-a.php” and not just “file-a.php”.

  This means that I have to use absolute paths for the included navigation. Or am I totally wrong here?

And then I have problems previewing my site locally, because the browser connects to the real server and not the testing server.

The solution is pretty simple. I used it on some of my sites which have files nested inside directories, and have subnavs, etc.


set up a variable. I call mine $path2root. I put this variable at the top of everypage on the site. Ofcourse this must be before the include script.

here’s how I point to my file with the navigation in it which exists in root/includes

First put this at the top of every page, before the include to the nav file, of course the values would change depending where the file is located. So this file is in root/dir-a/sub-1

$path2root = "../..";

<?php include "$path2root/includes/nav.php" ;?>

and in the nav.php file, the links for my nav would look like this:

<a href="<?php echo "$path2root"; ?>/index.php">Home</a>

And actually I use another $path2root for other things and use $path2nav. Why? well if you are on a page say index.php which is in your root dir. and you have the nav.php included. The user clicks to go to contact.php which also exists on the root dir, the code above goes to the link: /contact.php

Do you see the problem? the slash shouldn’t be there, so using $path2nav on everypage for navigation I can do this: (this is for files on the root)

$path2nav = "";

and in includes/nav.php I do:

<a href="<?php echo "$path2nav"; ?>index.php">Home</a>

Fixed! and for pages in root/dir-a set:

$path2nav = "../";

and so on. Hope this helps you and helps you understand how I’m using variables to solve the problem.

Hi Roshan,

thanks a lot for your reply!

This looks like a good solution, I’ll give it a try.

PS: Nice homepage!

I usually handle this in my config file using several “define()” statements. For example:


I then include the config file and use the defines as such:

require_once 'conf/settings.conf.php';
require_once LIB_DIR.'/my.lib.php';

This is just my personal preference, however. Just as any method would, it does have its obvious advantages and disadvantages. Feel free to use however you’d like - at your own risk, of course. :wink:



thanks for your reply!

I have not been using many includes in the past, but I’ll give all solutions here a try and see what works best for me. Everything is better than using absolute paths.

Thanks again!

No problem bud. :slight_smile: Just happy to contribute my part to the community. :tup:

Thanks, I really appreciate your help!

Google seems to be able to handle this and I have come to use it for all my sites

it also works well with CSS

url ==

add this to your index.php file
<div id=“content”

if (nav < ‘1’){

<div id"content">
if (nav == '1"){


and so on…

This way everything spurs off of your index.php and root and you will not have to wory about absolute paths…This also allow for you to change the layout of the site just by changing the CSS or index.php page and

Here is the method that I recently switched to using so that I could move my sites from one server to the next without having any include path issues.

My directory structure looks like this:

/httpd/php – for include files
/httpd/html – the document root

I use the following statement to include one of my files in the /httpd/php directory.

include( dirname( $_SERVER[‘DOCUMENT_ROOT’] ) . “/php/<include filename>” );

The dirname command gets the parent directory of the one specified (aka the document root) and then I can put in my php directory and the filename of the include file.


Something that I do is create a variable called $page_root_path and include that on every page at the top. I then append all links, includes, references and what not using that variable. Similar to:

$page_root_path = './';
//For nested pages
//$page_root_path = './../';

include($page_root_path . 'include_folder/includes.php');

echo '<a href="' . $page_root_path . 'linked_file.php">File is linked</a>';

This has worked in all applications that I have built wether it is single directory or multiple nested directories. Hope it can help.

I use an absolute path defined seperately now too. That way it will work if you move a directory to another location or nest it down further without having to change anything (except maybe links to the page ;)).

Thanks a lot for all your replies!

Great solutions.

I personally use the same method as Roshan Bhakta (post #7) and it works out very well for me :slight_smile:

You’re welcome pixelcop. Wow, a ton of solutions.

Thanks again for your solution - works great!

It does not really matter, but I think, you can even get away with 1 variable only:

For files on root, I give the variable a value of “.”: $root=“.”
For files in a subdirectory, I have $root=“…”
(Both without the trailing slash).

The included navigation then looks like this:
<a href=“<?php echo “$root”; ?>/file-a.php”>File-A</a> (for files on root)

<a href=“<?php echo “$root”; ?>/dir-a/file-a.php”>File-A</a> (for files in a subdirectory)

Seems to work.