2

My database communicates incorrectly with my page coded in php. First just some of the special characters displayed incorrectly, but when I applied mysqli_set_charset for my connection and default latin1 was overridden to correct utf8, my problem seemed just to change, but it wasn't solved: whenever I sent a text to database, it ended just before the first non-ASCII character. I searched more and found out that to state the encoding in a meta tag is not enough, that I must use headers. At first, I included header('Content-Type: text/html; charset=utf-8'); statement to the top of some of my pages. Not the texts taken from or inserted into database are correct, but on the rest of the page all special characters display like this: �

Also, having the header function called in every file is not very practical. I'm a beginner with headers, so I don't know how to apply this settings globally. I know .htaccess file can be used for this and my host claims to support it, so I started experimenting with it. I tried all the options suggested here, but nothing changed.

I guess both problems have the same cause: the files I get from the server are in a different charset (I don't have a proof, but the server seem to prefer latin). So how to tell the server I want them in utf8?

Settings as read by apache_request_headers (the important fields):

ACCEPT: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
ACCEPT-LANGUAGE: cs,en-us;q=0.7,en;q=0.3
ACCEPT-ENCODING: gzip, deflate

Only Mozilla Web-Sniffer sees some informations about charsets (and it varies in the languages):

Accept-Charset: ISO-8859-1,UTF-8;q=0.7,*;q=0.7
Accept-Language: de,en;q=0.7,en-us;q=0.3

Here is my .htaccess file:

<FilesMatch ".(html|htm|js|css|php)$">
FileETag None
<ifModule mod_headers.c>
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
AddCharset UTF-8 .php
</ifModule>
</FilesMatch>

Instead of "AddCharset UTF-8 .php" I used alternatives too, but nothing worked. But I'm a beginner, so there might be some mistake.

If I need something completely different, please guide me. Someone suggested XML might help, but here I'm lost even more than with the HTTP headers.

EDIT: I can't see my answer in the linked question. Yes, it guided me to php.ini, but I got only more subquestions, not answers (anything listed there don't work for me).

First, the php.ini file for my server is stored in the part of server I can't access. So I tried to create my own php.ini file in hope it will work. It consists of chunks of code I found elsewhere (I can't confess I understand it):

default_charset = "utf-8";
AddDefaultCharset UTF-8;

[iconv]
iconv.input_encoding = utf-8;
iconv.internal_encoding = utf-8;
iconv.output_encoding = utf-8;

exif.encode_unicode = utf-8;

Again, no joy here. What's wrong?

anubhava
  • 761,203
  • 64
  • 569
  • 643
Pavel V.
  • 2,653
  • 10
  • 43
  • 74
  • Have you added `default_charset = "utf-8";` in your php.ini? – Ja͢ck Dec 18 '13 at 05:39
  • @deceze: this helped me a lot to solve the problem on database side. But I've read it again and I didn't find any hint to the header problem I didn't already try. Perhaps I should spend an hour or two making a comprehensive list of topics I've already read and tried and make a reference list. – Pavel V. Dec 18 '13 at 11:16
  • @deceze: well, so it was there, just I was blind. Thank you. – Pavel V. Dec 18 '13 at 11:42

1 Answers1

1

Try adding these 2 lines on top of your .htaccess:

AddDefaultCharset UTF-8
AddCharset UTF-8 .htm .html .css .js .php

Also try adding this in your HTML's <head> section:

<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
anubhava
  • 761,203
  • 64
  • 569
  • 643