Adding Content-Type Header from PHP
How to correctly output different content-types using the PHP header function.
Edited: 2021-03-18 08:40
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/html | For .html pages (Note. Extensions are optional on the web). |
text/plain | Plain text files. If HTML pages are delivered with this, the HTML-source will be shown, without syntax-highlighting. |
image/jpeg | JPEG images. It is possible to output images with PHP as well. |
image/png | PNG images. You can also output PNG images in PHP. |
image/webp | WebP images. Compression is superior to PNG and JPG. |
image/avif | A1/AVIF images. Compression is superior to most other formats, including WebP. |
video/mp4 | Video format. Useful for streaming. |
text/javascript | JavaScript files. Usef for client-sided scripting. |
text/css | CSS files. Used to control the styling of web pages. |
application/pdf | Used 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: