Wrox Programmer Forums
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 7th, 2006, 10:11 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 347
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Adam H-W
Default PHP Beginner

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




 
Old August 7th, 2006, 10:52 AM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

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
 
Old August 8th, 2006, 12:45 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 347
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Adam H-W
Default

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

 
Old August 29th, 2006, 11:24 PM
Registered User
 
Join Date: Aug 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Severe beginner question, can't install PHP NoPantsJim Beginning PHP 1 November 15th, 2007 02:03 AM
Beginner in PHP vinu123 BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 1 April 18th, 2007 09:23 AM
Beginner Tilak_1 C# 2 January 21st, 2005 11:19 PM
Beginner [email protected] Beginning PHP 4 December 23rd, 2004 03:22 PM
Beginner NEED HELP!! nvillare VB.NET 4 September 18th, 2003 02:44 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.