Welcome to WebmasterWorld Guest from 54.227.5.198

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Site-Wide PHP Header & Footer Includes

php header footer

     

ultrasound0000

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

5+ Year Member



Hi,

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?

coho75

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.

ultrasound0000

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?

sikander

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...
thanks

coho75

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"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<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">

</style>

</head>

<body>

<div id="header">

</div>

<div id="content">

<?=$content ?>

</div>

<div id="footer">

</div>

</body>
</html>

webpage.php - this can live anywhere

<?php

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

$content=<<<EOF

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

EOF;

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.

thecoalman

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 :


echo $_SERVER["DOCUMENT_ROOT"];

You'll most likely get nothing.

 

Featured Threads

Hot Threads This Week

Hot Threads This Month