1

SOLVED - it was a problem with my encoding: see: Fix incorrectly displayed encoding on an html document with php

I am missing something obvious here. My process.php form processor is not redirecting properly.

My code (Short version)

header(sprintf("Location: %s", $insertGoTo));
header("Location: index.html");    
echo 'y u no work?', $insertGoTo;

The 'y u no work' line echos, and the page does not refresh (or throw any errors)

I took this code from another page, which works correctly. Its on the same hosting package, so there sholdnt be problems with the php version or anything link that. Any insight would be greatly appreciated.

Long Version of my code:

<?php ob_start(); ?>
<?php
$submitdate = date("F j, Y");

if (((isset($_POST["email"])) and ($_POST["email"]!="")) and ((isset($_POST["favorite-pet"])) and ($_POST["favorite-pet"]==""))) {

    $name = $_POST["name"];
    $name = str_replace("\r", "", $name);
    $name = str_replace("\n", "", $name);

    $phone = $_POST["phone"];
    $phone = str_replace("\r", "", $phone);
    $phone = str_replace("\n", "", $phone);

    $email = $_POST["email"];
    $email = str_replace("\r", "", $email);
    $email = str_replace("\n", "", $email);

    $comments = $_POST["comments"];
    $comments = str_replace("\r", "", $comments);
    $comments = str_replace("\n", "", $comments);


    $message = $submitdate."\r\n\r\n";
    $message .= "Name: ".$name."\r\n";
    $message .= "Phone: ".$phone."\r\n";
    $message .= "Email: ".$email."\r\n\r\n";
    $message .= "Comments: \r\n";
    $message .= $comments."\r\n\r\n";

    $find = array("/bcc\:/i","/Content\-Type\:/i","/cc\:/i","/to\:/i");

    $email = preg_replace($find, "", $email);
    $message = preg_replace($find, "", $message);

    $recipient = "example@example.com";


    /* subject */
    $subject = "Contact Inquiry";

    $toaddress = $recipient;
    $toname = $recipient;

    $fromaddress = $email;
    $fromname = $name;

    $headers = "From: " . $name ."<" . $email .">\r\n";
    $headers .= "Bcc:  example@example.com\r\n";
    $headers .= "Reply-To: " . $email . "\r\n";
    $headers .= "Return-path: " . $email;


    if(mail($recipient, $subject, stripslashes($message), $headers)) {

        $insertGoTo = "sent.html";
        header(sprintf("Location: %s", $insertGoTo));

    } else {

        echo "Message was not sent <p>";
        exit;

    }

} else {

    $insertGoTo = "index.html";
    header(sprintf("Location: %s", $insertGoTo));
    header("Location: index.html");    
    echo 'y u no work?', $insertGoTo;
}
?>

EDIT

Ok, I changed a couple things, but still having problems

I changed the top couple lines to:

<?php ob_start();
error_reporting(E_ALL);

And the header() lines to:

$insertGoTo = "index.html";
header(sprintf("Location: %s", $insertGoTo));
exit;

I am still getting this error:

Warning: Cannot modify header information - headers already sent by (output started at .../process.php:1) in .../process.php on line 76

Community
  • 1
  • 1
Zach Lysobey
  • 14,959
  • 20
  • 95
  • 149
  • try adding `error_reporting(E_ALL)` at the beginning of your script, to see what it outputs. – kontur Jan 02 '12 at 19:50
  • Why do you send out the Location twice – tacos_tacos_tacos Jan 02 '12 at 19:50
  • Is `$recipient = example@example.com";` just a typo? (This missed opening quote) – Joe Jan 02 '12 at 19:51
  • 1
    Add `exit;` after you call the `location()` function. – Ayman Safadi Jan 02 '12 at 19:51
  • This might be a long shot but if you remove the `?> – Kieran Jan 02 '12 at 19:52
  • Remove 2 `header(Location: ...)` calls and add `exit` call after a header function call. – anubhava Jan 02 '12 at 19:52
  • well that's certainly usefull! Cannot modify header information - headers already sent by (output started at .../process.php:1) in ...process.php on line 77 y u no work?index.html – Zach Lysobey Jan 02 '12 at 19:53
  • WOW, this question is getting a lot of attention very quickly. Is there a button to hide the question from the public while I sort some stuff out lol – Zach Lysobey Jan 02 '12 at 19:54
  • Can you check your HTTP response headers using Firebug/Chrome/curl? My guess is that the headers are not being sent. This may be caused by some unwanted whitespace appearing before your `ob_start` call which means the headers will not be sent. – cmbuckley Jan 02 '12 at 19:58
  • ob_start: http://php.net/manual/en/function.ob-start.php I am not really familiar with this function. I am beginning to think that this is where the problem lies however. [edit] I commented it out and the error remains – Zach Lysobey Jan 02 '12 at 20:04
  • Cargo cult programming: How about `header("Location: $insertGoTo");` This is not C. You can put variables into strings... – Marc B Jan 02 '12 at 20:08
  • Oh sorry, totally forgot that it was a core function. Is process.php being executed directly or called in with an include/require from another file? – Kieran Jan 02 '12 at 20:09
  • @Marc B - true. Never heard of 'Cargo cult' before. That will make for a good wikipedia read later. That being said, this is not the source of the error. – Zach Lysobey Jan 02 '12 at 20:19

4 Answers4

2

This is your error message:

Warning: Cannot modify header information - headers already sent by (output started at .../process.php:1) in .../process.php on line 76

And I'll just post it as answer, because people obviously ignore it otherwise: How to fix "Headers already sent" error in PHP

Community
  • 1
  • 1
mario
  • 144,265
  • 20
  • 237
  • 291
  • Not sure if you are posting a solution. I am aware that you cannot send headers after starting output. The problem I'm having is that I cannot figure out where this output happens. I realize I have a lot of learning to do. I will read the content of your link. – Zach Lysobey Jan 02 '12 at 20:10
  • You could have just googled that error message. This has been answered a bajillion times before. – mario Jan 02 '12 at 20:11
  • Is this not an appropriate question to ask? I appreciate the value of SO, and do not want to water it down with bad questions. I did however read several similar questions (including the one in your answer) none of which helped me determine why my code is failing. – Zach Lysobey Jan 02 '12 at 20:23
  • I was under the impression that ob_start() was supposed to create an output buffer which would stop this error from happening. Any ideas why that may not have worked? – Zach Lysobey Jan 02 '12 at 20:25
  • Oh my god. That's both answered in the link I gave you. Twice. – mario Jan 02 '12 at 20:26
  • 1
    I am sorry Mario. I now realize how in depth your answer was. I did not read the full content of the answer, as I immediately saw the problem in the questions code. I will vote to delete this question. That being said; I really do not appreciate your condescension. I have tried to be polite in asking for help, and wouldn't have except after having spent a couple hours trying to sort this out. I'm learning every day, and one day hope to contribute well thought out answers to common problems like the one in your link. But I won't be a jerk. – Zach Lysobey Jan 02 '12 at 20:34
  • wouldn't let me delete... I voted to close as exact dup. – Zach Lysobey Jan 02 '12 at 20:36
  • Then I apologize for the tone. The topic is just extraordinarily boring and also annoying after so many duplicates. – mario Jan 02 '12 at 20:44
  • Thank you for the apology. No hard feelings. Marking your answer as correct. (it was the UTF-8 BOM thinggy) – Zach Lysobey Jan 02 '12 at 20:45
0

throw an exit after the redirect.

Rooster
  • 9,954
  • 8
  • 44
  • 71
  • ok I put that in, but to no effect – Zach Lysobey Jan 02 '12 at 20:01
  • hmmm. your throwing an error somewhere else then. i would just try to pay attention to everypossible thing I could in the firebug console and NEt tabs. Are you running under a framework or just simple php? – Rooster Jan 02 '12 at 20:24
  • simple php. Im a chrome user, and not too familiar with this kind of problem. I'll swing over to FF and see if Firebug can help me out – Zach Lysobey Jan 02 '12 at 20:26
0

Your error suggests to change the first three lines from this...

<?php ob_start(); ?>
<?php
$submitdate = date("F j, Y");

To this...

<?php
ob_start();
$submitdate = date("F j, Y");

Either that or the ob_start function is echo'ing something (which will cause headers already sent).

Kieran
  • 2,554
  • 3
  • 26
  • 38
0

Check, if you have any whitespace or other "printable" characters before your opening <?php tag in your file process.php

As the error states: (output started at .../process.php:1)

Dan Soap
  • 10,114
  • 1
  • 40
  • 49