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

header redirect won't work in php tutorial? Please help...

Here's the code...

<?php

include("includes/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("includes/header.php"); ?>

11 Answers

Ah sorry... It just goes to a blank screen if I use the wrong id number eg: http://localhost:8888/PHP/shirt.php?id=4

Dan,

Makes sense. If the value isn't in your array, PHP will throw an error and stop running the script. Do you need help getting that fixed?

J.T.

Hey JT,

Oh wow... maybe I am really confused... My understanding of the way this should work is that if the id is missing altogether or wrong it should re-direct back to the shirts.php page.

As it stands at the moment as long as the url is correct it shows the right shirt directly off of the shirt.php page. Therefore the array is functioning fine... Isn't it?

(Yes I need help to fix... haha)

Cheers

Dan

Ok so after further playing around to see if I can get it to work... I can get the page to do the re-direct if and only if I remove this line of code... include("includes/products.php");

Of course then there is no array to pull id's from so the shirts page no longer works at all but the re-direct works. Solutions?

Actually Dan, I wasn't looking closely enough. Your code actually looks good.

I have no idea what products.php is supposed to look like, and perhaps I'm the wrong person to comment on this post, but I've tried running this code, and it seems to work okay...

// I'm pretending like I know what's stored in products.php, but I have no idea.
$products = array(0 => "Shirt", 1 => "Sweater");

if (isset($_GET["id"])){
    $product_id = $_GET["id"]; 
    if (isset($products[$product_id])) {
        $product = $products[$product_id];
    }
}

if (!isset($product)) {
    header("Location: http://google.com");
    exit();
}

$section = "shirts"; 

$pageTitle = $product["name"];

Perhaps there is an issue with your included files. Is this for a code challenge or for a tutorial? Again, I haven't done any PHP badges on Treehouse, but I am very familiar with PHP.

My apologies for the confusion earlier; I should have looked more closely.

Edit:

If you upload your project files to Dropbox, I'd be happy to take a look at your work to see that everything is in the proper directory and whatnot.

Thanks JT,

I've worked it out :)

It was an issue in products.php... There was a rogue space outside of the PHP. I'm guessing this caused output to be sent and thus it wouldn't do the re-direct.

Thanks for your help.

Cheers

Dan

WOOH TEAMWORK!

:D

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Hey @Dan,

That's exactly correct!

I'm planning to mention this in an upcoming video, but the best way to avoid that is to leave off the final closing PHP tag:

<?php

    // Lots of PHP code

When the PHP file ends without a closing PHP tag, it assumes there is one at the end. That way, you can't have a rogue space after the closing PHP tag.

Haha,

Believe it or not that wouldn't have helped because the products.php file had the rogue space before the start of the <?php...

In other words it was the dark side of the force in HTML space.

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

I always appreciate a good Star Wars reference.

I especially like them mingled with ancient Greek philosophy references. Or with cookie dough ice cream.

:~)

Haha,

With a surname like Solo I am constantly compelled to StarWarsIfy all sorts of things...

So the above could have just as easily have been "This is not the white space you are looking for!"