Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I'm trying to figure out how to get my php includes to work properly from files in different directories.

For example, I have a header.php for my layout which is included in the index.php - it's located in the root/ directory. Then, I have a page located at root/pages/etc/lib.php - It also needs to use the header.php. Now, obviously I could do a include "header.php" for the index and a include "../../header.php" for the lib.php file, but then I run into another issue...

My header.php also has a php include of it's own, and the relative path is relative to the original page (index.php or lib.php) and NOT the header.php.

Is the solution to simply give the full URL for the includes or is there another way to do this? Can I do URL for any included php files and it will work?

share|improve this question
    
You should have a constant variable that is NOT relative. Then have all your includes prefixed with this variable. – Dave Chen Jun 1 '13 at 22:19

2 Answers 2

Most servers don't allow http wrappers in includes. You can do it with the document root:

$_SERVER['DOCUMENT_ROOT']

You can put it in a var and use it with every include. (Or define it)

share|improve this answer

One way to do it:
include $_SERVER['DOCUMENT_ROOT'].$pathToYourIncludeFileOnTheServer;

Another is to set the php include_path in the php.ini. If you include a file php looks in the directories specified in include_path if a file is matching the include and if so chooses that file.

Including directly via http://www.server.com/include.php is often a problem since allow_url_include is sometimes disabled in the php.ini if you are using a free webhosting or something comparable.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.