Link to home
Create AccountLog in
PHP

PHP

--

Questions

--

Followers

Top Experts

Avatar of theprankstanator
theprankstanator

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

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of CyberGhostCyberGhost🇨🇿

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

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

Avatar of theprankstanatortheprankstanator

ASKER

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

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


ASKER CERTIFIED SOLUTION
Avatar of jkna_gunnjkna_gunn

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of CyberGhostCyberGhost🇨🇿

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

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

Avatar of CyberGhostCyberGhost🇨🇿

I'm going to parse it myself, just a few minutes please.

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


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


Avatar of CyberGhostCyberGhost🇨🇿

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

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.

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


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

Avatar of CyberGhostCyberGhost🇨🇿

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

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

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


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

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

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.

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


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!

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

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.

PHP

PHP

--

Questions

--

Followers

Top Experts

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.