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 trialJimmy Seeber
12,809 PointsPHP header(location) function not working
I'm working on the "Building a simple PHP application" deep dive and am stuck on the header function not redirecting my user to the 'Thank you' page. Here's my PHP code from the contact.php file setup in the videos.
<?php
if ($_SERVER["REQUEST_METHOD"]) == "POST" {
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
$email_body = "";
$email_body = $email_body . "Name: " . $name . "\n";
$email_body = $email_body . "Email: " . $email . "\n";
$email_body = $email_body . "Message: " . $message;
//To Do: Send Email
header("Location: contact-thanks.php");
exit;
}
?>
<?php
$pageTitle = "Contact Mike";
$section = "contact";
include('inc/header.php');
?>
Here is my 'Thank you' PHP code where it should end:
<?php
$pageTitle = "Contact Mike";
$section = "contact";
include ("inc/header.php");
?>
<div class="section page">
<div class ="wrapper">
<h1>Contact</h1>
<p> Thanks for the email! I’ll be in touch shortly.</p>
</div>
</div>
<?php include("inc/footer.php"); ?>
Final notes
I made sure all my files are properly placed in the htdocs directory and my end result is a blank page with no text. I tried changing the action attribute in the contact.php file to 'contact-thanks.php' from 'contact-process.php' and that sent me to the proper 'Thank you' page, but unfortunately doing it this way won't get my email message sent to the server. Any suggestions?
16 Answers
Omar Ramírez
183 PointsOk, I see...
Maybe you have printed out some text before the header() function, maybe a blank space.
Check the first line of your .php file does not have a blank space, something like this:
--- Blank space ---
<?php
Should be:
<?php
In other words, put the <?php tag on the top of your file.
Randy Hoyt
Treehouse Guest TeacherOnce you output any content (an echo statement, or white space or text outside of PHP), then you can no longer modify the header information.
Someone posted a similar problem recently, with a link to a good explanation on Stack Overflow:
Jimmy Seeber
12,809 PointsThanks Randy
Omar Ramírez
183 PointsI found a possible error in your file. Check the second line:
<?php
if ($_SERVER["REQUEST_METHOD"]) == "POST" {
This line should be:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
Only change the position of the close parenthesis symbol.
Hope this helps.
Jimmy Seeber
12,809 PointsOmar,
Thanks for your reply, and the correction you made was productive in getting the contact.php page to load (again) before submitting the form. However, once the form was submitted, the rest of the code must have had a second error in it because the page stayed on contact.php (as opposed to 'sending the email' and redirecting the user to contact-thanks.php).
I then finished the final video and made the last few modifications to combine all the files together in the contact.php page.
Here's my final code:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") { $name = $_POST["name"]; $email = $_POST["email"]; $message = $_POST["message"]; $email_body = "";
$email_body = $email_body . "Name: " . $name . "\n";
$email_body = $email_body . "Email: " . $email . "\n";
$email_body = $email_body . "Message: " . $message;
//To Do: Send Email
header("Location: contact.php?status=thanks");
exit;
} ?> <?php $pageTitle = "Contact Mike"; $section = "contact"; include('inc/header.php'); ?>
<div class="section page">
<div class="wrapper">
<h1>Contact</h1>
<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
<p> Thanks for the email! I’ll be in touch shortly.</p>
<?php } else { ?>
<p> I’d love to hear from you! Complete the form to send me an email.</p>
<form method="post" action="contact.php">
<table>
<tr>
<th>
<label for="name">Name</label>
</th>
<td>
<input type="text" name="name" id="name">
</td>
</tr>
<tr>
<th>
<label for="email">Email</label>
</th>
<td>
<input type="text" name="email" id="email">
</td>
</tr>
<tr>
<th>
<label for="message">Message</label>
</th>
<td>
<textarea name="message" id="message"></textarea>
</td>
</tr>
</table>
<input type="submit" value="send">
</form>
</div>
<?php } ?>
</div>
<?php include('inc/footer.php') ?>
Omar Ramírez
183 PointsJimmy, your new code it's working for me :)
Jimmy Seeber
12,809 PointsThanks, Maybe i have a local problem going on.
Omar Ramírez
183 PointsMaybe you can share your error_log here in the forum :D
Jimmy Seeber
12,809 PointsI don't have an error message... the page simply turns white and nothing else appears.
Omar Ramírez
183 PointsYou're working in a server or local server?
Jimmy Seeber
12,809 Pointslocal server, using MAMP.
Omar Ramírez
183 PointsIf your installation of MAMP does not show php errors, you may need to change a setting in your php.ini file. You can find this file in MAMP > conf > php5 > php.ini
Look for “display_errors = Off” and change to “display_errors = On”
After that you should restart your apache and mysql servers.
Check if with this you can see some kind of error. Share your results.
Jimmy Seeber
12,809 Pointshere is my error: Warning: Cannot modify header information - headers already sent by (output started at /Users/jimlseeber/Dropbox/Mamp/PHP/contact.php:2) in /Users/jimlseeber/Dropbox/Mamp/PHP/contact.php on line 19
Line 19 of code: header("Location: contact.php?status=thanks");
Jimmy Seeber
12,809 PointsWow! Omar Thank you very much!
so having a blank space before the php file breaks it from running?
Omar Ramírez
183 PointsNot always, in my experience I've only had this kind of problem when I use header function.
A good practice would be to avoid the blank space at the beginning of the file, just to avoid future problems.
Have fun with PHP!
Oliver Bradshaw
824 PointsI made the mistake of putting a comment on the top line of my code before the opening <?php ?> and that also broke my headers.
Jenny Swift
21,999 PointsJenny Swift
21,999 PointsThanks, you solved my problem!