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 Introducing Constants

David Wall
David Wall
11,088 Points

ROOT_PATH not working.

I can't include the header.php or footer.php using ROOT_PATH.

My config.php file is as follows(it's in the inc folder at the root):

<?php

define("BASE_URL", "/");
define("ROOT_PATH", $_SERVER["DOCUMENT_ROOT" . "/"]);

My index.php file is as follows(it's in the receipt folder at the root):

<?php

include_once("../config.php");
$pageTitle = "Thank you for your order!";
$section = "none";
include(ROOT_PATH . "inc/header.php");

?>

6 Answers

Hi David,

I don't think you have the correct relative path to your config file.

If your index file is in the "receipt" folder then you would need to go up one level then down into the "inc" folder.

include_once("../inc/config.php");

Try making that change as well as the one Chris pointed out and let us know what happened.

Jeremy Hayden
Jeremy Hayden
1,740 Points

Try echoing out your root path to see if its working

Chris Shaw
Chris Shaw
26,676 Points

Hi David,

You have a syntax error in your constant which needs to be resolved, currently you have the below which if you look closely you have concatenated your path onto the key name DOCUMENT_ROOT which should be on the other side of the closing square bracket.

define("ROOT_PATH", $_SERVER["DOCUMENT_ROOT" . "/"]);

Should be:

define("ROOT_PATH", $_SERVER["DOCUMENT_ROOT"] . "/");

Happy coding.

David Wall
David Wall
11,088 Points

Thanks for the replies. Thanks Chris. Sorry, i did have it that way before. I've redone this part so many times. It still doesn't work. Jeremy, i ran this:

<?php

include_once("../config.php"); echo ROOT_PATH; $pageTitle = "Thank you for your order!"; $section = "none"; include(ROOT_PATH . "/inc/header.php"); ?>

with this in the config:

<?php

define("BASE_URL", "/"); define("ROOT_PATH", $_SERVER["DOCUMENT_ROOT"] . "/");

The result is the words "ROOT_PATH" being echoed, not the actual path. So the ROOT_PATH isn't actually being defined...right?

Jeremy Hayden
Jeremy Hayden
1,740 Points

Just to check off the possibility that your config file does not have any errors, and is actually loading into main file, put a test echo at the end of it.

In fact, i would put 2 echos, a simple test, and the rootpath.

David Wall
David Wall
11,088 Points

Thanks Jason. That's definitely it!