0

I'm trying to implement a very simple shorthand function for sending headers, based on this answer.

The function goes like this:

function hdr($code)
{
    if (!headers_sent())
    {
        header('x', TRUE, $code);
    }
}

Now, it works well in my local server, but once I go into production I get a 500, and the following form the error log:

[Tue Jun 19 11:01:59 2012] [error] [client 87.12.83.179] malformed header from script. Bad header=x: php54.cgi

If i modify the function as such:

function hdr($code)
{
    if (!headers_sent())
    {
        header($_SERVER['SERVER_PROTOCOL'], TRUE, $code);
    }
}

It works on production, but not locally.

I guess the problem is with the production php being a cgi script? Any way to solve the problem (ie. have one function for all) without going with an associative array of 'codes' => 'messages'?

Community
  • 1
  • 1
mjsarfatti
  • 1,725
  • 1
  • 15
  • 22

1 Answers1

0

The problem is, that a header never looks like X. The header block starts with something like

HTTP/1.1 200 OK

And then only key-value-pairs follow

A-Header: Some value

It seems, that the webserver accepts HTTP/1.1 without status as header, but you shouldn' rely on it. Also that your local server accepts X as heade value doesn't mean you should use it ;)

KingCrunch
  • 128,817
  • 21
  • 151
  • 173
  • I guess that answer hasn't been tested thoroughly then... And forcing a status code doesn't force the status message as well. Thank you! – mjsarfatti Jun 19 '12 at 09:50