Use of Content Type Header

“Content-type: text/html; charset=iso-8859-1”

Content-Type header with charset is used for telling browser which type of content you have to show and which additional encoding is applied to the content.

RFC 2616 HTTP 1.1:

When an entity-body is included with a message, the data type of that
body is determined via the header fields Content-Type and Content-
Encoding. These define a two-layer, ordered encoding model:

entity-body := Content-Encoding( Content-Type( data ) )

Content-Type specifies the media type of the underlying data.
Content-Encoding may be used to indicate any additional content codings applied to the data, usually for the purpose of data compression, that are a property of the requested resource. There is no default encoding.
Any HTTP/1.1 message containing an entity-body should include a Content-Type header field defining the media type of that body.

You can specify content type by adding:

<meta name=’Content-Type’ Content=’text/html; charset:UTF-8′ />

between <head> and </head> tags.

This will set your content type in your document. But some browser like Mozilla do not accepts content-type correctly when set by this method. So you should set content type using server setting also. Server side setting will set default content type and encoding for all documents. You can set content type and encoding document wise using server side scripts also.

In PHP add this line at the top of script:
header (‘Content-type: text/html; charset=iso-8859-1’);

Some time you may have noticed in a document that junk character is coming. If this is the case with your document then you can control it. For multiple languages UTF-8 is the best choice. General accepted encoding is ISO-8859-1.

What to do if your document is not showing correctly after using <meta> in html and header(‘Content-Type:….’) from server side server.

I have simple test based on trial and error method that will work for you. In Internet Explorer: View -> Encoding and select encoding type one by one. In Firefox, View->Character Encoding and select encoding type one by one. One encoding or multiple encoding suits to you. Now after identifying content encoding you can use that at both places.

In Firefox you get additional information about any document by using Tools-> Page Info.
Here you will get info. About all Meta used, type, Cache source, render Mode, Encoding, Size, Referring URL, Modified and Expire. This was just example. You will also get form, links, media and security informations.


1 Comment

  1. August 4, 2008 at 11:54 pm

    Great post!
    I agree, I think it’s very important to set the character encoding on both the server side and HTML document. This way you don’t get any “junk characters”, as you said.

    For documents to display at full quality its safe to give as much information as possible. This, to a point, insures the integrity of you XHtml documents.

    In PHP, or any other server side language for that matter, you can manipulate any type of file by using the content type in the headers. For example, you can create any image by going: header (’Content-type: image/jpeg;’);
    or a pdf by doing: header (’Content-type: application/pdf;’);

    I find this very useful for things such as mp3s, image, pdfs, file downloads etc.

    Have a great day!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: