开发者

PHP UTF-8 Configuration

开发者 https://www.devze.com 2023-03-06 19:14 出处:网络
I am configuring my Apache/2.2.17 server with PHP 5.3.5. My goal is to create a clean configuration which defaults to the content-type UTF-8.

I am configuring my Apache/2.2.17 server with PHP 5.3.5. My goal is to create a clean configuration which defaults to the content-type UTF-8.

php.ini开发者_开发问答:

default_charset = "UTF-8"
default_mimetype = "application/xhtml+xml"

I receive:

Content-Type: application/xhtml+xml

but require:

Content-Type: application/xhtml+xml; charset=UTF-8

All Apache's configuration (AddDefaultCharset UTF-8) solutions seem not to work, and I have restarted Apache after I edited my php.ini configuration.

PHP documentation:

default_charset string

PHP always outputs a character encoding by default in the Content-type: header. To disable sending of the charset, simply set it to be empty.

I've changed the default_mimetype field to text/html and suddenly, it seems to work: Content-Type:text/html; charset=UTF-8.

Settings the default_mimetype back to application/xhtml+xml will not send the charset=UTF-8. This is without any Apache configuration.

Is PHP broken, or have I missed something?


I think you need to set those parameters in apache configuration, not PHP. Edit apache2.conf or .htaccess file for your project:

AddDefaultCharset utf-8
DefaultType application/xml


PHP Doesn't do this... However, you can use Apache to do this.

Assuming the above, you could use

AddDefaultCharset utf-8
DefaultType application/xhtml+xml

This should appear in your VirtualHost (or server configuration)


I have enabled the iconv module and added to php.ini:

output_handler = ob_iconv_handler

This handler adds the correct character-encoding for output to the Content-Type, instead of the default_charset.

However, you should set default_mimetype, or else it puts Content-Type:;charset=character-encoding.


Beware that if you set mimetype to "application/xhtml+xml", older versions of Internet Explorer will not render the page, but display a save dialog.

To get around this, you can set the mimetype to "text/html", but this is not valid in XHTML 1.1

You can also use content negotiation to tackle the problem.

Read more here: http://www.webstandards.org/learn/articles/askw3c/sep2003/

If it's an option, I would run with "text/html" and use the HTML5 doctype <!DOCTYPE html>. This way, you can still use XHTML syntax and be valid HTML5 (although not technically valid XHTML of course...)

0

精彩评论

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