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

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>

Content-Type: text/html; charset=UTF-8 would display in the browser text in bold and italics:

✅ OK

whereas Content-Type: text/plain; charset=UTF-8 would display in the browser like this:

<b><i>✅ OK</i></b>

TLDR Version: If you really are only outputing plain text with no special characters like < or > then it doesn’t really matter, but it IS wrong.

Leave a Comment