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

Walter Allen
seal-mask
.a{fill-rule:evenodd;}techdegree
Walter Allen
iOS Development with Swift Techdegree Student 16,023 Points

header function not working when include statement is part of previous code?

Here's my code:

<?php   

    include('inc/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');
        exit;
    }

    $pageTitle = $product["name"];
    $section = "shirts";
    include('inc/header.php');
?>

The problem is, the header function is not working. However, when I comment out the include function, the header function works fine. There doesn't seem to be any offending ode in the included file as I've commented everything out and the header function is still not called... Any ideas? Any help is greatly appreciated. :)

1 Answer

Walter Allen
seal-mask
.a{fill-rule:evenodd;}techdegree
Walter Allen
iOS Development with Swift Techdegree Student 16,023 Points

Okay... answering this one for myself. Fiddled with it for about an hour and a half before posting my question and then found the answer within fifteen minutes of posting. Ugh. ;)

According to the php documentation, with the header function, "It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called." So, I went back to my included files and took out as much whitespace as I could. Sure enough, apparently, there was some whitespace being output before the header() function was sent. It's all working well now. Go figure.