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

The "header("Location: shirts.php");" code will not fire for me.

This question is in regards to the "Redirecting invalid shirt ID" lesson in "Build a simple PHP application".

The "header("Location: shirts.php");" code will not fire for me. I can echo things onto the page with this code block:

if (!isset($product)) {
    echo "this is where my redirect should go"; 
    exit();
}

This echos back the string above; however I only load a blank page when I change it to:

if (!isset($product)) {
    header("Location: shirts.php");
    exit();
}

Any ideas? Anyone else having this issue?

2 Answers

Hi Stephen,

header() won't work if you have any output to the page before it. This can include any echo statements or any whitespace before your opening <?php tag.

Check and make sure you don't have any output to the page.

You can post the entire php block that the code above is in if you're still having trouble.

Is your opening php tag the very first line of the file and you don't have any spaces in front of it?

Is "products.php" in your main folder with all the other pages? It should be in the "inc" folder unless it gets moved there later.

Try doing a var_dump of the products array after it's included to make sure that's working properly.

If you have a valid $_GET id do you correctly get a shirt display?

Jason,

There is nothing preceding the opening <?php tag.

I have now moved "products.php" into the inc folder and updated the references accordingly.

I can var_dump($products) to ensure the array is included-that works fine.

Each individual shirt page loads properly. I have completed every step in the video for testing twice. All of the test strings echo out properly based on the URLs.

But for some reason, the header() function will not fire. It results in a completely blank page - view source info is completely blank. It's as though the header() function is ignored and exit(); fires.

I'm stumped...

Sorry, I'm stumped too.

What's the url in the address bar when you get the blank page?

Did you check your products.php to make sure you don't have any echo or var_dump's that you might have had from earlier debugging or testing?

Also, make sure the opening php tag in "products.php" also doesn't have any whitespace before it. If you include a file that has output in it then that would also cause header() not to work in "shirt.php"

The angle bracket should be line 1, column 1 in both files.

<?php opens in line 1, column 1 in both files.

Any URL that has either no or an invalid id will throw a white page.

URLs that have a correct id will bring up the corresponding page normally.

I just cleared my cache and tried it again, still nothing. :\

Jason,

The code now works. Honestly, I have no idea why...but it now fires correctly every time.

The issue must have been in the included products.php. I appreciate your help with this.

Happy Thanksgiving!

Glad it's working. Weird but maybe it was you clearing the cache.

You have a Happy Thanksgiving too!

Jason,

Thank you for getting back to me.

I have double checked the code. The block looks like this:

<?php include("products.php"); 
if (isset($_GET["id"])) {
    $product_id = $_GET["id"];
    if (isset($products[$product_id])) {
        $product = $products[$product_id];
    }
}
if (!isset($product)) {
    header("Location: shirts.php");  // THIS CODE DOES NOT FIRE
    exit();
}
$section = "shirts"; 
$pageTitle = $product["name"]; 
include('inc/header.php'); ?>

I even downloaded the instructors files and pasted them in over my code. The only difference you will notice is in structure. I don't have main pages in the inc directory -- just the header and footer.