2

I have header.php and footer.php files which I include in all of my pages. A sample page looks like this -

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

<div id="content">
...
</div> <!-- content -->

<?php include($_SERVER['DOCUMENT_ROOT'].'/footer.php') ?>

Although this works well on the server, but when I test pages locally [ xampp on Windows 7 ], I get the following error message instead of the header, similarly for the footer -

Warning: include(C:/xampp/htdocs/header.php) [function.include]: failed to open stream: No such file or directory in C:\xampp\htdocs\f\index.php on line 1

Warning: include() [function.include]: Failed opening 'C:/xampp/htdocs/header.php' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\f\index.php on line 1

This makes testing very tedious as I have to upload to the server for every minor change.

Also, I dug around the WP code, and it uses a get-header() to display the header.php file. I could not completely understand the function. My site does not use WP.

What is the correct way for including header and footer files?

Akshay Tyagi
  • 27
  • 2
  • 8

3 Answers3

2

the correct way to include any file is the include() or require() function. Wordpress uses a get_header() function because the header is more then just 1 file, so they created a function for outputting it.

The problem you have seems to be a problem with the $_SERVER variable. It has been quite a long time since I've worked with PHP, but what I would advise you to do is just use relative paths. For example, if the header.php and footer.php files are in the same directory as your index.php, you can just do:

<?php include("header.php');?>

<div id="content">
...
</div> <!-- content -->

<?php include('footer.php') ?>
bigblind
  • 11,435
  • 13
  • 61
  • 111
  • I have tried this already, although it works locally, but on the server, header/footer are not displayed! Instead, can I change the value of $_SERVER ? – Akshay Tyagi Nov 21 '11 at 11:52
2

Simple and useful way (I use this in my all projects):

// find Base path
define( 'BASE_PATH', dirname(__FILE__) );

// include files
include BASE_PATH . '/header.php';
include BASE_PATH . '/footer.php';
beytarovski
  • 2,649
  • 5
  • 31
  • 41
1

It seems that $_SERVER['DOCUMENT_ROOT'] is pointing to C:\xampp\htdocs, while your scripts are at C:\xampp\htdocs\f\, check the value of $_SERVER['DOCUMENT_ROOT'] on your local environment.

edit:

<?php
$rootDir = "";
if(strpos($_SERVER['HTTP_HOST'],'localhost')===FALSE)
{
  //On Production
  $rootDir = $_SERVER['DOCUMENT_ROOT'];
}
else
{
  //On Dev server
  $rootDir = $_SERVER['DOCUMENT_ROOT'].'/f';
}

<?php include($rootDir.'/header.php');?>

<div id="content">
...
</div> <!-- content -->

<?php include($rootDir.'/footer.php') ?>


?>
Muhammad Ummar
  • 3,426
  • 4
  • 36
  • 68
  • Thanks! Also, how can I change the value of $_SERVER variable? – Akshay Tyagi Nov 21 '11 at 11:38
  • This variable is populated automatically by the PHP server. however it seems that you have created a subfolder `f` on your local environment for testing. please see my edited answer, it might help you. – Muhammad Ummar Nov 21 '11 at 11:58
  • Works! Saved me a lifetime. I create sub-folders locally because I work on multiple projects/websites simultaneously. I hope this is the right way to do so!? – Akshay Tyagi Nov 21 '11 at 12:27
  • Yes @Akshay Tyagi, for your problem this is correct way to deal with. – Muhammad Ummar Nov 21 '11 at 12:36