How to resolve "Cannot Modify header information - headers already sent by" error in PHP?

Asked

Viewed 24,272 times

0

Because when we make use of the functions header() and setcookie() error occurs:

Warning: Cannot Modify header information - headers already sent by (output Started at /teste.php:10) in /php test. on line 21

Why does this mistake happen? How to solve?

  • 2

    It would be better if you showed the code, no? Incidentally, the SOPT deserves a version of How to fix "Headers already sent" error in PHP, these mistakes will drip day in and day out for a lot of time.

  • @brasofilo My question was purposeful, exactly for this. Good idea to assemble this guide! If no one answer I answer :)

  • 2

    possible duplicate ?

  • @lost, yes. Pity that the title of the other is so little descriptive...

  • 1

    nothing prevents us editing the title of the other, or prevents?

2 answers

6


In fact, it is not a mistake. It is a warning(Warning).

This is done when your browser requests the web server. With the response from the web server. It gives you a response header(header). This header contains information about page encoding, page size, cache duration time, last update time, and everything relevant about a web page to a browser.

When you read "headers already sent" means the server has already sent the header and AFTER this submission you are trying to change some information that should be sent in the header.

But if you’re not manipulating anything that comes in header. You have done nothing and are getting this error.

In PHP the header starts uploading as soon as you enter the first HTML character. Either outside of PHP or inside PHP code with an echo or print().

<?php
  $numero_1 = 5;
  echo $numero_1;
?>

Everything outside of PHP code is HTML, a space on line 1 before the opening of PHP code would be the reason for a response to the client. Any function, session, cookie would cause error.

To fix this warning. You would have to put all the code that works with Session, cookie, redirects, etc... before any HTML character. No trying to set/create a cookie after sending a "Hello world" message to your browser.

If you need to set/create a cookie before sending a "Hello world" message or anything, rethink what you are doing.

2

It depends, if you are editing with Dreamweaver it adds to your files at the time of saving an option called GOOD ENCODING (something of the type), this prevents your scripts in PHP run properly mainly on linux servers, windows works normally. Otherwise, see if php checks like:

<?php
if(isset($_POST['teste'])){
  header("Location: index.php");
  // é essencial utilizar o exit() após um redireccionamento com header() para evitar possíveis erros
  exit();
}
?>

And other checks are found in the page header, that is, the lines that do a check/redirect should always be above the code, always with the tags at the beginning of the HTML page. Forms, text, etc., can never be above php when redirects are made. Always above. Post your code to us that makes it easier to help.

  • 1

    Swear that Dreamweaver saves with GOOD by default? What...

  • We consider this an irony, however, many do not know... I found it interesting to emphasize.

  • It’s good to point out. I didn’t expect any program to still do this today. I’m glad I’m a long way from Dreamweaver :)

  • I used Dreamweaver for a long time, even the newest version (CS6) does this unfortunately. I don’t see any use in this coding, but there must be some... But I started using Netbeans and Sublime Text, in my view they are the best, simple and fast. There’s also the Eclipse I’ve heard so much about, but I’ve had little contact with it.

Browser other questions tagged

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