1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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

Discussion in 'PHP & Perl' started by unknown00, Oct 11, 2013.

  1. unknown00

    unknown00 Regular Member

    Joined:
    Jun 6, 2009
    Messages:
    212
    Likes Received:
    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
     
  2. Asif WILSON Khan

    Asif WILSON Khan Executive VIP Premium Member

    Joined:
    Nov 10, 2012
    Messages:
    10,116
    Likes Received:
    28,551
    Gender:
    Male
    Occupation:
    Fun Lovin' Criminal
    Location:
    London
    Home Page:
    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
     
    • Thanks Thanks x 1
  3. tripper_john_md

    tripper_john_md Newbie

    Joined:
    Feb 21, 2011
    Messages:
    40
    Likes Received:
    35
    Location:
    Southern Germany
    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?
     
  4. HerpDerpSlerp

    HerpDerpSlerp Power Member

    Joined:
    Mar 19, 2013
    Messages:
    778
    Likes Received:
    623
    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
     
  5. Asif WILSON Khan

    Asif WILSON Khan Executive VIP Premium Member

    Joined:
    Nov 10, 2012
    Messages:
    10,116
    Likes Received:
    28,551
    Gender:
    Male
    Occupation:
    Fun Lovin' Criminal
    Location:
    London
    Home Page:
  6. unknown00

    unknown00 Regular Member

    Joined:
    Jun 6, 2009
    Messages:
    212
    Likes Received:
    74
    thx, putting the header portion at the top of code seems to resolve the issue