0

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?

2
  • It just means your include path in php.ini isn't set to check the webroot: php.net/manual/en/ini.core.php#ini.include-path Commented Sep 15, 2013 at 2:16
  • PHP is usually sandboxed and not allowed to access files inside the file system. Check the php.ini file for open_basedir! Commented Sep 15, 2013 at 2:16

2 Answers 2

2

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';
Sign up to request clarification or add additional context in comments.

3 Comments

I can see my answer here somewhere, but being a PHP newbie I don't quite see how to implement it. Can you correct me where I'm wrong?
$_SERVER would be a constant that would be defined differently by my host and by my local xampp setup. Locally, is this 'localhost'? DOCUMENT_ROOT would be defined in php.ini and therefore different on my host server and on my local setup. On host, this would be the 'public' directory that pages must be located in? Locally, would be the "UnderConstruction" folder that I'm developing in (defined in my local php.ini)? Also: online testing is in /public/UnderConstruction, a subdirectory, whereas it IS the root folder locally? So I must change code online?
Okay, everything is working locally. I'm guessing the remote solution is just to put a copy of the includes folder in the root directory and pages in my UnderConstruction folder will find it. Thanks.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.