"Warning: Can not modify header information - headers already sent by" [duplicate]

1

I'm getting the following warning in PHP:

  

Warning: Can not modify header information - headers already sent by   (output started at newclient.php: 683)

Here is line 683:

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

I've been seeing it in SOen, but I do not see anything concrete in this case.

I've already seen this question and this but did not clarify anything about this particular issue.

[EDIT] The remaining code is PHP and "normal" HTML are if , echo , HTML tags.

    
asked by anonymous 05.08.2014 / 16:53

1 answer

1

Retrieved from: Resolving the "headers already sent" problem "

Resolving the "headers already sent" problem

An error that most beginner programmer comes across is the famous "Warning: Can not modify header information already sent by ..." or "Warning: Can not send session cookie - headers already sent by ..." and is not always easy to figure out what to do to resolve this error (which is not really an error, it's a warning, a warning).

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

Why "headers already sent"?

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

After sending the header, the server sends the HTML of the entire page and your browser starts to put it together for you.

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

For example, cookies: are defined 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 Session, which are like encrypted cookies that are saved on the server. Every session has a session cookie that is sent to the visitor in order to identify it and keep the values of your session ... If you try to create or remove some value of the session after sending the header will receive the message of error "Warning: Can not send session cookie - headers already sent by ...".

And when the hell did I send the header? I did not do anything!

Actually, if you do not use any headers manipulation function, you did nothing and you're getting this error ... But there's an explanation for that!

When it comes to PHP (and I believe the same is true for all other web languages that need to be read by a parser), the header begins to be sent as soon as you enter the first character in the final HTML of the page ... out of PHP code with normal HTML, either inside PHP code with an echo or print ().

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

Imagine that on line 1, before the "" is HTML, then a space there would be like the 1st character of the HTML causing the header to be sent ... Any session function / cookie / etc. 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 attempted to create a session (which will define a new session cookie) after sending the header (because of echo).

Okay, and how do I solve it?

Remember that I said that you did nothing and are still getting the error? To solve the problem is the same thing: nothing (beyond normal) needs to be done ... You just need to put all code that works with headers (sessions, cookies, redirects and etc.) before sending any HTML pro character ... Nothing to try set / create a cookie or session after sending a "Welcome!" or submitting your website.

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

And before you comment on "but my site needs to send HTML before creating a cookie!" I'll answer you "planning error". :)

I hope fewer people have this problem as of today!

Hugs and until the next: D

    
05.08.2014 / 17:21