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

 Tony Mikel
 Tony Mikel
10,661 Points

Root-relative path not working from receipt directory

<?php echo $_SERVER["DOCUMENT_ROOT"] . "/shirts4mike/"; ?> <?php

//require_once();

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

    <div class="section page">

        <div class="wrapper">

            <h1>Thank You!</h1>

            <p>Thank you for your payment. Your transaction has been completed, and a receipt for your purchase has been emailed to you. You may log into your account at <a href="http://www.paypal.com/us">www.paypal.com/us</a> to view details of this transaction.</p>

            <p>Need another shirt already? Visit the <a href="../shirts.php">Shirts Listing</a> page again.</p>

        </div>

    </div>

<?php include("shirts4mike/inc/footer.php"); ?>  

I get the following:
Warning: include(/shirts4mike/inc/header.php): failed to open stream:

If I understand root-relative correctly, that warning means, it's looking in my htdocs directory -> shirts4mike -> inc ->header.php..

Is that correct?

 Tony Mikel
 Tony Mikel
10,661 Points

I would like to add I'm on Windows 7, using XAMPP. My XAMPP installation is the following:

F:\xampp\htdocs

With my shirts4mike site at the following path:
F:\xampp\htdocs\shirts4mike

I would like to ask, does xampp automatically turns it into F:/xampp/htdocs? Because that's what I get when I echo my $_SERVER["DOCUMENT_ROOT"]

1 Answer

Lin Lu
Lin Lu
29,171 Points

I think there's the first forward slash shouldn't be there. Just like how you include the "foot.php" at the last line of your code, you should include the header file like this: include("shirts4mike/inc/header.php");, simply remove the first forward slash from your code.

 Tony Mikel
 Tony Mikel
10,661 Points

I haven't been able to work on it any since I posted this question but I will later today. I do have a question, if I use include("shirts4mike/inc/header.php"); wouldn't excluding the / make it a relative link and no longer work? Since the site is in a folder called shirts4mike in my root directory, wouldn't it look in a path resembling the following: shirts4mike/shirts4mike/inc?

Lin Lu
Lin Lu
29,171 Points

No, actually, it will be the directory relative to the file where function include("shirts4mike/inc/header.php"); is called.

 Tony Mikel
 Tony Mikel
10,661 Points

Neither include("shirts4mike/inc/header.php"); or include("/shirts4mike/inc/header.php"); work for the header or footer, respectively, in my receipt directory.

Lin Lu
Lin Lu
29,171 Points

I think I have found the problem. You need to modify the BASE_URL and ROOT_PATH constant variables in the config.php file.

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

After I changed this file, I was able to see the page.