Easy php question.... I think!

Hi all,

I’m trying to call my websites header which resides in the root directory to other files in other folders.

Root directory

  • header.php

root/folder/needsheader.php

I cant succesfully get my needsheader.php file to display the header that resides in the root directory. Is there anyway to do it other than using the absolute file name.

include "http://www.website.com/folder/header.php";

Is there a better way than my example?

Thanks, Norris

The problem is that you’re including a webpage, being requested as such.
You need to include the file directly:


include "folder/header.php";
// or, if you're working in - say - 'folder2' (which resides next to 'folder'):
include "../folder/header.php"

Or you could use the absolute path to the file:


include "/var/www/folder/header.php";

Note that this last version will be problematic if you ever change the folder layout of your website or move to a different host, as you may have to change all your include statements.

You can also use a combination of dirname() and FILE (or DIR depending on your php version) to create an absolute path based on the current file if you prefer that

Hmmmm. none of those are working. I did make a mistake about where I was trying to put the file.

I’m trying to grab file header.php and add it into root/folder/folder/file.php

How do I grab something from the root folder. Right now my include statement is

include "header.php";

Its not pulling the header from the root folder.

Thanks for all your help.

Try adding a / before header.php or


include($_SERVER['DOCUMENT_ROOT']."/header.php");

Thanks Rguy. I’ve been racking my brain over how to do this simple thing. Now I only have edit 160 pages with that code for the footer and header. Thanks. i might get to see sun light today.

You are welcome

Do you have a good search and replace in your IDE or if you use Linux you

find . -name '*.html' | xargs perl -pi -e 's/oldtext/newtext/g'. 

BTW no talking about Sun today… unfair, we have blowing snow, drab skies, and very cold - no Sun here :frowning: