0

Possible Duplicate:
Headers already sent by PHP

I am trying to write a JSON API for an app that I am making. It's in PHP. At the beginning of the PHP script I try to check if all the GET parameters are present, if not, then the server should send a 400 - Bad Request header.

the code looks like this:

//CHECK CONDITIONS
if (isset($GET["clat"]) && isset($GET["clng"]) && isset($GET["limit"]))
{
//do stuff
} else {
header( "HTTP/1.0 400 Bad Request");
exit;
}

The server simply serves an empty page with the 200 - OK header. So I tested it on another webserver and there it does seem to work.

So apparently there's something wrong with the server. How do I go around debugging this issue?

EDIT I seem to have found the line causing the header to always be 200-OK. At the beginning of the PHP I include another php file with some mySQL Connection parameters. When I comment out the include 'setup.php'; line it works. the setup.php contains the following:

 // Connects to Our Database 
 mysql_connect("#####", "#####", "######") or die(mysql_error()); 
 mysql_select_db("####") or die(mysql_error()); 

How do I remain the external DB info include while being able to send a different HTTP Header afterwards?

Thanks in advance.

Community
  • 1
  • 1
Wissej
  • 241
  • 3
  • 14

3 Answers3

1
header( "HTTP/1.0 400 Bad Request", true, 400);
Igor Popov
  • 924
  • 8
  • 14
1

Check if you're included files doesn't have UTF BOM at the beggining of file and any text after closing ?>, because if there is any, PHP will automatically send headers

ninaj
  • 748
  • 4
  • 9
  • 1
    The included file only contains the code that I pasted above surrounded by – Wissej Oct 10 '12 at 14:16
  • try to delete ending ?> tags from files as they're not necessary, if your files are only php code, and check the main file as well for the BOM – ninaj Oct 10 '12 at 14:18
  • Your answer got me to the solution. There was a space after the `?>`. Thanks a lot! – Wissej Oct 10 '12 at 14:19
0

Try this way:

<?php
header("Status: 400 Bad Request");
?>
yakxxx
  • 2,841
  • 2
  • 21
  • 22