I am restructuring a site, and use "include" files for much of the page content. I keep these files in a folder called "includes", and therefore have files like includes/header.php, includes/footer.php etc. In the past, I had always called the included files by using full http paths to the include files... ie include 'http://www.mysite.com/includes/header.php'; I know I should have been using the internal file path instead.... but now I also read that I should be using relative url's rather than full path urls to the include files. I don't know what the issue is, and am not that great at php, but can hack around. The problem I see, is that these include files are going to be getting called from all kinds of pages in different directories and different levels. I don't want to change the include path as I create pages in different directories. What am I supposed to do? What is the correct way to call include files sitewide regardless of where a particular file is in relation to the includes folder? Give me a code snippet if you can. Thanks
It's always best to use an internal relative path for including additional code. I'm not even sure how you're using a remote URL for an include path, but it doesn't sound like your include files are very secured. If the include statement has to use a remote URL, that means it has to first query the server for the file to be included (in this case your server will query itself), and then wait on the response before it can finish parsing your script. You should also consider using require_once instead of include if you aren't already. This ensures an included file won't be included multiple times from other included files. Here is an example piece of code I use to include a global (bootstrapped) file for all my pages: define('ROOT_DIR', './'); require_once(ROOT_DIR.'includes/global.php'); Code (markup): I use this same piece of code in all PHP files, and I only change the path for ROOT_DIR to reflect the actual script's location. For example, if I had another script located in a subdirectory from the root dir, I would use this code instead: define('ROOT_DIR', '../'); require_once(ROOT_DIR.'includes/global.php'); Code (markup): Hope that helps.
Ok, thanks. Now, what is the preferred way to place paths to images within an include file. Again, the page that ultimately displays the file could be in one of many different directories or subdirectories. Like if my logo is in an /image folder and my include file is in the /include folder, and the page using the include file is in /subdir/subdir/page.php. I assume this has to be by also using the full path to the root folder... but my host includes my account username in the path and for some reason I have always been reluctant to have that info display in the page code... but I guess that's normal. Thanks
If you mean within the HTML, you only need the specify the path relative to the web root. If your images are in domain.com/images, the HTML only needs to use src="http://domain.com/images/image.gif". If you want to use relative path, i.e. src="images/image.gif", you can use the <base> tag. For example, within the <head> of the skin, you can put <base href="http://domain.com/"> HTML: Then regardless of how many subdirectories deep within your site the current page is, you only need to specify the src relative to the base rather than the current page. Afaik, absolute paths are preferred - with a relative path, there's multiple locations in which PHP has to look for to find the file. Obviously there's not that issue with absolute paths. I'm sure there's probably not much difference in it either way though. Regarding the code posted above, that's not really a great solution as you still need to include some path info in every single file. Sure, you don't need to include the path for every include/require statement but you still need to change that definition in every file. If you're running Apache and allow htaccess to use SetEnv commands, you can place the root path in a single .htaccess file and access that through PHP. For instance, in the root directory of the script, the .htaccess would contain: SetEnv INIT_FILE /home/username/domains/domains.com/public_html/scriptsubdir/includes/init.php Code (markup): Then in every PHP file, you only need: empty($_SERVER['INIT_FILE']) ? die('Unable to initialise') : require $_SERVER['INIT_FILE']; PHP: The init.php script can then setup all needed paths with: define('ROOT_PATH', dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR); PHP: Or any variation of that principle. The fewer files that have to have a hardcoded path, the better.
Well, it's a step up to use internal relative paths than actual HTTP URLs. The thing I like about relative paths though, is that it's not environment dependent. So wherever a "root directory" is moved, it will always work, regardless of PHP or Apache configurations. I've even had problems in the past where absolute paths didn't work or weren't available (due to security), so this is the method I prefer.