3

I have recently updated my PHP-Webspace to 5.6 (from 5.5) and something strange happened. My website suddenly displays UTF-8 characters incorrectly eventhough I am using the Meta Tag <meta charset="utf-8"> and following line in my .htaccess file AddDefaultCharset utf-8.

After inserting following line in my PHP-Script everything worked again header('Content-Type: charset=utf-8');.

This both happened on my Webspace (where I don't have access to the php.ini file) and my local server. As I am also using other PHP scripts, I don't want to insert header('Content-Type: charset=utf-8'); into all config files...

What could be the cause?

Deproblemify
  • 3,340
  • 5
  • 26
  • 41
  • Just `Content-Type: charset=utf-8` is invalid, I don't know how a browser would interpret that. Perhaps you're actually overriding the correct UTF-8 headers so that the browser is now *not* interpreting your site as UTF-8. If it works when your site is *not* interpreted as UTF-8 that means your site is not actually UTF-8 encoded. Test that first by explicitly selecting an encoding in the browser in View → Encoding. – deceze May 22 '15 at 12:01
  • 1
    http://stackoverflow.com/a/9351983/953684 – Sharky May 22 '15 at 12:04
  • @deceze You might have misunderstood me. It only works if I do `Content-Type: charset=utf-8`. – Deproblemify May 22 '15 at 12:24
  • And so I'm telling you that this may mean that your site is not actually being interpreted as UTF-8, since that's an invalid Content-Type header. Which may mean that your site is not actually UTF-8 encoded. Which would point to some pretty deep seated problems. – deceze May 22 '15 at 12:26
  • @deceze I understand what you mean. But like I explained it all worked until I upgraded from PHP 5.5 to 5.6. To make it work again, I added the header in PHP (which by the way suprised me that it worked). Could it be related to the way I uploaded the files via FTP? – Deproblemify May 22 '15 at 18:05
  • Let's put it this way: "it worked before" doesn't help getting to the bottom of this. Step one: select the encoding in your browser's View menu to check which encoding the site looks okay in. Then debug from there. – deceze May 22 '15 at 19:11
  • OK, I created a test document and tested it locally and on my hosted webspace. Locally it worked fine without the extra header call but on the hosted one it was sent windows-1252 encoding. I will also check with my provider. – Deproblemify May 23 '15 at 09:55
  • For future readers: It turned out that it had something to do with uploading method (binary or ASCII). After reuploading it worked. Maybe someone wants to add this as the answer and explain it deeply? – Deproblemify Dec 09 '15 at 17:01

2 Answers2

0

As I have read:

the default_charset php.ini setting changed in PHP 5.6 from empty to UTF-8. This may break HTML output if you try to set a different charset in your HTML head. It may also break functions like htmlentities() and htmlspecialchars.

src: https://www.saotn.org/php-56-default_charset-change-may-break-html-output/

In my case I used to set the default charset to UTF-8 by using the php function mb_http_output('UTF-8');, though for PHP5.6 I removed that function since it seemed redundant. It made the chars show up readable again.

Frankey
  • 3,679
  • 28
  • 25
-1

If you do not want to use header() function of php then you can use meta tag of html.

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

Please check if this helps.

Deep Kakkar
  • 5,831
  • 4
  • 39
  • 75
  • @DeepKakkar deceze is right. It's not about the meta tag (I used both but now use the one in the question), it must be a PHP problem as it worked before. – Deproblemify May 22 '15 at 12:18
  • have you had upgraded only php or mysql version too ? In case of mysql mysql_set_charset('utf8',$link); add this as the first query after connect. and in case of PDO you can use in this way $options = array( PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', ); $dbh = new PDO($dsn, $username, $password, $options); – Deep Kakkar May 22 '15 at 12:20
  • @DeepKakkar I only update PHP, I already set the mysql charset anyway and I use mysqli not PDO. – Deproblemify May 22 '15 at 18:08