4

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?

3
  • 1
    I would probably recommend to manually set the encoding in your application instead, even though you hate it ;) Makes your application more portable. Commented May 19, 2011 at 8:25
  • 2
    Content-Encoding does not specify the content’s character encoding but the additional content encoding that has been applied to the entity body like gzip or deflate. Commented May 19, 2011 at 8:32
  • @Gumbo thanks for the pointer. I've accidentally mixed character-encoding and content-encoding. I've updated the question. Commented May 19, 2011 at 8:39

4 Answers 4

4

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
Sign up to request clarification or add additional context in comments.

Comments

1

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)

Comments

0

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.

Comments

-1

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

8 Comments

Luckily Internet Explorer 6 is my minimal requirement, and your source says it's not recognized up to 6. So I'm safe. Thanks for pointing out.
Yes, I saw that too, but I'm pretty sure I've run into the problem myself, and I have never (in modern times) tested in anything older than IE6, so I would not be at all confident that this is not a problem in IE678...
And it says "at least up to IE 6.x" with an implicit inclusion of IE6 (look at the table later in the post). Also, the post is from 2003, so this might well be a problem in later versions too...
However: HTML5 is not used in IE6. So how can I be valid XHTML in IE6?
You can still use the HTML5 doctype with IE6. It will automatically trigger standards-mode in all browsers :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.