00 Votes

PHP: Set UTF-8 Charset in HTTP Header

Question by Guest | 2015-07-03 at 22:00

Up to now, I have always specified the Charset (the character encoding) as meta tag within the head of my HTML page.

That is, for example

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

in HTML 4 or correspondingly the same in HTML 5:

<meta charset="UTF-8">

However, obviously, my server is nevertheless transmitting that my page is encoded in Latin 1 or ISO 8859-1. I do not know whether this can lead to problems in the browser, but I get the following error message when using an HTML Validator: The character encoding specified in the HTTP header (iso-8859-1) is different from the value in the element (utf-8).

Is there any possibility to modify the header with PHP so that the server is specifying UTF-8 as charset and content type correctly?

0Best Answer0 Votes

You can modify the header using the PHP function header(). In your case, that is the following line of code:

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

Important: You have to call this function before generating any output on your page. That means, before any echo and also before each character or space eventually written before your PHP code in your HTML page. Otherwise, the header would have already been sent and it would not be possible to change it anymore.
2015-07-06 at 09:58

ReplyPositive Negative

Related Topics

PHP: Sending an E-Mail

Tutorial | 0 Comments

PHP: File Download Script

Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.


Ask your own question or write your own article on askingbox.com. That’s how it’s done.