954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Creating Universal Header & Footer Files

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!

jrotunda85
Junior Poster
144 posts since Feb 2011
Reputation Points: 19
Solved Threads: 7
 
include('includes/header.php');

for top directory

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

for second level directory

Is that it?

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

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?

jrotunda85
Junior Poster
144 posts since Feb 2011
Reputation Points: 19
Solved Threads: 7
 

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>
diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

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>

almostbob
Posting Sensei
3,149 posts since Jan 2009
Reputation Points: 571
Solved Threads: 376
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: