views:

66

answers:

4

Why/when does one has to use CRLF's at the end of header in PHP? Here is one example (it's not necessarily correct):

header("method: POST\r\n");
header('Host: '.get_option('transact_url')."\r\n");
header('Content-type: application/x-www-form-urlencoded');
header('Content-length: '.strlen($transaction)."\r\n");
header($transaction."\r\n\r\n");
header("Connection: close\r\n\r\n");
header("Location: ".$key_client_url."\r\n");
+4  A: 

You should never do manual line-breaks inside of header(). The current implementation removes line-breaks so you're safe, but this could change in future (although there's no reason why it should be changed).

halfdan
So, what you're saying is that I can omit using CLRF's in header() alltogether AND that, by using "\r\n\r\n", it'll break my header code. Right? OK. Got that. But why do I see some codes (I think in Linux) using "\r\n" at every header statement?
nush
I've never seen such code. Can you show some project that does that?
halfdan
@nush he says not "you *can* omit" but "you **should never** use it". You see these codes because some programmers have not enough knowledge. Such a simple explanation.
Col. Shrapnel
@halfdan, 3 others agree with you but are you absolutely sure that adding in the extra whitespace to the `header` calls will break anything? Really?
salathe
@salathe: Those whitespaces might be removed by PHP before sending the header to stdout. Haven't tested it to be honest - but if they are sent to stdout the header is definetely broken.
halfdan
That was my point. I can't upvote with wrong information, even if the *idea* is sound.
salathe
@salathe: Well, tested it. PHP actually removes line-breaks, but if this would be changed in future the code might just not work.
halfdan
@halfdan and `echo` might suddenly output everything in reverse! Lets not worry too much about future (silly) changes. (P.S. Upvoted)
salathe
A: 

Got my log-in's all mixed, so I'm posting, although this a comment for halfdan. Feel free to correct it, if someone can.

link

I saw that in the link and nobody mentioned about what you've just told me, so I thought it has something to do with Linux line-endings. Granted, it was the only time I saw "\r\n" in header().

nush
Well that post is just complete crap (link). "header("method: POST");" doesn't even exist (some others don't exist either).
halfdan
Its worth pointing out that the code you've copied from that link is itself part of a "how to do I do this?" quesion; it wasn't one of the answers; it was the question. And the answers he got tell him it won't work that way.
Spudley
+2  A: 

If it's PHP, this code is nonsense.

header() function is used to send answer headers, while some of these headers are request ones.
You can see this code because one who wrote it has no clue.

Col. Shrapnel
A: 

Halfdan is correct. Here is the explanation.

The request line and headers must all end with CRLF (that is, a carriage return followed by a line feed). The empty line must consist of only and no other whitespace.

    Request       = Request-Line              ; Section 5.1
                    *(( general-header        ; Section 4.5
                     | request-header         ; Section 5.3
                     | entity-header ) CRLF)  ; Section 7.1
                    CRLF
                    [ message-body ]          ; Section 4.3

source: w3c.org - Hypertext Transfer Protocol -- HTTP/1.1

Elzo Valugi