PHP Includes and root variable

First off I googled a million pages and just could not find answers :shifty:.

I am trying to do this:

  1. In a configuration.php file I want to set up the path to my site
    I currently have this

 $siteroot = $_SERVER['DOCUMENT_ROOT'];

  1. In my other files I want to simply create blank containers and just include pages. So for instance in Index.php I want to include header.php, sidebar.php and footer.php.

I currently have this:
At the top of my page I am calling the configuration.php: <?php require_once (“includes/configuration.php”);?>

And in the spot where I want my header to appear I use this:

<?php include("$siteroot/includes/module-header.php"); ?>

And likewise for all the other includes - sidebar.php and footer.php.

This does not work, I get this error message:

[b]Warning[/b]:  include(D:/wamp/www//includes/module-header.php) [[function.include](http://localhost/imi/function.include)]:  failed to open stream: No such file or directory in [b]D:\\wamp\\www\\imi\\index.php[/b]  on line [b]105[/b]

Now I want this to work in my local server and seamlessly work in production.

I have no understanding and what code I am using was from one of my many google adventures… it apparently is incorrect.

$siteroot = rtrim($_SERVER['DOCUMENT_ROOT'],'/');

Try that, It looks like the problem lies with multiple directory separators:


If you would like to access that variable from within classes or functions its probably best to make it a constant.


I would suggest using a configuration file that where you would define the path:

define('YOUR_BASE_DIR', 'D:/wamp/www');

In your script, you would perform your include like this:

require_once(YOUR_BASE_DIR . "/includes/module-header.php");

Defining the path is always better than relying on what can otherwise be a moving target. Avoid relative paths whenever possible.

I will not be using it within classes or functions. My needs are to simply have a variable that I can use instead of a path.

The example you gave does not work - I yet get that error message.

The code I provided above will work as we use this as a standard practice for our system configuration.

That works - so when I take this to production, should the base directory change to" or the server path? If either one will work which is recommended?

When you go to production, you would use the server path to the file (the FULL path is advised).

Glad to see that worked.


Thank you. I have one more question.

So if I want to now use that variable (YOUR_BASE_DIR) in a link how would I call it?

For example i want to link to a file in information/faq/questions.php

How would i call the variable -
<a href=“<?php What php code ?>/information/faq/questions.php”>FAQ</a>

You wouldn’t, you would need to define a new constant:

define('YOUR_WEB_URL', '');

You would the write your link like this:

<a href=“<?php echo YOUR_WEB_URL; ?>/information/faq/questions.php”>FAQ</a>

Oh sweet! Thanks.

You don’t need a constant. / is already defined for you.

<a href=“/index.php”>
will always go to

While this is true, it is a best practice to not use or depend on relative URLs.

It’s not a relative url

<a href=“index.php”>

<a href=“/index.php”>

And to your other comment re php paths, it is actually best practice to depend on a relative path to define your root.

Defining the base URL is still recommended, particularly when you start scaling out to multiple servers or use a CDN. If you have no plans to do that, then sure, you could simply use the relative href.

It’s not recommended at all, and completely pointless to boot.

Okay - what be be your suggestions to my question?

this looks like what i’ve been looking at in this thread. i would need to include that configuration.php in every php file no matter what folder it is? that would be using relative paths i imagine… just getting utterly confused trying to solve a pretty similar problem…

How do you figure those aren’t relative? href=“index.html” == “./index.html” give me index of current directory.
If you are at and link to /some/dir/example.php it knows you mean not cause a single / means go to root then find x

For html you just use /
<a href=“/some/directory/img.php”> <– that url is the absolute path from the web servers root
<a href=“img.php”> <– that url is relative to what directory you are currently in

For php paths, you are no longer talking about the web server root, but the servers file system. So you need $root to be the path to your project, eg /home/hash/websites/myproject/
You can set this based off the DOCUMENT_ROOT, or based on an entry point in your script, or other ways, like hard coding a config file.

The first was relative for illustration of why the second wasn’t. I bolded the bit in your post which means absolute.