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 Enhancing a Simple PHP Application Cleaning URLs with Subfolders Using Absolute Server Paths

Greg Kaleka
Greg Kaleka
39,021 Points

$_SERVER["DOCUMENT_ROOT"] directory does not include my site.

$_SERVER["DOCUMENT_ROOT"] didn't work for me. I got an error message that looked like this:

Warning: include(/Library/WebServer/Documents/inc/header.php): failed to open stream: No such file or directory in /Users/Greg/Sites/shirts4mike/receipt/index.php on line 7

I took a look in the terminal to see what was in /Library/WebServer/Documents, and it included two PoweredByMac icons and index.html.en, which is the It works! message.

Is my configuration totally screwed up? I already had to adjust my root relative URLs, since they're at /~greg/shirts4mike.

Thanks in advance for any insight!

1 Answer

In what way are you using

<?php
$_SERVER['DOCUMENT_ROOT']
?>

It looks like you are having an error related to a PHP include on line 7 of your index file, I'm assuming that's where you include your header.php file? Have you tried just:

<?php
include('inc/header.php');
?>

or perhaps

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

that should fix your relative file paths.

Greg Kaleka
Greg Kaleka
39,021 Points

Mike,

Thanks for the response. Following along in the course, I previously had the first code you suggested, which was working fine:

<?php
include('inc/header.php');
?>

The instruction has you change it to the following for more flexibility:

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

Which is essentially equivalent to your code. The problem appears to be that none of my documents are actually in the DOCUMENT_ROOT directory.

Note, I do not have MAMP installed. Rather, I installed PHP on its own, and configured Apache, both using the terminal. I'm sure there's something about the way I have my configuration set up that's not pointing to the right place...

I would guess it's a configuration issue with Apache, then. You should be able to change the document root in Apache though to get it working.