浏览器在 UTF-8 cakePHP 应用程序上检测到 ISO-8859-1 编码

发布于 2024-12-08 20:13:39 字数 973 浏览 0 评论 0原文

在我的客户端服务器上,当我浏览应用程序时,字符是错误的,因为所有浏览器(Firefox、Chrome、IE)将页面解码为 ISO-8859-1 而不是 UTF-8。本地工作得很好,在我的服务器上也工作得很好。

我有一个使用cakePHP 1.3.12开发的应用程序:

  • 所有文件的默认编码都是UTF-8无BOM。
  • 所有页面都有 meta http-equiv="Content-Type" content="text/html; charset=UTF-8"
  • 在 core.php

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

    var $default = 数组(
        '司机' => 'mysql',
        '持久' =>错误的,
        '主机' => '本地主机',
        '登录' => 'aaa',
        '密码' => 'aaa',
        '数据库' => 'aaa',
        '前缀' => '应用程序_',
        '编码' => 'utf8'
    );
    
  • 数据库、表和字段排序规则是utf8_unicode_ci

我还把它放在bootstrap.php的开头:

echo mb_internal_encoding();

.. .并返回 ISO-8859-1,所以我输入...

mb_internal_encoding('UTF-8');

...但没有任何改变。

运行不良的服务器有 PHP 5.2.16。我认为这是客户端服务器上的模块或选项,因为本地和我的服务器工作正常。

任何想法表示赞赏。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仙女 2024-12-15 20:13:39

我通过放入 app/config/bootstrap.php 文件的第一行解决了这个问题:

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

简单,甚至看起来很明显,但在这个托管中,cakePHP 应用程序没有按预期工作。响应标头始终回答 Content-Type ISO-8859-1。现在,经过此更改,它回答了 UTF-8。

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.

初心未许 2024-12-15 20:13:39

您可以尝试检查服务器上的 .htaccess 文件是否添加了类似的内容:

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

Mime 类型可能不同,语法存在一些变化,但看起来很相似。

这将使用 .htaccess 中设置的全局字符集覆盖文件字符集

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

幻梦 2024-12-15 20:13:39

数据库排序规则可能与您的 information_schema 中的 mysql 用户的排序规则不同(使用 phpmyadmin 来检查这一点)。

如果您没有更改这些设置所需的权限,这可能会解决您的问题,假设您的页面标题 charset 是 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>";

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>";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文