0

I'm setting the header in PHP with header('name: '.$someVariable), where the variable varies depends on user input.

The problem is, when I check on the returned header values, all of them are a trimmed compared to the actual result I wanted, for example, " ABCD " becomes "ABCD" in the returned header. Is this a normal behavior? If so, how can I disable it? Or what are some alternatives that will retain the result perfectly?

My use case: I return the user input in the header as a verification of the response returned by the server, as there can be many requests/responses going on in a short period of time, and this will help me match these requests and responses

Note: I can't use echo because it's reserved for other tasks.

Jack
  • 5,354
  • 2
  • 29
  • 54

1 Answers1

1

Why do you use user input as HTTP header values? Why does whitespace matter there? What problem are you solving by doing this?

Anyway, leading and trailing whitespace in header values is ignored:

header-field   = field-name ":" OWS field-value OWS

Where "OWS" means "optional whitespace". And you can't use quotes around the value to circumvent this.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • I return the user input in the header as a verification of the response returned by the server, as there can be many requests/responses going on in a short period of time, and this will help me match these requests and responses. Anyways, does this mean there's no way for me to leave these whitespaces in the header? What does that optional whitespace mean? Does it mean I can put spaces there but they'll just be ignored? Also, if I can't do it this way, is there a better alternative for my use case? – Jack Jan 08 '19 at 20:00