Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 328
omg Avatar asked Sep 12 '09 04:09

omg


People also ask

Is Content-Type header necessary?

In short, no, it's not required.

What is Content-Type text plain?

The text/html content type is an Internet Media Type as well as a Multipurpose Internet Mail Extensions (MIME) content type. Using HTML in MIME messages allows the full richness of Web pages to be available in e-mail. text/plain [RFC1521] The text/plain content type is the generic subtype for plain text.

What does header Content-Type mean?

The Content-Type header is used to indicate the media type of the resource. The media type is a string sent along with the file indicating the format of the file. For example, for image file its media type will be like image/png or image/jpg, etc. In response, it tells about the type of returned content, to the client.

Why do we need Content-Type?

A sender that generates a message containing a payload body SHOULD generate a Content-Type header field in that message unless the intended media type of the enclosed representation is unknown to the sender.


1 Answers

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.

like image 112
Jeremy Logan Avatar answered Oct 10 '22 02:10

Jeremy Logan