Why I am getting (headers already sent)
Results 1 to 4 of 4

Thread: Why I am getting (headers already sent)

  1. #1
    Senior Member
    Join Date
    Oct 2011
    Posts
    392

    Why I am getting (headers already sent)

    Hi,

    I have a PHP which should return json output. I am using the same code in other application and it works fine but now getting:

    headers already sent

    This is my code.. Kindly help..

    PHP Code:
    <?php
      header
    ("Content-Type: application/json");

      if (isset(
    $_SERVER['HTTP_ORIGIN'])) {
        
    header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
        
    header('Access-Control-Allow-Credentials: true');
        
    header('Access-Control-Max-Age: 86400');    // cache for 1 day
      
    }

        
    $mysql_host        =    "mysql:host=mysql.mydomain.com;dbname=myDB";
        
    $mysql_user        =    "mydomainuser";
        
    $mysql_password    =    "mydomainPassword";
        
    $mysql_options     =    array
                            (
                                
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
                            );

        
    // $mysql_connection;
        
    $mysql_connection = new PDO($mysql_host$mysql_user$mysql_password$mysql_options);
        
    $mysql_connection->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);

      
    $mysql_query $mysql_connection->prepare('CALL sp_populate_countries()');
        
    $mysql_query->execute();

      if (
    $mysql_query->rowCount() <= 0) { echo "false"; }
      else
      {
        
    $jsonData '{ "Countries" :[';

        while(
    $mysql_row $mysql_query->fetch())
        {
          
    $jsonData .= '{"country_code_alpha2":"' $mysql_row["country_code_alpha2"] . '","country_name":"' $mysql_row["country_name"] . '"},';
        }

        
    $jsonData chop($jsonData",");
        
    $jsonData .= ']}';

            echo 
    $jsonData;
      }
    ?>

    Jassim

  2. #2
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,800
    What does the error message say, exactly? Namely, at what file/line-number does it say headers were already sent?

    One not uncommon gotcha is a BOM (Byte Order Mark) at the start of the file before the <?php tag, if your editor saved the file as UTF-8 with BOM (in which case there should be an option to save it without BOM).
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    Senior Member
    Join Date
    Nov 2002
    Posts
    163
    There's also a possibility - if this file is being included from a different script - that there's white space after a closing php ( ?> ) tag. You don't need the closing tags on files, and you'll avoid the possibility of this issue if you don't use them.

  4. #4
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,800
    Quote Originally Posted by maxxd View Post
    There's also a possibility - if this file is being included from a different script - that there's white space after a closing php ( ?> ) tag. You don't need the closing tags on files, and you'll avoid the possibility of this issue if you don't use them.
    +1
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •