Top 3 Include Paths in PHP
Just like my holiday this year – I’m going all inclusive…
Those of you who are PHP developers or who simply work with PHP as the best way to create websites know how important includes are in the overall structure of a website, how the maintenance time decreases and the tidiness of your coding increases after effective use of <?php include(‘this function’); ?>.
However there are times when using php includes can be a bit frustrating, especially when working in a complex folder structure. Includes are always relative, they include files that are local to themselves. This means that if you want to include your header.php from three-folders-deep you would need to type the following:
<?php include('../../../includes/header.php'); ?>
This can get tiresome – especially when you are four or file folders deep and your include strings are becoming astronomical. You can’t even link from root like you can with a header function:
<?php header('Location: /'); ?>
Nope. Not going to happen.
The Solutions
There are several solutions to this dilemma, some of which may work better than the others for your personal situation but I will show you my top 3 so you can decide for yourselves.
1. Relativity Script
This relativity script is something that I made while working on a back-end for an e-commerce website design. The front-end was simple enough but the back-end was more complicated. I decided to try out an automated path finding script that is useful as a way of finding your path and returning to root for all sorts of situations – includes included.
The script is as follows:
$place = $_SERVER['PHP_SELF'];
$place2 = explode('/', $place);
$path = "";
for ($i=0; $i<(count($place2)-2);$i++){
$path .= "../";
}
include($path.'includes/header.php');
This has many benefits but the downsides are that you really need to include this at the top of every page (or at least in the first include which you would have to include relatively). Not good enough really…but might be useful for some things.
2. PHP set include path
The set include path in php lets you define the include path for that file. this is a cleaner way to number 1 but is limited to includes only – which means you wont be able to link say $path.”/images/” to go to your root images folder (though you shouldn’t need to as you should just use /images/).
To use the php set include path enter the following code at the top of your pages:
// Works as of PHP 4.3.0
set_include_path('/inc');
// Works in all PHP versions
ini_set('include_path', '/inc');
However we are stuck again by the fact that this needs to be done on every page or in your first relatively included include.
This is where number 3 steps in and becomes my personal favourite.
3. .htaccess set include path
The beauty of the .htaccess file is that it is a broad setting. A site wide setting if you will. You can create an .htaccess file on the root of your site and it will affect every php file therein. What I found most spectacular about this method is that you can literally change your include path from folder to folder. Take my shop backend as the example. We have a front-end which needs template files including from the root layer. We can do this by creating an .htaccess file – adding the following code and uploading:
php_value include_path /var/www/vhosts/mywebsitename.co.uk/httpdocs/includes/
This will make all includes in the website include from that folder – you can now simply type in <?php include(‘header.php’); ?> no matter how many folders deep you are!
It doesn’t stop there however. If like me, you had an admin section that is using separate includes, perhaps for functions.php or similar, you can create another .htaccess file inside that folder which will override the previous one for that folder and recursive folders within that directory!
php_value include_path /var/www/vhosts/mywebsitename.co.uk/httpdocs/admin/includes/
Hey presto! You have now created a way to define really simple includes!
Oh and by the way – you know that long document path at the beginning of the htaccess script? Simply echo out some php to get that path:
<?php echo $_SERVER['DOCUMENT_ROOT']; ?>
And you now have everything you need.
Thanks for visiting our magento blog on e-commerce web design and please stay in touch with the magento fox!
