"Warning: Cannot Modify header information - headers already sent by"

Asked

Viewed 1,090 times

1

I’m getting the following Warning in PHP:

Warning: Cannot Modify header information - headers already sent by (output Started at newclient.php:683)

Here is the line 683:

...
682 </div>
683 <?php
684 if(...)
...

I’ve looked at Soen but I can’t find anything concrete for this case.

I’ve seen it before this question and this but I was not given any clarification on this particular subject.

[EDIT] The rest of the code is PHP and HTML "normal" ones if, a few echo, a few tags HTML.

  • Is there any session_start() before line 682?

  • Yes, it seems to be duplicated. If your question is more particular try to give more details...

  • Here are the details of the line. @Cesarmiguel so what’s the problem on line 683?

  • There is no way to answer your question specifically without knowing what you do in php from the 684 line. When a "headers already sent" warning happens, it means the server has already sent the header, and then you are trying to create or change some information that should be sent via header. If you improve your question, we can try to be more objective.

  • I don’t know, if you don’t give me more details, it’s not easy. With your question so you won’t get any more answers than the ones answered in the other two questions

  • 2

    If you are using DW this could be an error related to the time you save the file to without realizing saved with the option Incluir assiantura Unicode (BOM) checked. This automatically inserts a strange character before the <?php ... and fires the Warning.

  • Before line 682 and after line 684 I have PHP code and HTML "normal" none of those options that speak in the other question. The code is too big for here.

  • Pure PHP @marcosvinicius.

  • 1

    @Jorgeb. There is not only one possibility, and neither is that of HTML and "normal" PHP. If you can’t show what you’re doing in the code, the staff can only guess. If the code is too big, can pass details in a Pastebin or something like.

  • 1

    Problem solved, I was redirecting the page on loop that was the problem. Thank you @Blau was your comment that helped me.

Show 5 more comments

1 answer

1

Taken from: Solving the problem of "headers already sent"

Solving the problem of "headers already sent"

One mistake that many beginner programmer end up coming across there is the famous "Warning: Cannot Modify header information - headers already sent by..." or "Warning: Cannot send Session cookie - headers already sent by..." and it’s not always easy to figure out what to do to fix that mistake (which is actually no mistake, it’s a Warning, a warning).

Before solving the problem you need to understand why this error happens..

Why "headers already sent"?

Every web page is hosted on a server and your browser "asks" the page server the result (HTML) of the page with X address... Then the server moves its chopsticks, interprets the files and starts to answer you, sending a response header (the famous header) this header contains information about page encoding, page size, cache duration time, last update time and everything that is relevant, about a web page, to a browser.

After sending the header the server sends the HTML of the whole page and your browser starts to build it for you.

When you read "headers already sent" in the warning, it means that your server has already sent the header and AFTER that upload, you are trying to create or change some information that should be sent in the header.

For example, cookies: are set before sending the header and sent to the browser INSIDE the header... If you try to create or change a cookie after the header has been sent you will receive the error warning.

Another example that follows the same logic as Cookies is the Session, which are like encrypted cookies that are saved on the server. Every session has an identifier cookie (Session cookie) that is sent to the visitor in order to identify him and maintain the values of his session... If you try to create or remove any session value after header submission you will receive the error message "Warning: Cannot send Session cookie - headers already sent by... “.

And when the hell did I send the header? I didn’t do anything!

Really, if you don’t use any function handling headers, you haven’t done anything and are getting this error... But there is an explanation for this!

When it comes to PHP (and I believe the same happens with all other WEB languages that need to be read by a parser), the header starts to be sent as soon as you enter the first character in the final HTML of the page... Whether outside PHP code with normal HTML, whether inside PHP code with an echo or print().

<?php 
$numero = 3; 
$dobro = $numero * 2; // 6 
?> 

Imagine that on line 1, before "" is HTML, then a space there would be like 1° HTML character causing the header... Any session/cookie/etc. function within the PHP block would cause the error.

<?php 
echo 'Olá mundo'; 
session_start(); // Inicio de sessão depois do envio do header?! Problema! 
?> 

This is another classic case.. The developer tried to create a session (which will set a new session cookie) after sending the header (because of echo).

Yeah, and how I fix it?

Remember I said you didn’t do anything and you’re still getting the bug? To solve the problem is the same thing: nothing (beyond normal) It needs to be done... You just need to put any code that works with headers (sessions, cookies, redirects, etc.) before sending any character to HTML... No trying to set/create a cookie or session after sending a "Welcome!" or sending from your website.

Cookies and sessions, as well as redirects and content encryption should be sent, created, defined and modified BEFORE any HTML... After all, all HTML can and should depend on these factors.

And before you comment saying "but my site needs to send HTML before creating a cookie!" I reply "planning error". :)

I hope that fewer people have this problem from today!

Hugs and until next :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.