|
Subject:
|
PHP Beginner
|
|
Posted By:
|
Adam H-W
|
Post Date:
|
8/7/2006 10:11:33 AM
|
I"m the first to admit I don't know the first thing about PHP so when someone asked me to make a contact form for them in PHP then the first place I looked was for a script on the web which I found.
Howerver, I get the message:
Cannot modify header information - headers already sent by (output started at ....
What I'm trying to do is provide a thanks page once the form has been submitted - in the form I have this:
<input type="hidden" name="thanks" value="thanks.htm" />
However, the error message is referring to the include file with the following code:
/** * Redirect to thanks page or display posted * information in HTML template. */ if (isset($_POST['thanks']) and !empty($_POST['thanks'])) { if ($debug_mode != 'on') { header('Location: ' . $_POST['thanks']); }
The exact line is it refers to is:
header('Location: ' . $_POST['thanks']);
Please is someone able to help....
thanks alot
Adam
|
|
Reply By:
|
richard.york
|
Reply Date:
|
8/7/2006 10:52:22 AM
|
The error you are seeing is a result of sending HTTP headers after content has already been sent. As you output content in a PHP script, the server immediately sends that output to the browser. So, you can't send HTTP headers where the response body is to appear.
In this case, you'd be wasting resources anyway by sending a response that won't even be seen by the end user. If you do a redirect, you shouldn't output anything anyway, and you should exit the script after doing one. (via the exit keyword).
So, case-in-point, you can't have any script output before the redirect. You can't have any white space or HTML or anything at all before the opening <?php delimiter.
You can also control output using PHP's output buffer functions. See: http://www.php.net/outcontrol
These functions allow you to write output to a buffer so that it isn't sent right away. You consume more resources this way, but it allows you to work around errors like the one you saw.
HTH!
Regards, Rich
-- Author, Beginning CSS: Cascading Style Sheets For Web Design CSS Instant Results
http://www.catb.org/~esr/faqs/smart-questions.html
|
|
Reply By:
|
Adam H-W
|
Reply Date:
|
8/8/2006 12:45:19 PM
|
Richard
Thanks for your response. The reason he wants a thanks page is that he wants to track how many people actually send the form rather than just how many people just go to the page.
Is there any way I can just re-direct them easily to a thankyou page after the form has been sent?
thanks
Adam
|
|
Reply By:
|
veggivore
|
Reply Date:
|
8/29/2006 11:24:38 PM
|
Place the php block with the header() function at the header of your POST target script as previously suggested.
OR
Echo a dynamic meta refresh tag instead of the header() function.
OR
"include" the POST handling logic within the various thank you pages.
|