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

SERGIO RODRIGUEZ
SERGIO RODRIGUEZ
17,532 Points

header redirect not working on localhost.

HI! I'm using Randy's code to test how everything should work. However the header redirect that should take you to shirts.php if the get id is not valid or if doesn't exist, is not working.

The URL keeps being the same and I end up in a completely blank page. Any ideas?

This is the code I'm dealing with:

<?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();
}

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

Hi Sergio,

Can you please post the code in question? If you haven't posted code on the forums yet, check out this helpful link: How To Post Code

SERGIO RODRIGUEZ
SERGIO RODRIGUEZ
17,532 Points

Y updated the post to display the code. Thanks!

Alex Trzeciak
Alex Trzeciak
468 Points

I cannot get this to work either. I have verified with echo that the code reaches the redirect but again does not fire.

<?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(); 
}
$section = "shirts"; 
$pageTitle = $product["name"];
include("inc/header.php"); ?>

6 Answers

Your redirect is working fine. Is the location changing in the address bar of your browser? With just that code you gave me, everything is working. But, if it is changing to shirts.php and you are getting a blank page, that leads me to believe the problem is with shirts.php, not with the code posted above.

SERGIO RODRIGUEZ
SERGIO RODRIGUEZ
17,532 Points

The url is not changing to shirts.php. It stays in whatever faulty url I type.

With just that code alone, I was able to get a successful redirect to my own made up shirts.php. Is shirts.php in the same directory as the page that this code is from?

SERGIO RODRIGUEZ
SERGIO RODRIGUEZ
17,532 Points

yes it is. I believe it has something to do with my localhost settings

SERGIO RODRIGUEZ
SERGIO RODRIGUEZ
17,532 Points

yes it is. I believe it has something to do with my localhost settings

Kian Woo
Kian Woo
3,097 Points

I had the same problem... Mine was fixed by removing the white space before the first <?php

Les Campbell
Les Campbell
26,071 Points

I have the same problem a lot. So much so that it's ingrained as my first go to for debugging.

Removing white space at the first <?php resolved the blank page issue for me.

I gave this code in the begining of the document and works fine

<?php ob_start(); ?>

but i think it isn't correct method.

Were you able to fix this problem, Sergio?

SERGIO RODRIGUEZ
SERGIO RODRIGUEZ
17,532 Points

Not really but I'm pretty sure it has something to do with my local settings.

Quite possibly, Sergio. As I said before, the code itself works perfectly when using XAMPP. I don't know what server software you're using right now, but XAMPP is my favorite. I would give it a try.

Zachary Hale
PLUS
Zachary Hale
Courses Plus Student 10,946 Points

Yeah I'm having the same problem. Must be something with MAMP.

Daisho Komiyama
Daisho Komiyama
15,906 Points

You might need to check if there is a blank space before <?php include("inc/products.php"); I had the same problem, but mine had one tiny blank space before that code. I removed it then the file started working. It is worth checking it if it is still not working!