13

I have page which encoding is declared with

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

But when I enter the page another encoding (ISO) is chosen in browser. I have tried to set encoding by PHP method

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

But it also didn't help. All source files are encoded in UTF-8 without BOM. The only solution which I tried and it had worked was setting encoding in .htaccess file by adding AddDefaultCharset UTF-8 line, but then another pages on the server were not displayed correctly. How can I solve this problem?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
mkas
  • 513
  • 2
  • 6
  • 15
  • 4
    Please note that the meta content type tag is **ignored** when the page is served over HTTP. It's only used when you let the browser save the page to disk and then open it from the disk by `file:///`. The charset as specified in `charset` attribute of the HTTP response `Content-Type` header is been used when the page is served over HTTP. Perhaps you're setting the header too late in PHP (it has to be set before you write any bit to the response, you would however have been notified of this mistake if you have set the proper PHP error reporting level). – BalusC Oct 18 '11 at 15:42
  • How are you able to identify the encoding is different? Are characters not displaying appropriately? Is it SQL DB fetched text, in which case mysql_set_charset can help... – SW4 Oct 18 '11 at 15:49
  • 2
    @Ergo: that wouldn't have been solved by setting `AddDefaultCharset UTF-8` in `.htaccess`. – BalusC Oct 18 '11 at 15:49
  • @BalusC: the php header was sent as first bits, nothing before it was sent. I know that wrong encoding is chosen beacause I can see it in Firefox: [screen](http://i.imgur.com/w98Km.jpg) (Sorry for non-English screen, chosen menu items says: developer tools -> characters set -> unicode). In my page ISO-8859-2 is set by default, but when I click UTF-8 all special characters are displayed correctly. – mkas Oct 18 '11 at 16:01
  • Apparently the `header()` which you set in PHP was been overriden somewhere else. Check the actual response header value which your browser has retrieved. You can check that in for example Firebug's *Net* panel. – BalusC Oct 18 '11 at 16:08
  • You've **two** charset attributes in `Content-Type` header and the last one of `ISO-8859-2` apparently got precedence over the first one of `UTF-8`. This is a misconfiguration in the webserver or proxy used. You can't do anything against it from in the PHP/HTML side. Edit: why did you delete the comment? – BalusC Oct 18 '11 at 16:18
  • Here is header response `Date Tue, 18 Oct 2011 16:11:10 GMT Server Apache/2.2.6 (Fedora) X-Powered-By PHP/5.1.6 Content-Length 7967 Content-Type text/html; charset: utf-8; charset=ISO-8859-2` – mkas Oct 18 '11 at 16:21
  • Excuse me for deleting previous comment, I wanted to correct formatting. Thanks for your help. I have to check server configuration then. – mkas Oct 18 '11 at 16:24
  • You could just click the "edit" link at end of the comment to edit comments. You can edit comments within 5 minutes after posting the comment. – BalusC Oct 18 '11 at 16:25
  • 1
    @BalusC: `` **is** used to determine the character encoding of a page, even if the page is served over HTTP. However, the Content-Type header takes precedence over the `` tag, if it is present. See http://www.w3.org/TR/html401/charset.html#h-5.2.2 – Thanatos Oct 19 '11 at 07:09

1 Answers1

8

Disable default charset:

AddDefaultCharset Off
Gerben
  • 16,747
  • 6
  • 37
  • 56
  • I reckon, that that will cause that other pages on server which have to be encoded in ISO will not be displayed correctly. (Similarly to `AddDefaultCharset UTF-8`.) – mkas Oct 18 '11 at 16:38
  • If apache doesn't send the charset in the headers, the browsers will guess the encoding. Most likely they will guess iso-8859-2. – Gerben Oct 18 '11 at 16:47
  • You are right, it helped. I hope it is valid and in accordance with strandards. – mkas Oct 19 '11 at 06:11
  • 1
    Don't worry about it being invalid. It is the default value of apache. Somehow your server was configured differently. So you are good now. – Gerben Oct 19 '11 at 11:09
  • Where? What? How? – ˈvɔlə Feb 26 '22 at 08:11