Solving the problem of "headers already sent"
m error that a very beginner programmer ends 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, on line 1, before "" there was a space... Everything outside of "" is HTML, so a space there would be like 1° HTML character causing header uploading... 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!