We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now


Cannot modify header information - headers already sent by

Medium Priority
Last Modified: 2012-06-27
Hi All,

I have a few scripts whereby once a form is submitted (and info uploaded and stored in the db), it redirects to another page.

The problem is, that once I uploaded it to my clients server, I get this error;

Warning: Cannot modify header information - headers already sent by (output started at /path/to/mainframe_distributors_add.php:19) in /path/to/add_dist.php on line 79

Now, I know that header() must be before any output, but the site is in frames and I need the code where it is, it also worked fine on my Linux server.

I trie ob_start() - but that caused more errors than it fixed....

Anyway, heres my code;

  $insertGoTo = "mainframe_distributors_thanks.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  header(sprintf("Location: %s", $insertGoTo));

Thanks for any help.

Watch Question

I think there is no problem with frames in here.

First thing I registered was that you are enclosing the HEADER statement with the } and it's nowhere opened. Well, maybe there's just a cut of your code, so in that way it's ok.

Second, why you are using sprintf in a HEADER statement? That's something I've never seen before. I think this may cause the problem, because sprintf output the text to a browser and therefore it must send header the information before it can print anything out.
Why don't you simply use: header("Location: ".$insertGoTo); ?

if the frame code is output first then header will always give that error.
why not use javascript instead to redirect?
<script language="JavaScript">
    window.location = <?=$insertGoTo?>



I changed it to that and got a parse error, so i changed it around a bit a few times with the same parse error...
  $insertGoTo = "mainframe_distributors_thanks.php";
  <script language="JavaScript">
    window.location = $insertGoTo;

Cyberghost, yeah that was just a snippet of my code, i tried
  $insertGoTo = "mainframe_distributors_thanks.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  header("Location: %s", $insertGoTo);

And got the header error again.

Thanks guys!

Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Man, I didn't told you to use header("Location: %s", $insertGoTo);   :-)
Do NOT use the comma, but a dot after the " ...

header("Location: ".$insertGoTo);
header("Location: ", $insertGoTo);

... dot is an operator that combines string literals and variables. Don't put there that %s, otherwise it won't work.
BTW: it's not a good idea to use JavaScript when using PHP to relocate, because there could be a guy with JavaScript turned off and then nothing happen. PHP will allways work, just learn how to use it ;o)

BTW2: you may put the exit; command to exit execution of further code lines when relocating via header();

So your code finally should looks like:
$insertGoTo = "mainframe_distributors_thanks.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  header("Location: ".$insertGoTo);



Hi CyberGhost,

Still getting an error with that code;
$insertGoTo = "mainframe_distributors_thanks.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  header("Location: ".$insertGoTo);  // -> Line 81 is the error.


And jkna, I will use that if I cant get this to work tonight, but as Cyber said, if somebody has it turned off etc.. :) Thanks again!

I'm going to parse it myself, just a few minutes please.
well if your worried about javascript being turned off (doubtful these days i would have thought)
you can use the meta refresh tag instead.

example :
<META HTTP-EQUIV="refresh" content="1;URL=http://www.yoursite.com/newpage.htm">

just replace the url site with your variable

Well, got some problems with my Apache, so didn't parse it yet.
But got a suggestion: are you sure, that there isn't any code before the 81st line that make some output (like you had in that HEADER part)?

Sometimes this error also occurs if you have some blank lines even before the <? and ?> tags.
I mean, don't you have something like:
*** File starts here ***
     <-- blank line (there ain't be even a single character, only an empty line)
<? <-- followed by first PHP tag
*** File ends here ***

OR - don't you have even the <HTML> or <HEAD> or <TITLE> or ANY of the HTML tags before the <? php tag?

BTW: it seems there is all ok in your coding if you only get the header error (mean no condition errors or so befor the HEADER part)


I think you need to check earlier in your file.  The error message says the output was started by something on Line 19, but the error is being triggered by line 79.  What's happening on line 19?

Also, you should check for any whitespace before/between any of the <? tags - I've been frustrated by that error before just to find I've put a space before the first opening <? tag.  

Next option to try is to put an exit() command on line 78 - this will stop the code from going any further, so you can view the source and see what has been output.


I love you jkna!! Thanks heaps mate, I used that java and it worked perfectly.

Sorry boys, but I'll stick with the java atm.

Thanks everyone for their help!

Your choice. Good luck ;o)
I have something simmilar to the above. Here is what is happening:
--Below is the error that comes up in IE 6.0 & Netscape


Warning: Cannot modify header information - headers already sent by (output started at /home/content/f/u/n/function/html/Connections/connBlueSky.php:12) in /home/content/f/u/n/function/html/order_history.php on line 45


there is code where line 45 is and where the error is generating from?
--Below is the code:


$MM_restrictGoTo = "login.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {  
  $MM_qsChar = "?";
  $MM_referrer = $_SERVER['PHP_SELF'];
  if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
  if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0)
  $MM_referrer .= "?" . $QUERY_STRING;
  $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
  header("Location: ". $MM_restrictGoTo); ________________________________________________________________________(this is line 45)

Please let me know if you have any ideas??

thanks a bunch!
what is on line 12 in the file connBlueSky.php?
nothing this is the complete file, it only covers 10 lines:
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_connBlueSky = "mysql31.secureserver.net";
$database_connBlueSky = "BlueSkyData215";
$username_connBlueSky = "BlueSkyData215";
$password_connBlueSky = "belle5";
$connBlueSky = mysql_pconnect($hostname_connBlueSky, $username_connBlueSky, $password_connBlueSky) or trigger_error(mysql_error(),E_USER_ERROR);
I would say you have an extra cariage return at the end of the ?> in the file... Put your cursor right after the ?> in the file and hit delete a few time to ensure that there is nothing in the file after that point and then save it and try again.

It's easy to fix

Please open php.ini file


And set
output_buffering = On = On

output_buffering = On

Please send point to me!
The "headers already sent" error is usually caused by having white space before or after the opening and closing PHP tags (<?php . . . ?>).

the basic thing is.. you shouldnt and cant put a whitespace, html or anything that considered to be a html or text, or anything put not inside <?php ?> before header() command..

header can be used to specify a redirection or a type of the file to be returned with php..

ok.. now for your problem..
1. you included a script with header("location:xxxxx") in the middle of html.. for examle
<div><?php include "filewithheader.php";?> this is not allowed.. you should put that file on top of everything that will output html
2. you write a header function after some html.

to solve it you need to put the header function or any included file with that header function on top of everything, ofcourse after some connection file if you use databases inside that script..
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.