Is header('Content-Type:text/plain'); necessary at all?


Question

I didn't see any difference with or without this head information yet.

1
56
9/12/2009 4:57:50 AM

Define "necessary".

It is necessary if you want the browser to know what the type of the file is. PHP automatically sets the Content-Type header to text/html if you don't override it so your browser is treating it as an HTML file that doesn't contain any HTML. If your output contained any HTML you'd see very different outcomes. If you were to send:

<b><i>test</i></b>

a Content-Type: text/html would output:

test

whereas Content-Type: text/plain would output:

<b><i>test</i></b>

TLDR Version: If you really are only outputing text then it doesn't really matter, but it IS wrong.

102
10/1/2015 8:21:52 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon