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

I am following the "Enhancing a Simple PHP Application" tutorial and have encountered a problem with the Introducing Constants video. After adding the BASE_URL links to both the receipt/index.php and to the inc/header.php, the index.php page load great, but when I click on the links an error message appears.

I am working on a WAMP testing server and the files are located in a file called phpTreehouse on the root. Therefore, my config file looks like this:

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

I cannot for the life of me figure out why this is not working. Any suggestions would be much appreciated.

Cheers, Aleks

5 Answers

Anywhere you BASE_URL or ROOT_PATH in a page, you'll need to also include the config file.

If you use these in the header file, say to link in CSS or something (which I do) you will need to include the config file on every page.

The reason you can't include the config file in the header is because the path to the config file can change. This is why you must include in in the index file, and/or the file in that subfolder. Let me explain.

Let's pretend this is what your folder structure looked like.

\ INC / -->config.php

\ SHIRTS / -->index.php

\ PRODUCTS / -->index.php \ SEARCH / -->index.php

In the index file of the shirts subfolder your path to your config would look like: require_once("../inc/config.php");

But in the index file of the search subfolder, that is nestled inside of the products subfolder, your path would look like: require_once("../../inc/config.php");

So just keep that in mind.

Are you including the config file above in each file using a relative URL?

I have included:

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

to receipt/index.php. Do I need to add it to other pages (ex. shirts.php), in order for the link to work?

Okay. I just added it to shirts.php and the link worked. In that case, I assume in order for the links to work I need to convert the rest of the pages to the same format?

Thank you for the quick reply :)

Cheers, Aleks

Thank you so much Kevin. Back on track. :)