Help Please - Cannot modify header information - headers already sent by

unknown00

Regular Member
Joined
Jun 6, 2009
Messages
213
Reaction score
74
Code:
<?php

require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'internals'.DIRECTORY_SEPARATOR.'Header.inc.php';


$Layout = SK_Layout::getInstance();


$httpdoc = new httpdoc_Index;


$Layout->display($httpdoc);




require_once('geoip.inc');


$gi = geoip_open('GeoIP.dat', GEOIP_MEMORY_CACHE);
$country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);


$my_countries = array('us', 'ca', 'gb', 'fr', 'de', 'nl');
if (!in_array(strtolower($country), $my_countries))
{
header('Location: http://www.blah.com');
}
else
{
header('Location: http://www.blahblah.com');
}


?>

Im getting the following error on when i access the page
Cannot modify header information - headers already sent by

how can i resolve this issue, thx in advance
 
Next time try to post what you have done to rectify the error as searching Google brings up lots of responses for that error and it is hard to know which might be suitable.

Try the following then let us know how you get on:

Code:
[B]PHP Cannot modify header information[/B]

[B]Warning:[/B] Cannot modify header information - headers already sent by (output started at /home/user/public_html/test.php:3) in [B]/home/user/public_html/test.php[/B] on line [B]4[/B]   Look familiar? This PHP error has haunted website owners for years.   Please understand, HostGator does not normally support your webdesign   and coding, but this error is too common for us to ignore. So, one of   our PHP experts has the answer for you.
  The problem you face is that you are trying to use the PHP header   function, header(), but there can be absolutely no HTML output before   this function is declared. Let's use some examples:  
 <!DOCTYPE "HTML 4.01 Transitional EN" "http://www.w3.org/">
<html><body>
<?php header ('Location: [URL="http://hostgator.com/%27%29;"]http://hostgator.com/');[/URL] ?>
</body>
</html> 
  This is incorrect coding. You cannot have any HTML preceding the header function. A few more bad examples:   <?php
echo "Task complete.";
header ('Location: [URL="http://hostgator.com/%27%29;"]http://hostgator.com/');[/URL]
?> 
   Task complete! <?php
header ('Location: [URL="http://hostgator.com/%27%29;"]http://hostgator.com/');[/URL]
?> 
  This is incorrect coding for the same reason. You cannot have any text   output before the header function. Plain text is always treated as  HTML  by your browser. One more bad example:    <?php
header ('Location: [URL="http://hostgator.com/%27%29;"]http://hostgator.com/');[/URL]
?> 
  This one is tricky. The blank space before the PHP tag counts as HTML output.    It is acceptable to have other PHP code before the header function, as   long as nothing is outputted as HTML. This is a correct example:    <?php
$variable = "value";
mysql_query("blah blah");
header ('Location: [URL="http://hostgator.com/%27%29;"]http://hostgator.com/');[/URL]
?> 
  I hope this helps. Remember, our technicians cannot fix your PHP code,  so if you need more help, please refer to  [URL]http://us.php.net/manual/en/index.php[/URL]



Source: http://support.hostgator.com/articl...echnical/php-cannot-modify-header-information
 
Well, there are not many cases where the error message describes the problem in a more accurate way. And a simple search with your favorite search engine would've showed many solutions for sure.

You have to use header() before any output. Even a simple space will break it. I guess $Layout->display($httpdoc); generates output?
 
There cannot be any output before using the php header function. Very common error. Check what is above where you call that in your script
 
thx, putting the header portion at the top of code seems to resolve the issue
 
Back
Top