0

I have created a contact form in PHP. The form works fine and sends the message but when I try to redirect users to my thank you page I receive a headers already sent message.

My form starts:

<form name="myform" id="myform" class="form-foot" action="/receiving.php" method="post">

And the PHP code for receiving.php is:

<?php 

$name = $_POST['name']; 
$email = $_POST['email']; 
$message = $_POST['message']; 

if(isset($_POST['submit']))

{
$from_add = "mysite@mysite.com"; 

$to_add = "mysite@mysite.com"; 

$subject = "Contact Form";

$message = "
Name:$name \n 
Email: $email \n 
Message: $message \n 

$headers = "From: $from_add \r\n";
$headers .= "Reply-To: $from_add \r\n";
$headers .= "Return-Path: $from_add\r\n";
$headers .= "X-Mailer: PHP \r\n";


if(mail($to_add,$subject,$message,$headers)) 
{
    Header("Location: /thanks");
} 
}


?>

How can I redirect users to www.mysite.com/thanks after successful submission?

Alan Carr
  • 322
  • 2
  • 9
  • 25
  • Here is the solution http://stackoverflow.com/questions/4459204/seeing-cannot-modify-header-information-error-when-attempting-to-redirect – Kevin Jun 11 '13 at 19:44
  • 1
    When you are done going through the duplicate you may want to read a bit on the topic of header injection – PeeHaa Jun 11 '13 at 19:45

1 Answers1

0
  1. Make sure there is NO output before header calls. Any echos or any space before <?php will break it.

  2. header("location:...") needs an absolute URI.

    header("location: http://www.site.tld/thanks");
    
  3. header("location: /thanks"); should have an exit immediately following.

    header("location: http://www.site.tld/thanks"); exit;
    
000
  • 26,951
  • 10
  • 71
  • 101
  • 1
    It is not about the putting `exit` after the header, or using absolute URI's I think, it is about output buffering. – samayo Jun 11 '13 at 19:45
  • 1
    The output buffering issue was only one problem. If I can help with more things, then I am solving the problem and not only the symptoms. – 000 Jun 11 '13 at 19:47
  • Hi Joe, I've tried that and I'm still getting the same error: Warning: Cannot modify header information - headers already sent – Alan Carr Jun 11 '13 at 19:50
  • Then you're not showing us your full code. – 000 Jun 11 '13 at 19:50
  • In general, headers (e.g. "Location") should start with a capital, even though they will work when in lower-case. – halfer Jun 11 '13 at 19:50