$_SERVER['DOCUMENT_ROOT'] question please?

<?php include $_SERVER['DOCUMENT_ROOT'].'/inc/toplinks.php'; ?>
  1. Thats what I’m using atm. But there are many variants floating around on the web. Some with (…) and etc. Is this the cleanest way to write it?

  2. Also chris corier writes it like…

<?php 
   $path = $_SERVER['DOCUMENT_ROOT'];
   $path .= "/common/header.php";
   include_once($path);
?>

I assume thats the long hand version. But I see some say include once. If you leave that out will it include more than once?

  1. And which one of these is better and why? I am just using it for simple includes and saving myself the trouble of relative paths.

<?php include($_SERVER[‘DOCUMENT_ROOT’] . ‘/include/abc.php’) ?>

vs

<?php include($_SERVER[‘HTTP_HOST’] . ‘/include/abc.php’) ?>

  1. And another one! Is there such a thing as a htaccess line or two that would set the root so i don’t have to in each php include?

Thanks

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’]

Hi bud,

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.

Have you thought about using the front controller pattern? If you route all your requests through a single script, any common files only need to be included in one place. There are some good router libraries and [URL=“http://www.slimframework.com/”]micro-frameworks available that you could use.

Keep in mind what document root is. It is the script in which the parent PHP file is being executed from. So now take this into consideration:


#index.php
require_once $_SERVER['DOCUMENT_ROOT'] . '/lib/myClass.php';


#lib/myClass.php
require_once $_SERVER['DOCUMENT_ROOT'] . '/requiredFile.php';

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.

Huh. I don’t get that. If I say…

<?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.

From PHP.Net

‘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.

Let me know if your still not following