Pindatjuh
Pindatjuh

Reputation: 10526

PHP UTF-8 Configuration

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?

Upvotes: 4

Views: 3289

Answers (4)

Pindatjuh
Pindatjuh

Reputation: 10526

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.

Upvotes: 0

Adrian Schmidt
Adrian Schmidt

Reputation: 1895

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...)

Upvotes: -1

Mez
Mez

Reputation: 24951

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)

Upvotes: 1

Silver Light
Silver Light

Reputation: 46002

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

Upvotes: 4

Related Questions