There are several ways to do this. I'll list them in my personal preferred order from least to most effective:
1) Relative
2) include_path
3) Absolute
Relative: include by using dot notation (../file.inc). There are several disadvantages to using this notation. First, if you move files around, things will break. Second and most important, if you're like me you'll have nested includes. So a single file might include another file which includes another. The problem here is, referencing them relatively is always with respect to the original calling template. For example, if all my includes are in the lib directory, the example below will not work:
original.php -- include('./lib/include1.inc');
include1.inc -- include('./include2.inc');
because even though include1.inc and include2.inc are in the same directory, original.php is one level up, and the nested reference to ./include2.inc will fail. Confused? Don't worry. I suggest you don't use this notation. Its only good for portable apps, in which case you need to structure it such that your entire app will work with this notation.
include_path: In this case you use the include path configured with php. If you run phpinfo() find the value for the directive include_path. I think for our servers its the default value
.:/php/includes:/usr/share/php This means for includes it will look first in the current directory of the executing template (.), the /php/includes directory, then the /usr/share/php directory. Obviously, we don't have access to these directories, but luckily we can modify the directive in our .htaccess file. So for the above example to work, you can specify
/home/virtual/siteXX/fst/var/www/html/lib as your include_path and reference like this:
original.php -- include('include1.inc');
include1.inc -- include('include2.inc');
or if you specify
/home/virtual/siteXX/fst/var/www/html as your include path:
original.php -- include('lib/include1.inc');
include1.inc -- include('lib/include2.inc')
To change the directive, add the following in your .htaccess file:
Code:
php_value include_path '/home/virtual/siteXX/fst/path/you/can/access'
This is not my preferred method because if I switch hosts, I'm not guaranteed .htaccess permissions, so its not as portable.
Absolute: This is the method I use and is most portable. You begin your include path with "/" and give the absolute path to the file relative to the
server. But you don't want to hard code your server path. So do this:
Code:
include($_SERVER["DOCUMENT_ROOT"] . 'path relative to DocRoot');
So for the original example:
original.php -- include($_SERVER["DOCUMENT_ROOT"] . 'lib/include1.inc');
include1.inc -- include($_SERVER["DOCUMENT_ROOT"] . 'lib/include2.inc');
Man am I really verbose. Hope this helps.