开发者

Browser detects ISO-8859-1 encoding on UTF-8 cakePHP app

On the server of my client开发者_运维百科, when I browse the application, the characters are wrong, because all of the Browsers (Firefox, Chrome, IE) decode the page as ISO-8859-1 instead of UTF-8. Local works great, and on my server works fine too.

I have an application developed with cakePHP 1.3.12:

  • The default encoding of all files is UTF-8 without BOM.
  • All pages has meta http-equiv="Content-Type" content="text/html; charset=UTF-8"
  • In core.php

    Configure::write('App.encoding', 'UTF-8');
    
  • In database.php

    var $default = array(
        'driver' => 'mysql',
        'persistent' => false,
        'host' => 'localhost',
        'login' => 'aaa',
        'password' => 'aaa',
        'database' => 'aaa',
        'prefix' => 'app_',
        'encoding' => 'utf8'
    );
    
  • The database, tables and fields collation is utf8_unicode_ci

I also put on the beginning of bootstrap.php:

echo mb_internal_encoding();

...and returns ISO-8859-1, so I put...

mb_internal_encoding('UTF-8');

...but nothing change.

The server that work bad has PHP 5.2.16. I think it's a module or option on the client server, because local and in my server works fine.

Any idea is appreciated.


I solved the problem by putting in the first line of app/config/bootstrap.php file:

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

Simple and it even seems obvious, but in this hosting, the cakePHP application did not work as expected. The response header always answer Content-Type ISO-8859-1. Now with this change, it answered UTF-8.


You could try checking if the .htaccess file on the server has something like this added :

AddType 'text/html; charset=ISO-8859-1' html

Mime type could be different and some variatons in syntax exists, but it would look similar.

This would overwrite the files charset with the global charset set in .htaccess


the database collation might differ from the collation for the mysql user in your information_schema (use phpmyadmin to check this).

in case you dont have the necessary privileges to change these settings, this might solve your problem, supposing that your page header charset is utf-8:

// Opens a connection to a MySQL server
$connection = mysql_connect ($server, $username, $password);
$charset = mysql_client_encoding($connection);
$flagChange = mysql_set_charset('utf8', $connection);
echo "The character set is: $charset</br>mysql_set_charset result:$flagChange</br>";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜