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 Build a Simple PHP Application Integrating with PayPal Redirecting Invalid Shirt IDs

Graham Patrick
Graham Patrick
8,294 Points

PHP Error on if and else statements

Good morning guys, So im trying to finish up this track and im pulling this error on this part, my code looks good but i seem to be pulling an error, not sure why

error is as follows : Warning: Cannot modify header information - headers already sent by (output started at /home/content/97/10391297/html/morbygraphics/mikeshirts/inc/products.php:47) in /home/content/97/10391297/html/morbygraphics/mikeshirts/shirt.php on line 9

this is my code from line 1: <?php include ("inc/products.php"); if (isset($_GET["id"])) { $product_id = $_GET["id"]; if (isset($product[$product_id])) { $product = $products[$product_id]; } } if (!isset ($product)) { header("Location: shirts.php"); exit(); }

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

Thanks in advance!

4 Answers

Robert Bojor
PLUS
Robert Bojor
Courses Plus Student 29,439 Points

Hi Graham,

When using header() to redirect the user to another address, be it internal or external, you are not allowed to have any other output on the page before the header function call.

Judging by your errors you already have some output coming from products.php, line 47, when calling the header function in shirt.php, line 9.

If you can move the if clauses and ensuing header call before the products.php include, everything should work correctly. As I remember from this course the products.php should only include an array of available shirts. If you added any other code inside you should move that out.

Graham Patrick
Graham Patrick
8,294 Points

Hi Robert

Thanks for the response Lines 40 to 47 on that page are

$products[108] = array( "name" => "Logo Shirt, Orange", "img" => "img/shirts/shirt-108.jpg", "price" => 25, ); ?>

When i remove line 47 it just stops the site working

Robert Bojor
Robert Bojor
Courses Plus Student 29,439 Points

Check and see if products.php has an empty line either at the beginning or at the end of the file. I've had this problem in the past and it drove me crazy until found.

Another problem I encountered was with stray "invisible" characters at the beginning of the file, generated by changing the file encoding while working under windows and saving to a linux server. Removing the first line of code and rewriting it manually did the trick.

Graham Patrick
Graham Patrick
8,294 Points

Hi robert

Thanks for that , i had two lines underneath which i removed and all works fine! Real pain in the Butt

Thanks again

Van Doan
Van Doan
32,285 Points

Thank you so much for the post, Robert! You're a life-saver. It was ONE stupid invisible space at the end of my products.php file.