Reputation: 1
I'm using a PHP include for my header/navigation, but some pages are unable to find that file. I'm using a root-level link
<?php include("/includes/masthead.php"); ?>
but pages outside of the root folder are not locating the masthead file. For instance:
index.php locates and processes masthead.php just fine; adopt/adoptadog/php returns an error saying the file does not exist.
Is this because PHP doesn't process links in the same way that HTML does, so my root-relative link just isn't being interpreted by the php include function?
I'd like to be able to have a root-relative link work in my include statement so that statement can go into an Expression Web template. The template seems to write the same address in every page regardless of the location of the page. Maybe it doesn't see a link within a PHP tag the same way it does in HTML--I don't know.
I hope this is clear. Any help?
Upvotes: 0
Views: 108
Reputation: 37065
Try putting this at the top of your script:
set_include_path(get_include_path() . PATH_SEPARATOR . $_SERVER['DOCUMENT_ROOT']);
Or go to your php.ini file and update the include_path
directive to always search your webroot directory.
Upvotes: 0
Reputation: 173642
You could use the document root as the anchor:
include $_SERVER['DOCUMENT_ROOT'] . '/includes/masthead.php';
If your included files are one level outside of the document root, you just need to move with it:
include dirname($_SERVER['DOCUMENT_ROOT']) . '/includes/masthead.php';
If you have a script that gets loaded by all your pages that resides on the document root itself, you can use a constant:
define('PROJECTDIR', dirname(__FILE__));
To include the mast head:
include PROJECTDIR . '/includes/masthead.php';
Upvotes: 2