The include
statement includes files relative to the script executing include
, which is why you're seeing this issue. You have a couple of options available to you:
- You can preface your include path with something like
$_SERVER['DOCUMENT_ROOT']
, which if you're using Apache, will reference your document root directory. So something likeinclude( $_SERVER['DOCUMENT_ROOT'] . "/includes/header.php" );
will appear to the script as an absolute path, so it will work if called from various places in your structure, but still be portable (note that it would require being called from a web server, this won't work if you're using your scripts through CLI). - You can create some sort of placeholder at the root of your directory structure, and work your way up until you find it, then consider this the root and make your include statements relative to this. This will be more portable and work in CLI mode, but it will be slightly more resource intensive.
- You can use different include statements depending on where you're script including the include file is located, such as
include( "includes/header.php" );
if the file you're including is in theincludes
directory of the directory you're currently in, orinclude( "../includes/header.php" );
if theincludes
directory is in the parent directory of the script being run.