How to set HTTP header to UTF-8 using PHP which is valid in W3C validator

PhpHtmlHttp Headers

Php Problem Overview


I have several PHP pages echoing out various things into HTML pages with the following code.

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

However, when I validate using the W3C validator it comes up with:

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

I am quite new to PHP, and I was wondering if I could and should change the header for the PHP files to match the HTML files.

Php Solutions


Solution 1 - Php

Use header to modify the HTTP header:

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

Note to call this function before any output has been sent to the client. Otherwise the header has been sent too and you obviously can’t change it any more. You can check that with headers_sent. See the manual page of header for more information.

Solution 2 - Php

First make sure the PHP files themselves are UTF-8 encoded.

The meta tag is ignored by some browser. If you only use ASCII-characters, it doesn't matter anyway.

http://en.wikipedia.org/wiki/List_of_HTTP_header_fields

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

Solution 3 - Php

This is a problem with your web server sending out an HTTP header that does not match the one you define. For instructions on how to make the server send the correct headers, see this page.

Otherwise, you can also use PHP to modify the headers, but this has to be done before outputting any text using this code:

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

More information on how to send out headers using PHP can be found in the documentation for the header function.

Solution 4 - Php

You can also use a shorter way:

<?php header('Content-Type: charset=utf-8'); ?>

See RFC 2616. It's valid to specify only character set.

Solution 5 - Php

For a correct implementation, you need to change a series of things.

Database (immediately after the connection):

mysql_query("SET NAMES utf8");

// Meta tag HTML (probably it's already set): 
meta charset="utf-8"
header php (before any output of the HTML):
header('Content-Type: text/html; charset=utf-8')
table-rows-charset (for each row):
utf8_unicode_ci

Solution 6 - Php

PHP sends headers automatically if set up to use internal encoding:

ini_set('default_charset', 'utf-8');

Solution 7 - Php

As explained on http://php.net/default-charset,

> the "UTF-8" is the default value and its value is used as the default > character encoding for htmlentities(), html_entity_decode() and > htmlspecialchars() if the encoding parameter is omitted.

It is set on default php.ini as "UTF-8" on the "Data handling" section as:

; PHP's default character set is set to UTF-8.
; http://php.net/default-charset
default_charset = "UTF-8"

Also, you can set, before the content, the header to another encoding as needed:

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

or

header('Content-Type: text/html; charset=iso-8859-1');

or any other charset you need to declare.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmanycheeseView Question on Stackoverflow
Solution 1 - PhpGumboView Answer on Stackoverflow
Solution 2 - PhpKingCrunchView Answer on Stackoverflow
Solution 3 - PhpEdoDodoView Answer on Stackoverflow
Solution 4 - PhpJason OOOView Answer on Stackoverflow
Solution 5 - PhpUnChien AndalouView Answer on Stackoverflow
Solution 6 - PhpNiklView Answer on Stackoverflow
Solution 7 - PhpRodolfo Bojo PellegrinoView Answer on Stackoverflow