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 trialSERGIO RODRIGUEZ
17,532 Pointsheader 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"); ?>
SERGIO RODRIGUEZ
17,532 PointsY updated the post to display the code. Thanks!
Alex Trzeciak
468 PointsI 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
Marcus Parsons
15,719 PointsYour 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
17,532 PointsThe url is not changing to shirts.php. It stays in whatever faulty url I type.
Marcus Parsons
15,719 PointsWith 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
17,532 Pointsyes it is. I believe it has something to do with my localhost settings
SERGIO RODRIGUEZ
17,532 Pointsyes it is. I believe it has something to do with my localhost settings
Kian Woo
3,097 PointsI had the same problem... Mine was fixed by removing the white space before the first <?php
Les Campbell
26,071 PointsI have the same problem a lot. So much so that it's ingrained as my first go to for debugging.
George Akinian
17,615 PointsRemoving white space at the first <?php resolved the blank page issue for me.
radekfabisiak
8,064 PointsI gave this code in the begining of the document and works fine
<?php ob_start(); ?>
but i think it isn't correct method.
Marcus Parsons
15,719 PointsWere you able to fix this problem, Sergio?
SERGIO RODRIGUEZ
17,532 PointsNot really but I'm pretty sure it has something to do with my local settings.
Marcus Parsons
15,719 PointsQuite 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
Courses Plus Student 10,946 PointsYeah I'm having the same problem. Must be something with MAMP.
Daisho Komiyama
15,906 PointsYou 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!
Marcus Parsons
15,719 PointsMarcus Parsons
15,719 PointsHi 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