First off, try specifying your include
files using the document root: $_SERVER['DOCUMENT_ROOT']
... this will always direct to the public root folder so it will be the same no matter where in the site you are - this is particularly useful when you start including files which themselves include
/require
other files... in that scenario an included script which itself included sub-scripts relatively (../load.php
|subfolder/load.php
, etc) would never work from two different levels in the folder hierarchy.
<?php include($_SERVER['DOCUMENT_ROOT']."/load.php"); ?>
Then for resources that are being called in html, simple make them all relative to the public root as well:
<?php echo '<img src="/image.png"/>'; ?>
Notice the /
in front of the image src
? That will always call the image no matter what folder the currently displayed page is in.
Alternately, you could just specify resources absolutely (though this isn't ideal as if the site domain changes it will break the resource links):
<?php echo '<img src="http://www.yoursite.com/image.png"/>'; ?>
Either way, no matter what script you're calling the include from it will always grab it from the correct location (no need to add ../
etc in your include
calls) and images/scripts/css/etc will all work no matter what page they're pulled into