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 Basic PHP Website (2018) Listing and Sorting Inventory Items Item Details and Redirection

rob111
rob111
8,379 Points

Code block breaks my page

When I take this out the page loads, otherwise I get a blank white page...

Please see commented-out lines

<?php 
include("inc/data.php");
include("inc/functions.php");

if (isset($_GET["id"])) { 
    $id = $_GET["id"];
    if (isset($catalog[$id])) {
        $item = $catalog[$id];
    }
}
/* This code block breaks the page
if (!isset($item)) {
   header("location:catalog.php");
   exit;
}
*/

if (!isset($item)) {
  header("location:catalog.php");
  exit;
}

$pageTitle = $item["title"];
$section = null;

include("inc/header.php"); 
?>

<div class="section page">

    <div class="wrapper">

        <div class="media-picture">
        <span>
            <img src="<?php echo $item["img"]; ?>" alt="<?php echo $item["title"]; ?>" />
        </span>     
        </div>
    </div>
</div>

2 Answers

rob111
rob111
8,379 Points

My data.php file had the following

<!--Catalog items-->

<?php
$catalog....

Make sure not to have any extra spaces or lines. I removed

<!--Catalog items-->

and it worked.

rob111
rob111
8,379 Points

This simply tests if the value is False, if it is then execute the HEADER function.

if (!isset($item)) {
   header("location:catalog.php");
   exit;
}

To test this header function I should be able to do something simple, example

if (TRUE) {
   header("location:catalog.php");
   exit;
}

or even simpler just include the

header("location:catalog.php");

All results should trigger the header function to take the user to the catalog.php page. It never happens, I get an error instead.

Warning: Cannot modify header information - headers already sent by (output started at Study/Sandbox/Basic PHP Website/inc/data.php:3) in Study/Sandbox/Basic PHP Website/details.php on line 13

By the way this is the same thing that happens when I use the header function in a different file. I think these two are related: https://teamtreehouse.com/community/warning-cannot-modify-header-information-headers-already-sent-by-3

Simon Coates
Simon Coates
28,694 Points

unless you have buffering enabled, calls to header need to occur prior to any output. The html comment is output (ie. the response body), hence a failure would be expected.

rob111
rob111
8,379 Points

You're right Simon Coates I turned on output_buffering via .htaccess file and it solved the problem I was having. Yes the calls are being made after output.

Simon Coates
Simon Coates
28,694 Points

https://phpfashion.com/everything-about-output-buffering-in-php : "HTTP headers: Output buffering does not affect the HTTP headers, they are processed in different way. However, due to buffering you can send the headers even after the output was sent, because it is still in the buffer. However, you should not rely on this side effect, because there is no certainty when the output exceeds the buffer size." (i had some idea that the output may send itself if you exceed the buffer size and you could still get the the header problem)