Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Raquel Redondo
Raquel Redondo
6,494 Points

Using $_SERVER["DOCUMENT_ROOT"] for including files

I just watched "Absolute Server Paths" video (second stage of Enhancing a Simple PHP Application course) and I don't get why we are supposed to use $_SERVER["DOCUMENT_ROOT"] instead of a simple root-relative link type for including files. . Example, why should we write this:

include($_SERVER["DOCUMENT_ROOT"] . '/inc/header.php');

instead of this:

include('/inc/header.php');

How is it any different from using root-relative urls on anchor links:

<a href="/shirts.php">Shirts</a>

2 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi Raquel,

The most basic reasoning I can give is as follows:

  • Absolute paths are based on your user permissions
  • they also refer to the root directory of the server.
What does that mean exactly?

When we think of absolute paths unlike a website url paths in PHP are UNIX based therefore they will refer the furthest level up in the server our logged in user can access, for instance if you were to set your website's public folder up under the root user it would then have access to the most top level folder of the server which is /.

We don't want that as it opens up a host of security issues, instead when running under a user we're normally restricted to a folder within /home/<username> which sandboxes our account and prevents us from being able to leave to a level above, in the case of absolute include paths it's not a good idea to rely on just an absolute path since server configurations can change and folder structures won't be the same unless you set them up yourself and use the same software across them all.

DOCUMENT_ROOT to the rescue

When we use $_SERVER["DOCUMENT_ROOT"] PHP will automatically get the base folder path for the current working directory the executed file exists within, what this does is provides us a solid base to work from as no matter which server we upload our code to it will work without errors saying the file couldn't be found which generic absolute paths will cause.

At the end of the day you don't have to use an absolute path nor do you have to use $_SERVER["DOCUMENT_ROOT"] but for this course it just works, PHP offers a lot more ways you can get the current working directory through constants and built-in functions.

Hope that helps.

Raquel Redondo
Raquel Redondo
6,494 Points

Thank you very much. Things are a lot clearer now.