Adding Content-Type Header from PHP

How to correctly output different content-types using the PHP header function.

4335 views

Edited: 2021-03-18 08:40

HTTP, Content-Type Header.

Adding the right Content-Type header is important for PHP applications to function properly. Yet, surprisingly many still do not add the correct character encoding for the type of content they wish to deliver.

In some cases, people even choose to replace certain characters with HTML encoded alternatives, rather than learn how to properly pick and implement an appropriate character encoding.

In todays globalized world, supporting multiple languages from the beginning of new projects is generally a good practice. It can be difficult to tell when you might need to support special characters from other languages, and so, supporting UTF-8 from the start is a good idea.

For example, to support Danish letters (Æ, Ø, and Å, you can use UTF-8 in the Content-Type header field.

header('Content-Type: text/html; charset=utf-8');

To add a content-type, we can use PHP's header function.

Examples of Mime-Types

The mime-type should be placed before the character encoding. In the above example, we simply use text/html – but there are many others! I included some commonly used ones below:

text/htmlFor .html pages (Note. Extensions are optional on the web).
text/plainPlain text files. If HTML pages are delivered with this, the HTML-source will be shown, without syntax-highlighting.
image/jpegJPEG images. It is possible to output images with PHP as well.
image/pngPNG images. You can also output PNG images in PHP.
image/webpWebP images. Compression is superior to PNG and JPG.
image/avifA1/AVIF images. Compression is superior to most other formats, including WebP.
video/mp4Video format. Useful for streaming.
text/javascriptJavaScript files. Usef for client-sided scripting.
text/cssCSS files. Used to control the styling of web pages.
application/pdfUsed to deliver .pdf files. Yes! You may also create .pdf's in PHP!

It is important you choose the correct mime-type in order for a browser to know how to display the content. PHP does not just deliver HTML and text pages, it can also show images and video!

Character encoding in meta is not necessary

If you expect users to save your web pages locally, then you should be aware that some systems might not save the file in the correct character encoding. In these cases, you can include a meta element in your HTML, declaring the character encoding of the file:

<meta http-equiv="Content-type" content="text/html; charset=utf-8">

If there is a miss-match between the Content-Type declared in your meta element and your HTTP headers, a HTML validator will show a error similar to the below:

The character encoding specified in the HTTP header (iso-8859-1) is different from the value in the element (utf-8).

The solution to this problem is to always make sure both your meta, and HTTP header Content-Type match.

Your CMS should automatically do this for you, but sometimes it may be bugged, or the server might not be configured correctly. Shared hosting solutions can be very bad. It is probably best to host a server on your own, either using a cloud service provider, or on a physical server you own yourself.

Tell us what you think:

  1. In this Tutorial, it is shown how to redirect all HTTP requests to a index.php file using htaccess or Apache configuration files.
  2. How to create a router in PHP to handle different request types, paths, and request parameters.
  3. Tutorial on how to use proxy servers with cURL and PHP
  4. When using file_get_contents to perform HTTP requests, the server response headers is stored in a reserved variable after each successful request; we can iterate over this when we need to access individual response headers.
  5. How to effectively use variables within strings to insert bits of data where needed.

More in: PHP Tutorials