Warning: Cannot modify header information – headers already sent by

It’s very frustrating when you try to invent something new with your ability and it just won’t work, isn’t it? PHP is indeed a basic programming language anyone can learn easily. Though, sometimes errors just pop-up and male you feel irritated.

One of such basic error is invalid arguments for foreach().

I personally don’t like errors. But you know what is important in writing the code is? It’s a habit of keeping basic rules of writing code of any language (PHP, wordpress, drupal etc.) to say bye bye to errors.

Here I am talking about the most common error any developer would have face till time:

“Warning: Cannot modify header information – Header already sent by (Output started at /controller/index.php: #) in view.php on line #”



No matter how it seems scary, you just don’t worry about it, we’ve got a solution for you. PHP headers are inbuilt function to send raw headers and manipulate informations sent to the client by the server.

First, Let’s see the reasons why this error even occurs:

  1. When you try to output anything before header output. Yes, remember headers must be sent before any page/output. When PHP receives its output, it will flush all collected headers.
  2. Make sure headers will be first to send before any of your echo or print statements.
  3. Extra spaces before <?php and after ?> tags.
  4. While dealing with session or cookie, when session is started again or is sent after any output, it will generate an error.
  5. When in long block of PHP you use multiple closing and opening PHP tags.
  6. Modifying header information after HTML has already been displayed to client.

How to know from where error is actually generated?

As you can see the error I have specified above, you will clearly know about the line number of your code that prompts error.

Here #, suggests a line number.

For example: “Warning: Cannot modify header information – headers already sent by (output started at /www/page.php:7) in /www/page.php on line 50”

You can see that our attempt on line 50 to modify header information is failed due to line number 7.

How to fix the error?


  1. First, check for extra spaces in your code as well as before and after of PHP tags – <?php and ?>. Remove them all. They may cause error.
  2. In case of session, start session and output buffering at top:

<?php
ob_start();
session_start();
?>
<html>....

  1. Remove extra closing and opening PHP tags in case of long block of code.
  2. Do not echo anything before header:

<?php
echo "say hello";
...
header('Location: http://...');
?>

  1. If you required to redirect your page with some redirection message, then try:

header("refresh:5;url= ".$myurl);
echo '<h1>Wait for a while, we are securely moving you...</h1>';
exit();

Or you can use

<meta http-equiv=”Refresh” content="2; url=mypage.html”>

Or echoing with javascript:

echo "<script> location.replace("target.html"); </script>"

Find this error in detail over here: https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php

Do not forget to remove extra spaces even if you paste code from somewhere or write your own. With basic understanding of functions and their working can save your head from errors.

Hope this helps you. Best of luck for you code!