Problem using the header function. Warning: Cannot modify header information

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jessica87
    New Member
    • May 2008
    • 10

    Problem using the header function. Warning: Cannot modify header information

    hi there, i m using this coding to retrieve the file from database so that my user can download the file...

    [code=php]
    <?php

    if (!isset($_GET['id'])) die('Invalid Parameter');

    include 'connectdb.php' ;

    $id = $_GET['id'];

    $result= "SELECT content FROM upload WHERE id = '$id'";

    $data = mysql_query($re sult) or die(mysql_error ());

    $info = mysql_fetch_arr ay($data);

    header('Content-disposition: attachment; filename="'.$id .'"');

    header('Content-type: application/msword');

    //header('Content-type: text/plain');

    readfile($info['Content']);

    ?>
    [/code]

    result,i keep getting this warning..
    Code:
    Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\xampp\htdocs\ym\downloadprocess.php:13) in C:\Program Files\xampp\htdocs\ym\downloadprocess.php on line 27
    i dont understand ,what is the problem...
    please help me...i m not familiar with php....
    Thank u
    Last edited by Atli; May 19 '08, 03:07 AM. Reason: Added [code] tags.
  • jessica87
    New Member
    • May 2008
    • 10

    #2
    hi it's me again..
    i try to use your coding...
    all of it...
    i've got problem with the coding on phase 4...

    whenever i want to download,,,
    it will give me a warning...simil ar to the previous warning..

    Code:
    Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\xampp\htdocs\second\get_file.php:13) in C:\Program Files\xampp\htdocs\second\get_file.php on line 69
    but below the warning, it display the file content
    but with all this ÿÿ����)���ÿ ÿÿÿÿ mixed up with the original content..


    can u tell me what i did wrong?
    please....

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Hi. Welcome to Bytes!

      This error is shown when you try to modify the header information after you have already added content to the response.

      To use the header function, nothing can be added to the response before it is called. Not even a single white-space. After anything is added, the headers are locked.

      Check out line 13 in your downloadprocess .php and see what's there. The error indicates that this is where you started adding output.

      P.S. I've moved your question into it's own thread in the PHP Forums, so not to crowd the article's comment thread.

      Comment

      • TheServant
        Recognized Expert Top Contributor
        • Feb 2008
        • 1168

        #4
        Maybe a silly question, but does your connectdb.php have any output (echo/print)? If it does, you will have sent info before finished modifying the headers and get that error.

        Comment

        • Adrock952
          New Member
          • Aug 2007
          • 8

          #5
          Any headers has got to be placed before any HTML is output to the screen.

          I used to get that problem but if you place any processing php about the <html> tag, it should work.

          There is a workaround using ob_start() and ob_flush() and the start and end of your document which will get rid of the errors

          Comment

          • jessica87
            New Member
            • May 2008
            • 10

            #6
            hi ....
            thank u very much for the advice....
            my coding's problem is solved....
            thank u so much....
            all of u...
            i love u guys so much
            ^_^
            won't be able to solve my prob without u guys.....

            thank u.....
            next time when i got problem with coding...can i ask u guys again?
            ^_^ thank u.....

            Comment

            • Markus
              Recognized Expert Expert
              • Jun 2007
              • 6092

              #7
              Originally posted by jessica87
              hi ....
              thank u very much for the advice....
              my coding's problem is solved....
              thank u so much....
              all of u...
              i love u guys so much
              ^_^
              won't be able to solve my prob without u guys.....

              thank u.....
              next time when i got problem with coding...can i ask u guys again?
              ^_^ thank u.....
              We love you too..

              I think.

              You're welcome back anytime!

              Comment

              • TheServant
                Recognized Expert Top Contributor
                • Feb 2008
                • 1168

                #8
                Originally posted by jessica87
                hi ....
                thank u very much for the advice....
                my coding's problem is solved....
                thank u so much....
                all of u...
                i love u guys so much
                ^_^
                won't be able to solve my prob without u guys.....

                thank u.....
                next time when i got problem with coding...can i ask u guys again?
                ^_^ thank u.....
                Can you tell us how you solved it so others can learn as well. Thanks!

                Comment

                • jessica87
                  New Member
                  • May 2008
                  • 10

                  #9
                  hehe
                  simple...
                  there a html code above my php coding,,,
                  i just switch the php coding above the html coding...
                  then it's done....
                  i guess i m not alert with the coding
                  thats y i have a hard time to detect my coding mistakes

                  Comment

                  • TheServant
                    Recognized Expert Top Contributor
                    • Feb 2008
                    • 1168

                    #10
                    Originally posted by jessica87
                    hehe
                    simple...
                    there a html code above my php coding,,,
                    i just switch the php coding above the html coding...
                    then it's done....
                    i guess i m not alert with the coding
                    thats y i have a hard time to detect my coding mistakes
                    That's why it's good to have some friends look over the code. I know I have spent a very long time on that problem too, tryingt o find where I have used some html! Anyway, thanks for dropping by and we'll see you next time.

                    Comment

                    • ronverdonk
                      Recognized Expert Specialist
                      • Jul 2006
                      • 4259

                      #11
                      This 'headers not sent problem' pops up every 2 months or so, you can almost set your watch on it. And the answer is always the same: you send data to your browser (mostly unintentionally ) before sending any other output. So you are getting this message is because you have may have/be:
                      • Whitespace before the opening php tag <?php
                      • Outputting something to the browser before you use session_start, header, setcookie etc

                      session_start, setcookie, header and a few other functions write header information to the web server/browser what to do. Such as when you use session_start it requests the browser to create a cookie which stores the PHPSESSID.

                      If you have output before you use these functions then they are unable to send new header information as it has already been sent in the form text/html, and so you get the headers already sent error message.

                      You can easily find the source of the problem by looking at the error message. As the answer is actully in there, but most people dont notice it as they may not understand what the error means. So lets take this error message as an example:

                      Warning: Cannot modify header information - headers already sent by (output started at C:\server\www\t est.php:6) in C:\server\www\t est.php on line 8
                      Now PHP has given us a clue here as it has told use where the output has started! Have look where it says output started at and it gives you the full path to the file and the line number. Now the line number that it stats is not usually where the output has started but where the output has ended, becuase the output could be above that line.
                      So lets look at test.php:
                      Code:
                       1 <html>
                       2 <head>
                       3 <title>Header test</title>
                       4 </head>
                       5 <body>
                       6 <?php
                       7
                       8 header("Location: http://www.google.com");
                       9
                      10 ?>
                      11 </body>
                      12 </html>
                      As you can see line 6 is the opening PHP tag (< ?php) this isn't the output but look above that line you'll notice it has some HTML code. This html code is the cause of the error!

                      So when you get this error message again look for the clue where it says output started at and goto the file and line number it says. Look above the line number and find where your output is located to.[/quote]Hope that helps you understand why your are getting this error message. (Thanks to wildteen88)

                      Ronald

                      Comment

                      Working...