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'?

1 Answer 1

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 ;)

Sign up to request clarification or add additional context in comments.

1 Comment

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.