Let's say I have the majority of my site in the 'root' WWW directory on the server, such as:

www.domain.com/index.php

My current header & footer files are set to respect this configuration and work beautifully. I am now in the process however of creating a new directory which will be used in the main site, such as:

www.domain.com/newfolder/index.php

What would be the easiest way to make the header and footer file work in both directories instead of having to create a new header/footer file for each directory? Perhaps another way to say this, is there a way to append a "relative" file name depending on which directory the file is in?

Thanks!

Member Avatar for diafol
include('includes/header.php');

for top directory

include('../includes/header.php');

for second level directory

Is that it?

Wow talk about a quick reply! ;)

I am currently doing that but that's not really what I'm after here. Let me give another example that's not related to the header/footer above. Let's say I have a central file called menu.php that has a list of all of my pages found in the menu. If you were in the 'root' directory, the relative link would look something like:

page1.php
page2.php
page3.php

Usuing the same menu.php file though, when you are navigating the site and go to the 'test' directory, the links would break since they now refer to objects that exist in the 'test' directory, not the 'root'. So I am looking for something that does this:

If ( Directory = something other than root ) {
echo '../page1.php';
echo '../page2.php';
echo '../page3.php';
}

Else {
echo 'page1.php';
echo 'page2.php';
echo 'page3.php';
}

I realize that's not proper syntax, but just to give you an idea of what I am looking for. My problem is finding the variable/function in PHP that would find out what the directory the page currently resides in?

Member Avatar for diafol

There's a better way to do this, but a simple workaround would be:

include($_SERVER['DOCUMENT_ROOT'] . "/includes/header.php");

That'll work from anywhere in your code (most likely).


If it's for navigation, use absolutes:

<li><a href="/index.php">home</a></li>
<li><a href="/folder/newpage.php">page</a></li>
commented: This is the exactly what I needed! Thanks a lot! +0

or if using multiple directory structures

<?php define('_path','../../');//example only it would be different for each folder
include(_path.'menu.php');?>

and each item in the menu file also references the path <a href='<?php echo _path; ?>/downto/thisfolder.htm'>thisfolder</a>

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.