tags:

views:

494

answers:

4

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

+7  A: 

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 woud 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.

fiXedd
+1  A: 

PHP uses Content-Type "text/html" as default - which is pretty similar to "text/plain" - and this explains why you don't see any differences. text/plain is necessary if you want to output text as is (including <>-symbols). Examples:

header("Content-Type:text/plain");
echo "<b>hello world</b>";
// Output: <b>hello world</b>

header("Content-Type:text/html");
echo "<b>hello world</b>";
// Output: hello world
Kristoffer Bohmann
Your *browser* doesn't use text/html as default: PHP does.
Miles
minor detail - the example code is still valid.
Kristoffer Bohmann
And the explanation is still wrong...
fiXedd
Corrected => "PHP uses Content-Type "text/html" as default."
Kristoffer Bohmann
A: 

no its not like that,here is Example for the support of my answer ---->the clear difference is visible ,when you go for HTTP Compression,which allows you to compress the data while travelling from Server to Client and the Type of this data automatically becomes as "gzip" which Tells browser that bowser got a zipped data and it has to upzip it,this is a example where Type really matters at Bowser.

Sam Rudolph
That's not actually how that works at all.
fiXedd
can please explain if i am wrong as i really want to correct my knowledge and really keen to learn..
Sam Rudolph
GZipped HTTP transfers use headers to indicate their status. If you were sending a GZipped html file the headers would look like: `Content-Encoding: gzip` `Content-Type: text/html`
fiXedd
oohh !! ya sorry it just slipped from my mind,but don't you agree if you define the TYPE,it allows you to compress some selective type
Sam Rudolph
Well sure, you can do selective compression based on MIME type (at least you can in Apache), but there's no reason to ever NOT compress text-based entities (unless the requesting party can't handle it).
fiXedd
ya thats true !! thanx for correcting me..
Sam Rudolph
+3  A: 

Setting the Content-Type header will affect how a web browser treats your content. When most mainstream web browsers encounter a Content-Type of text/plain, they'll render the raw text source in the browser window (as opposed to the source rendered at HTML). It's the difference between seeing

<b>foo</b>

or

foo

Additionally, when using the XMLHttpRequest object, your Content-Type header will affect how the browser serializes the returned results. Prior to the takeover of AJAX frameworks like jQuery and Prototype, a common problem with AJAX responses was a Content-Type set to text/html instead of text/xml. Similar problems would likely occur if the Content-Type was text/plain.

Alan Storm