homepage Welcome to WebmasterWorld Guest from 54.227.11.45
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
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
ultrasound0000




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

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




msg:3827504
 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.

ultrasound0000




msg:3827594
 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?

sikander




msg:3827745
 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...
thanks

coho75




msg:3827815
 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"
"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




msg:3828358
 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 :


echo $_SERVER["DOCUMENT_ROOT"];

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