Problem with $_SERVER['DOCUMENT_ROOT']


I have been working my way through “BUILD YOUR OWN DATABASE DRIVEN WEB SITE USING PHP & MYSQL” 4th Edition and all was going well until I got to Chapter 7. I just can’t seem to get the DOCUMENT_ROOT working. As I am working through this book, I am a novice at both PHP and mySQL.

I am using Dreamweaver to open and edit my PHP and HTML files. Also, I am using MAMP because I am on a Macintosh (OSX 10.6.2). I followed the set up instructions in the book, including using the Terminal to make some changes. (That seemed to me to be much simpler than the other set-up described in the book.)

In the earlier chapters which used include files in the same directory as the calling file, everything worked.

When I try to run the examples that have includes using $_SERVER[DOCUMENT_ROOT] then everything fails with the following error message (I have display_errors = On):

Warning: include_once(/Applications/MAMP/htdocs/includes/ [function.include-once]: failed to open stream: No such file or directory in /Applications/MAMP/htdocs/phpmysqlcode_archive/chapter7/admin/jokes/index.php on line 3

Warning: include_once() [function.include]: Failed opening ‘/Applications/MAMP/htdocs/includes/’ for inclusion (include_path=‘.:/Applications/MAMP/bin/php5/lib/php’) in /Applications/MAMP/htdocs/phpmysqlcode_archive/chapter7/admin/jokes/index.php on line 3

Warning: include(/Applications/MAMP/htdocs/includes/ [function.include]: failed to open stream: No such file or directory in /Applications/MAMP/htdocs/phpmysqlcode_archive/chapter7/admin/jokes/index.php on line 306

Warning: include() [function.include]: Failed opening ‘/Applications/MAMP/htdocs/includes/’ for inclusion (include_path=‘.:/Applications/MAMP/bin/php5/lib/php’) in /Applications/MAMP/htdocs/phpmysqlcode_archive/chapter7/admin/jokes/index.php on line 306

Notice: Undefined variable: link in /Applications/MAMP/htdocs/phpmysqlcode_archive/chapter7/admin/jokes/index.php on line 307

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /Applications/MAMP/htdocs/phpmysqlcode_archive/chapter7/admin/jokes/index.php on line 307

Hopefully someone who has worked through this book successfully with a similar set up to me (Dreamweaver and MAMP) will be able to help me.

Many thanks in advance


Temporarily add this line to find out what your server understands docroot to be for this page.

var_dump( $_SERVER ) ;

Generally though, most people would put their include files in the folder that PHP understands contains those files.


Or you can add paths to that one, go and look at that line in your PHP.ini file and you will see more instructions on how to do that.

Yeah me too I had same problems in that chapter of the book till I resorted to simply

include '../includes/';


and it worked but I want to try the suggestion… though

I am sorry, but I don’t understand, I am a complete novice.

var_dump($_SERVER) reported Applications/MAMP/htdocs as being the document root.

I tried putting my includes folder in: .:/Applications/MAMP/bin/php5/lib/php
and this didn’t work. So I really am in pickle, I seriously want to learn about php and mySQL, but this is a real stumbling block. I do realise that I can put my include files in the same directory as the file they are included in (this is what has been done up to now in the book) but using DOCUMENT_ROOT seems to open all sorts of possibilities, that I want to learn how to do it.

I did also try iTenzo’s suggestion of putting …/ before the include line, but that didn’t give me any success either.


I tried putting my includes folder in: .:/Applications/MAMP/bin/php5/lib/php

If you put an entire folder into your includes directory, lets say it was:


containing 2 files:


then to include database.php your include call should be:

include 'myfolder/database.php';

PHP “knows” to start in /Applications/MAMP/bin/php5/lib/php and you have then told it to traverse into myfolder and get database.php

I do realise that I can put my include files in the same directory as the file they are included in (this is what has been done up to now in the book)

PHP “knows” to include files you include in the same folder as your script thanks to this part of the directive in your php.ini file:

This first dot->. : /Applications/MAMP/bin/php5/lib/php

It might be tricky to get your head round at first, but once you realise how this works you will save yourself a lot of bother in the future.

You can add other folders to the include_path directive using other means, depending on your setup (and the set up you will find on rented server space).

You are right, it is a fundamental thing to grasp.

rookie warning 1. Make sure you only have one php.ini file on your system, search your HDD and check carefully!

rookie warning 2. If you make a change to your php.ini file directly, then make sure you stop/start your server to apply the changes (apache or whatever)

Read the manual pages about configuring include_path very carefully, experiment a bit till you “get it”.

Thanks very much cups.

That has given me heaps to think about and to experiment with. I have, in the past tried to use $_SERVER(DOCUMENT_ROOT) to set up an include file of my website menu, so that when I make a significant change to my website pages, I don’t have to make changes to the menu on every single page, just in the include file. This is another example of where I couldn’t get something to work, but in this case I just thought that this was an idea I had had that was impossible. Since Kevin Yank’s book said that these includes (using DOCUMENT_ROOT) should work, then I knew that they must do so. So I will persevere until I can get it sorted.

Thanks for your time and expertise, it is very much appreciated.


So using my example put your /myfolder/ and its contents into the htdocs folder.

now access it using:

include $_SERVER['DOCUMENT_ROOT'] . '/myfolder/database.php' ;

I dont want to labour the point but, he said labouring the point, depending on how your server is setup though, if you can navigate to:


Then your security is potentially compromised. Include files are generally served from somewhere outside of the document root.

If you setup is so that you have as your “homepage”:


Then you are probably OK.

Thank you again.

I can see I have a lot more to swot up than I first thought. Still, what else is there to do?

I can see the danger of people being able to see my php files, and I guess also maliciously altering them.



I have had to take a bit of time out and do some “real” work, which explains the delay for getting back to this.

The actual path to my includes folder is:


I found that if I put the includes file here:


everything worked as expected. Now I know (or rather assume) that that is the wrong place to put it. I am using Dreamweaver to work on my web pages, so each site I have is in a different folder inside htdocs. It would would not be possible to put every websites’ include file in htdocs. So my question is, how do I change my php.ini file so that I can get inside my site root folder, and perhaps even deeper than that (as in the case above, trying to get into folder Chapter7).

I am really sorry if I am being obtuse, but I just cannot wrap my head around this.


Step 1: Google “virtual host” or “mamp virtual host”. Follow instructions, this will allow you to have actual sites rather than folders in localhost.
Step 2: Once you’ve got that set up you will realise you can have a site in any folder on your hard drive - as long as apache can read it.
Step 3: var_dump($_SERVER[‘DOCUMENT_ROOT’]); and notice how the root is whatever folder you told it to be when setting up a virtual host.
Step 4: have a beer

Now for your includes you can do one of 3 things:

  1. use a relative path based on your doc root or current working directory/file
  2. set the php include path - php will then go through all directories listed looking for your file
  3. use an absolute path based on your file system (not recommended at all)

Hope that helps

Thanks Hash

I was beginning to wonder about having all my websites in htdocs (I have six in there). I had though of scrapping MAMP and setting up the alternative Server setup in Kevin’s book, but I am not that savvy with mucking around with my System, so I will stick with MAMP for now and try this new solution.

I have looked at the a couple of pages that tell me how to set up virtual hosts in MAMP. Looks fairly straightforward, so long as I back up my config files before I start, right!

So hopefully, this will be the last time I will have to ask about this.

Many thanks for all the help I have had on this. Sitepoint Forums are tremendous.