homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Site-Wide PHP Header & Footer Includes
php header footer

5+ Year Member

Msg#: 3827470 posted 10:52 pm on Jan 15, 2009 (gmt 0)


I tried looking at other posts before posting myself but still haven't found a clear answer for this ...

I'm having some issues implementing side-wide PHP header & footer includes for my site.

I've created header.html & footer.html and include them on index.php using:

<?php include($_SERVER["DOCUMENT_ROOT"]."/includes/header.php");?>

.....page content here .........

<?php include($_SERVER["DOCUMENT_ROOT"]."/includes/footer.php");?>

This works fine until I get into sub-directories. such as otherstuff/index.php. Here is when the includes break because the DOCUMENT_ROOT changes to the new location.

One solution I've seen is using config.php to define a $site variable, which looks like a great solution, however I still would have to include config.php on each page which takes me back to the initial problem with subdirectories.

Another solution I've seen is setting a variable $includes_path to the includes path and then the whole link becomes absolute, but this again will cause issues in subdirectories.

Any suggestions?



10+ Year Member

Msg#: 3827470 posted 11:57 pm on Jan 15, 2009 (gmt 0)

Have a look at this thread: [webmasterworld.com ]

The second post by jatark is a very simple way to implement a template like system.


5+ Year Member

Msg#: 3827470 posted 4:47 am on Jan 16, 2009 (gmt 0)

thanks for your reply coho75

this looks like an interesting solution however I'm still not sure how I would handle the subdirectories issue I mentioned in the original post.

The solution for index.php would be:

$content = "pathtocontentpage.html";
include "template.php";

But for, let's say, directory otherstuff/index.php it would have to be something like:

$content = "pathtocontentpage.html";
include "../template.php"; //relative link

I want to find a solution where my call to the include template (be it header, footer or template.php) is the same.

Hope this makes sense ... any thoughts?


5+ Year Member

Msg#: 3827470 posted 11:11 am on Jan 16, 2009 (gmt 0)

hi i am trying to develop new webside ....please tell me ..it is possible in this sides...


10+ Year Member

Msg#: 3827470 posted 2:01 pm on Jan 16, 2009 (gmt 0)

If you do things the way that jatark outlined in the post I linked to, linking to the template should be quite easy.

For example, you could do something like this:

template.php - lives in the root directory

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

<title><?=$title ?></title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="<?=$keywords ?>">
<meta name="description" content="<?=$description ?>">

<style type="text/css">




<div id="header">


<div id="content">

<?=$content ?>


<div id="footer">



webpage.php - this can live anywhere


$title = 'This is a title';
$keywords = 'these, are, some, keywords';
$description = 'this is a description';


<p>This is some content.</p>


include_once ( '/var/www/htdocs/template.php' );


As you can see, it doesn't matter where your file resides, the path to the template will always be the same. I hope this helps.


WebmasterWorld Senior Member 10+ Year Member

Msg#: 3827470 posted 10:35 am on Jan 17, 2009 (gmt 0)

$_SERVER["DOCUMENT_ROOT"] is a server variable and your host most likely does not have it set. Try :


You'll most likely get nothing.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved