Solved

Cannot modify header information - headers already sent by

Posted on 2004-03-31
19
126,419 Views
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.

Christian
0
Comment
Question by:theprankstanator
  • 5
  • 4
  • 3
  • +6
19 Comments
 
LVL 6

Expert Comment

by:CyberGhost
ID: 10730339
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); ?

regards
CyberGhost
0
 
LVL 6

Expert Comment

by:jkna_gunn
ID: 10730340
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?>
</script>
.....
0
 

Author Comment

by:theprankstanator
ID: 10730403
Jkna,

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;
  </script>

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!

Christian
0
 
LVL 6

Accepted Solution

by:
jkna_gunn earned 500 total points
ID: 10730436
ok try
print "<script language=\"JavaScript\">";
print "window.location = '$insertGoTo' ";
print "</script>";
0
 
LVL 6

Expert Comment

by:CyberGhost
ID: 10730583
Man, I didn't told you to use header("Location: %s", $insertGoTo);   :-)
Do NOT use the comma, but a dot after the " ...

Code:
header("Location: ".$insertGoTo);
NOT
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);
  exit;
}

regards
CyberGhost
0
 

Author Comment

by:theprankstanator
ID: 10730856
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.
  exit;
}

Thanks!

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!

Christian
0
 
LVL 6

Expert Comment

by:CyberGhost
ID: 10730929
I'm going to parse it myself, just a few minutes please.
0
 
LVL 6

Expert Comment

by:jkna_gunn
ID: 10730945
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

0
 
LVL 6

Expert Comment

by:CyberGhost
ID: 10731253
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)

regards
CyberGhost
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Expert Comment

by:rlawley
ID: 10731475
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.
0
 

Author Comment

by:theprankstanator
ID: 10732386
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!

Christian
0
 
LVL 6

Expert Comment

by:CyberGhost
ID: 10738787
Your choice. Good luck ;o)
0
 

Expert Comment

by:onlycubes
ID: 13598420
I have something simmilar to the above. Here is what is happening:
--Below is the error that comes up in IE 6.0 & Netscape

Start__________________________________________________

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

__________________________________________________End

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

start______________________________________________

$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)
  exit;
__________________________________________________End

Please let me know if you have any ideas??

thanks a bunch!
onlycubes
0
 
LVL 6

Expert Comment

by:jkna_gunn
ID: 13599124
what is on line 12 in the file connBlueSky.php?
0
 

Expert Comment

by:onlycubes
ID: 13602610
nothing this is the complete file, it only covers 10 lines:
___________________________start
<?php
# 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);
?>
---------------------end
0
 
LVL 2

Expert Comment

by:The_Systech
ID: 13683578
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.
0
 

Expert Comment

by:CongUan
ID: 14106950
It's easy to fix

Please open php.ini file

Find
output_buffering

And set
output_buffering = On = On

Replace
output_buffering = On

Please send point to me!
0
 

Expert Comment

by:QuanTriWeb
ID: 20106965
The "headers already sent" error is usually caused by having white space before or after the opening and closing PHP tags (<?php . . . ?>).
0
 

Expert Comment

by:r4ccoon
ID: 23334596
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..
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now