Avatar of theprankstanator
theprankstanator asked on

Cannot modify header information - headers already sent by

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
PHP

Avatar of undefined
Last Comment
r4ccoon

8/22/2022 - Mon
CyberGhost

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
jkna_gunn

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>
.....
ASKER
theprankstanator

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
Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER CERTIFIED SOLUTION
jkna_gunn

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
CyberGhost

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
ASKER
theprankstanator

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
CyberGhost

I'm going to parse it myself, just a few minutes please.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
jkna_gunn

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

CyberGhost

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
rlawley

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.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER
theprankstanator

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
CyberGhost

Your choice. Good luck ;o)
onlycubes

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
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
jkna_gunn

what is on line 12 in the file connBlueSky.php?
onlycubes

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
The_Systech

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.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
CongUan

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!
QuanTriWeb

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

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..
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.