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 trialAdam Smallman
4,182 Pointsphp Redirect
Hello, I need a way to redirect to a different page within an if statement. I have tried header (Location: filename.php); but it doesn't work.
Any ideas??
Thanks
Here is my code:
php(
<?php
$password = $_GET['passw'];
$passTry = $_GET['askPass'];
if($passTry == $password){
echo"YOU GOT IT!";
}else{
echo"You are wrong";
}
?>
)
4 Answers
James Andrews
7,245 Pointsheader("Location: filename.php\n\n");
The header header function requires quotes around it as it requires a string. It also requires 2 trailing \n return carriages.
Adam Smallman
4,182 PointsHay, James thanks for your help. I have tried header and dose not work here is my code
(
<?php
$password = $_GET['passw'];
$passTry = $_GET['askPass'];
if($passTry == $password){
echo"YOU GOT IT!";
}else{
echo"You are wrong";
header("Location: test.php");
}
?>
)
James Andrews
7,245 Points<?php
/*
$password = $_GET['passw'];
$passTry = $_GET['askPass'];
if($passTry == $password){
echo"YOU GOT IT!";
}else{
echo"You are wrong";
*/
header("Location: test.php");
/*
}
*/
please try this and tell me what happens.
James Andrews
7,245 PointsAlso, please in your php.ini file make sure display_errors = On
so that you see any errors, and then restart apache. by default it's usually set to display_errors = Off
.
Adam Smallman
4,182 PointsOk, I copied the code exactly and all it shows you is the echo"You are wrong" it's still not redirecting.
Where exactly do I place the display_errors = On?
Thanks
Adam Smallman
4,182 PointsOk I got the display_errors = On working is saying this -->
You are wrong Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/test/test.php:3) in /Applications/MAMP/htdocs/test/test.php on line 35
James Andrews
7,245 Pointsah ha! you can't echo before passing header, because you have already sent output to the browser, remove the echos from your original code and it will redirect.
Adam Smallman
4,182 PointsOk, so all I have to do is delete the echo's like this?
<?php ini_set('display_errors', 'On');
$password = $_GET['passw'];
$passTry = $_GET['askPass'];
if($passTry == $password){
}else{
header("Refresh: 10; url=index.php");
}
?>
Im still getting the same error
Shawn Gregory
Courses Plus Student 40,672 PointsHello,
You can use HTML's redirect method to redirect to another page.
<META http-equiv="refresh" content="5;URL=http://www.website.com">
The content is where you can specify the length of time the page waits till it redirect (in this example is 5 seconds) and what URL you want to redirect to. Hope this helps.
Cheers!
Adam Smallman
4,182 PointsHey, thanks for the help Shawn, but it didn't work. Take a look at my code to see if I am doing it right.
(
<?php
$password = $_GET['passw'];
$passTry = $_GET['askPass'];
if($passTry == $password){
echo"YOU GOT IT!";
}else{
echo"You are wrong";
print"<META http-equiv="refresh" content="5;URL=http://www.website.com">";
}
?>
)
Shawn Gregory
Courses Plus Student 40,672 PointsJames,
Explain why you need to use \n\n at the end of the argument for the header function as I've never had to use \n\n in all the instance that I've used the function. Also keep in mind that there maybe white-space in his code that he is overlooking that is preventing him from using the header function (of course it would display the headers already sent error). The html redirect way allows you to redirect at any place in your scripting regardless of if headers were sent or not plus it gives you the option to tell the browser how long you want to wait till you redirect. But in all practical purposes I would try to use the header function over the html redirect.
Cheers!
James Andrews
7,245 PointsI've been doing php programming since version 2.0, and at that time header required \n\n at the end of it. They may have changed it since then so it's not required. The reason for the \n\n is because the http protocol required it at the end.
James Andrews
7,245 Points<?php
$password = $_GET['passw'];
$passTry = $_GET['askPass'];
if($passTry == $password){
echo"YOU GOT IT!";
}else{
header("Location: test.php");
}
Adam Smallman
4,182 PointsI'm doing something really wrong, I'm still getting the same error message.
Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/test/test.php:3) in /Applications/MAMP/htdocs/test/test.php on line 35
This is all of the code
<!DOCTYPE html>
<?php session_start(); ?>
<html> <head> <title>CITYlife.com</title> <meta charset="UTF-8"> <link rel="stylesheet" href="css/reset.css" > <link rel="stylesheet" href="css/text.css" > <link rel="stylesheet" href="css/stylesheet.css">
</head> <body>
<div class="wrap">
<?php
ini_set('display_errors', 'On');
$password = $_GET['passw'];
$passTry = $_GET['askPass'];
if($passTry == $password){
echo"YOU GOT IT!";
}else{
header("Location: test.php");
}
?>
</div> <!-- END wrapper -->
</body>
</html>
James Andrews
7,245 PointsYou can't have any content before the call for header. No echo at all. This is saying line 35 so somewhere before line 35 you are echoing something. This should be at the very top of your page before any other content is printed to the browser.
Adam Smallman
4,182 PointsI moved the PHP code to the top of the page above all the html and it worked :)
Thanks for ll your help
Adam Smallman
4,182 PointsAdam Smallman
4,182 PointsI want the redirect code to be where the echo's are