#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0

    Warning: Cannot modify header information - headers already sent


    I have a form in the index page of my site with the following php action attributed to it:

    Code:
    <form action="php.php" method="POST" enctype="multipart/form-data" onsubmit="return regchk()" name="myform">
    When users submit their information, the field data gets POSTed to a url. After that, I need them to get redirected to a thank you page.

    There is something clashing with the line
    PHP Code:
    header('Location: thankyou.html'); 
    Which is causing it to produce the following error:

    Code:
    Warning: Cannot modify header information - headers already sent by (output started at /home/jsanders/public_html/xxx.com/php.php:3) in /home/jsanders/public_html/xxx.com/php.php on line 48
    Any suggestions? Here is the code in the php.php file.

    PHP Code:
    <html>
        <body>
            <?php
            
            
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
                
    error_reporting(E_ALL E_NOTICE);
                
    $fname urlencode($_POST['fname']);
                
    $lname urlencode($_POST['lname']);
                
    $email urlencode($_POST['em']);
                
    $st urlencode($_POST['st']);
                
    $zip urlencode($_POST['zip']);
                
    $ip urlencode($_POST['ip']);
                
    $ts urlencode($_POST['ts']);
                
    $phone urlencode($_POST['pn']);
                
    $agree urlencode($_POST['custom1']);
                
    $agree2 urlencode($_POST['custom2']);
                
    $city urlencode($_POST['ct']);


                  echo 
    $fname "<br />";
                  echo 
    $lname "<br />";
                  echo 
    $email "<br />";
                  echo 
    $st "<br />";
                  echo 
    $zip "<br />";
                  echo 
    $ip "<br />";
                  echo 
    $ts "<br />";
                  echo 
    $phone "<br />"; */


                
    $url "http://dailygobblet.com/13/cr-rtp-richalissa?fname=$fname&lname=$lname&em=$email&st=$st&zip=$zip&ip=$ip&ts=$ts&pn=$phone";

                
    $ch curl_init();
                
    curl_setopt($chCURLOPT_URL$url);
                
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
                
    curl_setopt($chCURLOPT_CONNECTTIMEOUT1); // Socket Timeout value in mille second.
                
    curl_setopt($chCURLOPT_FAILONERROR1);
                
    curl_setopt($chCURLOPT_TIMEOUT60); // Total time to fetch the response in seconds after socket is opened.
                
    $response curl_exec($ch);
                
    $urlresponsecode curl_getinfo($ch);
                
    curl_close($ch);
                if (
    $urlresponsecode['http_code'] == 200) {
                  
                    
    header('Location: thankyou.html');
                } else {
                    print(
    "There is an error present. Please copy and email this error to support@creditrepair2013.com.<br />");
                }
            }
            
    ?>
        </body>
    </html>
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,996
    Rep Power
    9397
    Ah, haven't seen this question in a while.

    PHP FAQ
    #16 Why am I getting the error "Headers already sent"?

    If you try to send a header using the header() function (like for a redirect) and headers have already been sent previously, the call will fail. ANY kind of output will send the plaintext headers, preventing a Location header from being sent. The error message contains the file and line that sent the output. Go there, find the problem, and remove it. Whitespace and Byte Order Markers (BOM) outside of the PHP tags count as output. It's best to remove the closing PHP tag from all your files anyway.
    Your output is the HTML before the code and the echos you do inside it. Restructure your file so that the redirect can happen before any output is sent.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0
    Originally Posted by requinix
    Ah, haven't seen this question in a while.


    Your output is the HTML before the code and the echos you do inside it. Restructure your file so that the redirect can happen before any output is sent.
    Thanks for the response. It seems like the POST to the one url containing the database is conflicting with the header redirect to the url where I want the traffic to go...

    Perhaps one solution is to set something up on their database that will direct all incoming traffic from the site back again and to the thank you page.

    But is there another solution that I can do on my end that will direct the traffic through the url that the POST is going to and then onto the thank you page?
  6. #4
  7. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,996
    Rep Power
    9397
    The best solution is to move code around so that the header() can happen and does so before there's any kind of output.
    Rather than output the form fields (do you still want to do that?) immediately, save it for later in case there's a problem - which is the only circumstance where the user would actually see them (because they'd be redirected immediately without seeing the contents of the page). Do the same if there's an error.

    Basically,
    PHP Code:
    <?php

    // process the form

    // if there were any problems {
        // save information so you can customize the form to display the error/form fields
    // } else {
        // redirect and exit
    // }

    ?>
    <!-- show the page -->
    Be sure to exit; immediately after trying the redirect with header(). PHP will not stop the script for you and there's no point in outputting anything the user won't see. In fact it might even be harmful.

IMN logo majestic logo threadwatch logo seochat tools logo