So “Yes” to your first and second question, the latter is long-hand for what you originally wrote, which is just fine.
For relative versus absolute, my personal opinion is to use absolute paths, but that’s purely my opinion as it just always works. The second you need to move a file to another directory/sub-directory, relative paths puke (rightfully so, if you fail to update their relative path inclusions).
Now for [fphp]include_once[/fphp] versus [fphp]include[/fphp], include_once simply tells PHP, I only want this file included once during the execution of this script. Ignore all other attempts at including it.
Finally, your .htaccess question, you can set an environment variable and then retrieve that variable within your PHP script using $_ENV[‘variable_name’]
The double period just means ‘go up one level from the current directory’. There’s no problem with the way you’re currently doing it.
Let’s say you have a script, A, which includes B, and B includes C. If A then includes file D, which also includes C, then you could end up with a problem (fatal errors trying to redeclare functions etc). Using include_once just means that PHP won’t try to include a file more than once.
Definitely use $_SERVER['DOCUMENT_ROOT'] for file includes.
Using HTTP_HOST will give you a URL rather than a path, which means that:
you can only use urls with include() if the ‘URL include wrappers’ option is enabled in PHP
you can only include files within the web root folder
included PHP scripts would be executed as a separate request, and only the output returned to the calling script
You could create an environment variable in you .htaccess like this:
SetEnv APP_ROOT '/var/www/include/'
and then access it in PHP like this:
include($_SERVER['APP_ROOT'] . 'abc.php');
Although this doesn’t really give you much unless you have a long include path.
That’ll error out because its looking for requiredFile.php at the same level as index.php. Ok, so we add lib into the path, right? Now what if I have another file, somewhere else that uses myClass.php. It might break again.
Try DIR as the prepender. It will return the path of where the current PHP file resides, not where the execution began. This makes it much more dynamic.
Thanks guys! I am added all sub pages tommorow and will let you know if I run into any probs. Thank you for the detailed explanations. I think I got most of it now.
<?php include $_SERVER['DOCUMENT_ROOT'].'/inc/toplinks.php'; ?>
it’s always looking for the inc folder directly off the root - hence the DOCUMENT_ROOT bit. Even if the include is in another file located in another folder it will still look for the inc folder off the root.
‘DOCUMENT_ROOT’ The document root directory under which the current script is executing, as defined in the server’s configuration file.
Assuming /var/www/public_html, and everything being routed through index.php at this level, every include you prepend with $_SERVER[‘DOCUMENT_ROOT’] will be set to the same level as index.php. That’s great and all, but not the best practice.
Let’s say your publishing a package to packigist to be used in existing projects. Now you have to tell that developer to place this project in ‘inc’ within their project or the includes you have in your files wont work.
DIR and dirname(FILE) as a prepender break this couple. Now your linking to a file based on its location from the current files location, rather than where the request is coming from.