Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Site-Wide PHP Header & Footer Includes

php header footer



10:52 pm on Jan 15, 2009 (gmt 0)

5+ Year Member


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?


11:57 pm on Jan 15, 2009 (gmt 0)

10+ Year Member

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

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


4:47 am on Jan 16, 2009 (gmt 0)

5+ Year Member

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?


11:11 am on Jan 16, 2009 (gmt 0)

5+ Year Member

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


2:01 pm on Jan 16, 2009 (gmt 0)

10+ Year Member

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.


10:35 am on Jan 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

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


You'll most likely get nothing.


Featured Threads

Hot Threads This Week

Hot Threads This Month