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

"Introducing Constants" is killing my soul

I have followed Randy's video on using constants, and it just isn't working. My project is in a subfolder of htdocs, called, shirts4mike.

Here is my config file in the includes folder. The end php tag is left out per Randy's instructions:

<?php

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

Here is my index file inside of the receipt folder:

<?php

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

$pageTitle = "Thank you for your order!";
$section = "none";
include(ROOT_PATH . "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="<?php echo BASE_URL; ?>shirts.php">Shirts Listing</a> page again.</p>

        </div>

    </div>

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

Here is the header file from the includes folder:

<html>
<head>
    <title><?php echo $pageTitle; ?></title>
    <link rel="stylesheet" href="<?php echo BASE_URL; ?>css/style.css" type="text/css">
    <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Oswald:400,700" type="text/css">
    <link rel="shortcut icon" href="<?php echo BASE_URL; ?>favicon.ico">
</head>
<body>

    <div class="header">

        <div class="wrapper">

            <h1 class="branding-title"><a href="./">Shirts 4 Mike</a></h1>

            <ul class="nav">
                <li class="shirts <?php if ($section == "shirts") { echo "on"; } ?>"><a href="<?php echo BASE_URL; ?>shirts.php">Shirts</a></li>
                <li class="contact <?php if ($section == "contact") { echo "on"; } ?>"><a href="<?php echo BASE_URL; ?>contact.php">Contact</a></li>
                <li class="cart"><a target="paypal" href="https://www.paypal.com/cgi-bin/webscr?cmd=_cart&amp;business=Q6NFNPFRBWR8S&amp;display=1">Shopping Cart</a></li>
            </ul>

        </div>

    </div>

    <div id="content">

Thank you very much for taking the time to look. I've tried so many different path variations at this point that I am about to lose it.

Andrew Shook
Andrew Shook
31,709 Points

Ben what exactly is the problem? Is the header.php not being loaded, are the links not working?

Hi, Andrew,

None of the constant links are working.

Andrew Shook
Andrew Shook
31,709 Points

How are they not working? For example, in your nav, does what do the link href's look like? Are they just "file.php" or does it show the base url?

Andrew Shook
Andrew Shook
31,709 Points

Add this to the top of your "Thank you" page:

ini_set('display_errors',1);  error_reporting(E_ALL);

This will turn on php error reporting. Refresh the page and tell me if anything shows up at the top.

Hi, Andrew,

In answer to your first question, the constants are not linking to the pages where I think they are being directed. For instance, none of the CSS is loading. The header and footer are not loading. The receipt page is not loading. And when I click on links they don't work.

For the error reporting, where should I be looking for the output? I placed the code you provided at the top of my 'thank you' page, and then reloaded the page. But I'm not sure how I'm supposed to receive the results.

Thanks!

Andrew Shook
Andrew Shook
31,709 Points

If there is any kind of error or warning in your php script that code should output it at the top of the page. Is anything showing up on you "thank you" page or are you getting a white screen?

EDIT: Is the receipt folder located in the root folder of you site?

When I put the code inside of the PHP block, I get nothing. When I run the code before the PHP, I get the same code appearing at the top of the screen. Like this:

ini_set('display_errors',1); error_reporting(E_ALL);
Andrew Shook
Andrew Shook
31,709 Points

What I meant is, with or without my code, do you get a white screen or just the page without the header and footer?

Maybe I'm not executing it correctly. But after putting the code at the top of the index ('thank you') page, located in the receipt folder, I then type in http://localhost/shirts4mike/receipt/. and the full page does appear with header and footer.

The error reporting code you provided.

Andrew Shook
Andrew Shook
31,709 Points

That shouldn't have fixed anything, it just prints errors to the screen. Take the code out and see if it still works.

Still works. I don't know what to say. I guess I must have fixed something without knowing it. But I've been careful to keep track of everything I've done. Weird. Maybe I've been working too hard.

Thanks, though!

1 Answer

Aaron Lafleur
Aaron Lafleur
10,474 Points

I had super headaches around this time in the course, too. My issue was that I forgot to include my config file in my htdocs/index.php. One thing I can think to suggest, and correct me if I'm wrong, is in your config file, instead of '/shirts4mike/', try dropping the first /, so 'shirts4mike/'. I THINK i remember something about the BASE_URL and ROOT_PATH includes the first /.

Aside from that, when I messed around with using shirts4mike as my root folder things got all screwy and I had trouble getting all my files in the right place.

Good luck!