cannot modify header info, headers already sent

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JRough

    cannot modify header info, headers already sent

    I have this section at the end of a page
    -------------------
    if ($_POST['assign']== 'Open in Excel'){

    if (empty($data)) {
    $data = "\n(0) Records Found!\n";}
    header("Content-type: application/xmsdownload");
    header("Content-Disposition: attachment; filename=".
    $file_name.date ("Y:m:d H:i").".xls");
    header("Pragma: no-cache");
    header("Expires ; 0");

    print "$header\n$data ";

    exit;

    }else{
    include "header.php ";
    include $template_path. $template;
    include "footer.php ";

    }

    Because the header.php is sent at the bottom of the page I can't have
    the print to Excel output header in the if statement or I get the
    message header already sent. I need the else statements where they
    are because earlier in the code there are different selections of
    templates depending on if a form is filled out so at the end it has to
    display the right template.

    Is there a way to send the excel output away from the page in a
    function or something so that it processes the else?

    thanks,
  • Jerry Stuckle

    #2
    Re: cannot modify header info, headers already sent

    JRough wrote:
    I have this section at the end of a page
    -------------------
    if ($_POST['assign']== 'Open in Excel'){
    >
    if (empty($data)) {
    $data = "\n(0) Records Found!\n";}
    header("Content-type: application/xmsdownload");
    header("Content-Disposition: attachment; filename=".
    $file_name.date ("Y:m:d H:i").".xls");
    header("Pragma: no-cache");
    header("Expires ; 0");
    >
    print "$header\n$data ";
    >
    exit;
    >
    }else{
    include "header.php ";
    include $template_path. $template;
    include "footer.php ";
    >
    }
    >
    Because the header.php is sent at the bottom of the page I can't have
    the print to Excel output header in the if statement or I get the
    message header already sent. I need the else statements where they
    are because earlier in the code there are different selections of
    templates depending on if a form is filled out so at the end it has to
    display the right template.
    >
    Is there a way to send the excel output away from the page in a
    function or something so that it processes the else?
    >
    thanks,
    >
    Nope. Once you've sent ANYTHING to the client, the web server sends
    default headers.

    You can send an Excel file or HTML in a request - but not both.

    As has been explained to you time and time again...

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • JRough

      #3
      Re: cannot modify header info, headers already sent

      On Oct 14, 7:16 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
      JRough wrote:
      I have this section at the end of a page
      -------------------
      if ($_POST['assign']== 'Open in Excel'){
      >
                 if (empty($data)) {
                         $data = "\n(0) Records Found!\n";}
         header("Content-type: application/xmsdownload");
         header("Content-Disposition: attachment; filename=".
      $file_name.date ("Y:m:d H:i").".xls");
         header("Pragma: no-cache");
         header("Expires ; 0");
      >
         print "$header\n$data ";
      >
         exit;
      >
      }else{
      include "header.php ";
         include $template_path. $template;
         include "footer.php ";
      >
      }
      >
      Because the header.php is sent at the bottom of the page I can't have
      the print to Excel output header in the if statement or I get the
      message header already sent.   I need the else statements where they
      are because earlier in the code there are different selections of
      templates depending on if a form is filled out so at the end it has to
      display  the right template.
      >
      Is there a way to send the excel output away from the page in a
      function or something so that it processes the else?
      >
      thanks,
      >
      Nope.  Once you've sent ANYTHING to the client, the web server sends
      default headers.
      >
      You can send an Excel file or HTML in a request - but not both.
      >
      As has been explained to you time and time again...
      >
      --
      =============== ===
      Remove the "x" from my email address
      Jerry Stuckle
      JDS Computer Training Corp.
      jstuck...@attgl obal.net
      =============== ===
      Okay, then maybe I can figure out how to redirect the excel if/excel
      output to another page. the problem is then I have to requery the
      database and run through all these queries depending on what a user
      puts in a form. Is there a way to send the one query for the
      selection to the redirected page so I don't have to go through all of
      that?
      thanks,

      Comment

      • Geoff Berrow

        #4
        Re: cannot modify header info, headers already sent

        Message-ID:
        <c8b8ff47-ba0c-4136-83c9-1971cd9ae191@s9 g2000prg.google groups.comfrom
        JRough contained the following:
        >Okay, then maybe I can figure out how to redirect the excel if/excel
        >output to another page. the problem is then I have to requery the
        >database and run through all these queries depending on what a user
        >puts in a form. Is there a way to send the one query for the
        >selection to the redirected page so I don't have to go through all of
        >that?
        >thanks,
        The best way is to re-arrange your logic so that all processing is done
        before any page output. This may involve temporarily storing the output
        as variables. Once the logic is finished you can either redirect using
        header() or echo the output variables.

        Like Jerry says, you can't do both.
        --
        Geoff Berrow 011000100110110 0010000000110
        001101101011011 001000110111101 100111001011
        100110001101101 111001011100111 010101101011
        http://slipperyhill.co.uk - http://4theweb.co.uk

        Comment

        • transpar3nt

          #5
          Re: cannot modify header info, headers already sent

          I don't know every detail of your current logic, but I would think you
          could just have the input on this page and have it post that
          information (like you already are doing here) to another page where
          all the processing is done first. That way you have only one file
          that accesses the database because it's doing all the work... instead
          of splitting everything up amongst this one page.

          Comment

          Working...