9

Could someone explain what is wrong with this PHP code? (PHP Version 5.3.5)

<?php 
    header('Bad Request', true, 400);
    exit;

I get a status 500 instead of 400. And in the apache error log I get

malformed header from script. Bad header=Bad Request: listener.php

What am I doing wrong here? Have I misunderstood the docs?

Svish
  • 152,914
  • 173
  • 462
  • 620

1 Answers1

24

you have to write:

header('HTTP/1.0 400 Bad Request', true, 400);
Racooon
  • 1,468
  • 2
  • 10
  • 28
  • Thank you! Why couldn't it just have figured that out on it's own... *sigh* – Svish May 23 '11 at 21:53
  • 2
    I find that you don't really need the two optional parameters for HTTP response codes - `header('HTTP/1.0 400 Bad Request')` works just as well. – HorusKol May 23 '11 at 23:48
  • @HorusKol, Yeah, but I must say I find that quite dumb. The status code is the only thing that should have to be required. Shouldn't PHP know the right text and stuff itself kind of? – Svish May 24 '11 at 04:36
  • "Forces the HTTP response code to the specified value. Note that this parameter **only has an effect if the string is not empty.**" http://php.net/manual/en/function.header.php – neiker Dec 03 '12 at 13:54