Warning: Cannot modify header information - headers already sent by (output started at /home/mysite/public_html/redirect_index.php:20) in /home/mysite/public_html/redirect_index.php on line 38

I've converting as asp site to php. I'm a partial numpty in asp and complete numpty in php

I keep being told this is a white space problem..... I can't see it!

The code below is a redirect: from one page to another where a Yes or No is clicked and then redirected accordingly.

The asp code was "converted" to php using a conversion prog, so anything could have happened! Maybe the sequence of events is incorrect?

Any advice greatly appreciated.

<? if(${"SubArea"}=="N")
{
?>
	<? setcookie("Copyright2009","do not agree",0,"","",0);
?>
	<? header("Location: "."copyrightNotAgree.php");?>
<? }?>
<? $strTemp=$_COOKIE["Copyright2009"];?>
<? echo $strTemp;?>
<? if($strTemp=="agree")
{
?>

	<? header("Location: "."index2.php");?>
<? }
  else
{
?>

	<? if(${"SubArea"}=="Y")
  {
?>

		<? setcookie("Copyright2009","agree",0,"","",0);
?><?php
//Calculate 30 days in the future
//seconds * minutes * hours * days + current time
$inOneMonth = 60 * 60 * 24 * 30 + time(); 
setcookie('lastVisit', date("G:i - m/d/y"), $inOneMonth); 
?>

<? $URL="";?>
	<?   }
    else
  {
?>

		<? header("Location: copyright.php");?>  (this is line 38 referred to in the error message)
	<?   } ?>
<? } ?>

Dani AI

Generated

Short diagnosis: that warning means PHP tried to send HTTP headers after output had already been emitted. Common culprits are stray whitespace outside PHP tags, leftover HTML or debug echo/print, included files that start with a UTF-8 BOM, or many open/close <? ... ?> blocks that introduce blank lines. is right to call out any output before header-setting calls, and ’s suggestion to consolidate PHP into a single block is good practical advice. ’s ob_start() tip will hide the symptom but is a workaround; ’s recommendation to use @ to suppress the warning is unsafe and should be avoided.

Quick checklist to fix and to debug (run from your script or a bootstrap file):

  • Remove unnecessary PHP close/open tag pairs; keep logic in one PHP block and avoid trailing ?> at the end of pure-PHP files.
  • Search included files for leading/trailing whitespace and save files as “UTF-8 without BOM” (many editors offer this option).
  • Don’t output anything (HTML, echo, var_dump or even a blank line) before calling functions that send headers (cookies, session_start, redirects).
  • Use headers_sent to locate exactly where output started; it tells you file and line.

A small debugging snippet to detect where output began:

<?php
if (headers_sent($file, $line)) {
    error_log("Headers already sent in $file on line $line");
    exit;
}

Final notes: prefer fixing the root cause (remove whitespace/BOM and consolidate PHP) over buffering. After you call a redirect or set a cookie, stop processing (exit) so no further output is emitted. This approach will resolve the warning reliably and keep behavior predictable.

Recommended Answers

All 8 Replies

Member Avatar for Member #120589

You can't have anything echoed to the screen (including whitespace) before the header() function.

<? echo $strTemp;?>

will ensure your code goes "t*** up".

Thanks Buddie, methinks I had better bury my head in some books to get at least up to half speed on this coding lark!

if you still want to make is as error free please use ob_start() function at the start of this page. ob_start() function will handle this error

You do not need all of those PHP start and end tags.

You only need one at the beginning of the PHP code and one at the end. Any blank line in between a close and start tag is being sent to the browser. Eliminate all of the unecessary PHP start and end tags and your problem will go away.

hi
my name is paul who have the username of (wbtech.zxq.net this is the address my online banking, i'm the one who coded this online bank, i've got the error "Warning: Cannot modify header information - headers already sent by" but one that i did to remove this tricky error is adding a "@" sign before the word header>> @header just like this, then boom!, its gone, also the session_start() >>"Warning: Cannot modify header information - headers already sent by" i also just add a "@" in session_start >>>@session_start(); just like this..then all done..:-)

actually you dont need to erase all blank spaces either beginning nor ending, just add @ sign where your error is..:-)

Cannot send session cache limiter>> is should say in the session_start(); just add a "@" sign..that's all folks!, hi add me in my facebook my email is see yah!!

Member Avatar for Member #120589

Thanks for resurrecting this dead post. Placing @ is not a replacement for proper coding.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.