Forum Moderators: coopster
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?
The second post by jatark is a very simple way to implement a template like system.
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?
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.