-2

Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\ecommerce\base\classes\baseconfig.php:23) in C:\wamp\www\ecommerce\base\classes\adminHelper.php on line 6

I know the root cause for the above warning. But iam not able to reslove it. My problem is different from other questions and many down-voted and closed topics.

  • The warning page is : index.php, is the only page that im dealing with displaying and Setting data. this warning is shown when i called header('Location') on helper adminHelper.php

  • Im using a class coreConfig.php which sets and gets the data for the index page.

  • And some helper classes too.

with the index page im using the same form , Same functions , same routines for setting data in a form. only data changes with the passed argument. and this page works perfectly with all other set of data, except for one.showing the above warning. i cannot figure it out why this happens. im formatting the output by using this function

    public $mainTabLength = "\t";
      public $tabLength = "\t\t";

      public function printFormat($text, $class) {
    if($this->useDivToPrint) {
      echo $this->mainTabLength . "<div class=\"$class\">" . $this->tabLength . "$text\n";
      echo $this->mainTabLength . "</div>\n";
    }
    else{
      echo $text;
    }
  }

I dont want to use buffering of the codes too .. awaiting comments .

Note : I had formatted my php code by removing any white-spaces from the IDE

Update :

The problem was solved by inspecting the buffer output.As expected my code was setting some header information already before im calling header('Location'). But i was sure that i was not doing such a mistake, because my all other piece of data is working fine. But i was surprised to see the output of buffer for working set of data and the one that caused the warning :

form 1: adding new category (working fine header('Location')) even i can see the following header information already sent :

<!DOCTYPE HTML>
<html>
<head>
<title>E-Commerce</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<link href="../css/style.css" media="screen" rel="stylesheet" type="text/css" />
<link rel="shortcut icon" href="../images/favicon.png"/>
<script type="text/javascript" language="javascript" src="../jses/jquery-1.11.3.min.js"></script>
<script type="text/javascript" language="javascript" src="../jses/scripts.js"></script>
<script type="text/javascript" language="javascript" src="../jses/datePicker.js"></script>

form 2: adding new product (not working header('Location') causing warning) i can see the same header information already sent :

<!DOCTYPE HTML>
<html>
<head>
<title>E-Commerce</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<link href="../css/style.css" media="screen" rel="stylesheet" type="text/css" />
<link rel="shortcut icon" href="../images/favicon.png"/>
<script type="text/javascript" language="javascript" src="../jses/jquery-1.11.3.min.js"></script>
<script type="text/javascript" language="javascript" src="../jses/scripts.js"></script>
<script type="text/javascript" language="javascript" src="../jses/datePicker.js"></script>

This was the root cause that i was not able to detect a problem with my code. why it worked for my form1 even though it already sent some header information already !!

Shan
  • 1,081
  • 1
  • 12
  • 35
  • @NanaPartykar But my code works perfectly with some other set of data – Shan Nov 10 '15 at 18:12
  • Actually, the issue is exactly the same as all the other downvoted questions. And this will likely get downvoted too. You have some output before calling header. This could include newlines/spaces at the beginning or end of a file, echo'ing anything, even an error somewhere that is sending output or html before php. This would also include any included files. – Jonathan Kuhn Nov 10 '15 at 18:13
  • And it isn't hard to prove. Go to the top of the first page loaded (likely index.php). First line after ` – Jonathan Kuhn Nov 10 '15 at 18:15
  • @JonathanKuhn i can understand that echoing may be causing the problem. But i have a set routine that does the same job for every set of data. But it causing only problem with one set of data. no echoing/prinitig when saving data from teh form. Just redirection to index page on success – Shan Nov 10 '15 at 18:17
  • 1
    But it's not just a call to `echo` that causes this. If you have html outside of php or a newline before `` in another file that is included. Even a simple error like a warning or notice from an undefined index or assumed constant will send output. I suggest turning on buffering just for testing and the line before the call to header, `file_put_contents("test.txt", ob_get_contents());` which will dump any output to a test.txt file. Then you can see if/what was written. – Jonathan Kuhn Nov 10 '15 at 18:20
  • Yes i had just tested, as you mentioned, what i can see the DOCTYPE defintion and and html tags head tags. I had also tried with my working set of data. its also showing the same set of string :( – Shan Nov 10 '15 at 18:23
  • 1
    So, your options are, either use buffering to capture that and not send it out prior to header call or find a way to not send out that data unless it is needed. – Jonathan Kuhn Nov 10 '15 at 18:30
  • @JonathanKuhn Thanks!! <3 <3 you saved me.. Actually the html output started for all set of data but it showing warning only for my this set of data. & thanks for introducing : ob_get_contents function. it just saved me ... And Now i wonder why this is not causing any warning for other set of data :D – Shan Nov 10 '15 at 18:38
  • No problem. There are actually quite a few buffer (ob_*) functions that do all sorts of stuff like getting the contents in the buffer, flushing it to the user or cleaning it. I'll post an answer in a few minutes so you can close the question. – Jonathan Kuhn Nov 10 '15 at 18:43

1 Answers1

0

Like I said in the comments, the issue is that you are having some output being sent before the call to header which is a no-no. This could include:

  • output from any included files, inlcuding all below and regardless of depth of included file (included file that includes another file that includes another file...etc).
  • a newline/space before or after <?php or ?> tags.
  • echo'ing anything.
  • even html output from outside of php tags.
  • a php error or warning.

These, and more, will all output something and will be considered "output before call to header". Because some of it can be hard to see, you might not even see it when viewing a page.

Your options are to either turn on output buffering or find some way to not display output during this request.

If you want to see what is being output (might help find the issue), you can do something like this for testing:

  • in the first line of the first script being requested (likely index.php, whatever file is requested by the browser), add a call to ob_start() right after <?php. If your file doesn't start with <?php then that is your problem right there.
  • on the line right before the call to header where you are getting your error, add something like file_put_contents("output.txt", ob_get_contents());.

This will get whatever output there was and write to to a text file. The file will likely run without errors this time (you did just turn on buffering) but it should still write any output. Also remember, it might just be a space or newline written. So if you open the file and see nothing, make sure to check if some non-display character was written.

Now it is just a matter of leaving on buffering or finding and removing that extra space.

Jonathan Kuhn
  • 15,279
  • 3
  • 32
  • 43
  • with file_put_contents("output.txt", ob_get_contents()); i was able to catch the string and soon i was able to resolve the warning. But that same string was sent already before i calls the header , for all other data set, But not showing any warning for except for the one i mentioned !! any idea about that ? – Shan Nov 10 '15 at 19:11
  • Buffering must have been on for the other calls or you were getting the error. Only two options. I guess a semi-third-option might have been that the server wasn't outputting the error because of error_reporting or display_errors. However it would also still have not redirected. You might just have been getting no output and a http 500 status code. Note too though that output buffering can be turned on through the php config to automatically start buffering on any request or even through a htaccess file. – Jonathan Kuhn Nov 10 '15 at 19:15
  • No, it was redirected with my success message even html doctype and html tags already sent(seen from ob_get_contents) if there is any warning , there is no redirection still I hve had the same string already sent. !! some how right now the problem is solved & im gettinh a blank output with ob_get_contents just before header call . – Shan Nov 10 '15 at 19:27