views:

2654

answers:

8

I'm setting up a new server, and want to support UTF-8 fully in my web application. I have tried in the past on existing servers and always seem to end up having to fall back to ISO-8859-1.

Where exactly do I need to set the encoding/charsets? I'm aware that I need to configure Apache, MySQL and PHP to do this - is there some standard checklist I can follow, or perhaps troubleshoot where the mismatches occur?

This is for a new Linux server, running MySQL 5, PHP 5 and Apache 2.

A: 

As far as I know, UTF8 is the standard encoding for php's internals. What does matter though is the charset in the headers sent by the server.

In the php.ini file, there must be something like default_charset, which sets the default encoding in the headers.

For apache 2.0 , you can specify AddDefaultCharset in the httpd.conf.

And for MySQL, the encoding is specified on four different levels, so you must verify all four (server, database, table, and column) of them.

Berzemus
It's not. But it will be from php 6
troelskn
A: 

In PHP, you'll need to either use the multibyte functions, or turn on mbstring.func_overload. That way things like strlen will work if you have characters that take more than one byte.

You'll also need to identify the character set of your responses. You can either use AddDefaultCharset, as above, or write PHP code that returns the header. (Or you can add a META tag to your HTML documents.)

JW
+4  A: 

In addition to setting default_charset in php.ini, you can send the correct charset using header() from within your code, before any output:

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

Working with Unicode in PHP is easy as long as you realize that most of the string functions don't work with Unicode, and some might mangle strings completely. PHP considers "characters" to be 1 byte long. Sometimes this is okay (for example, explode() only looks for a byte sequence and uses it as a separator -- so it doesn't matter what actual characters you look for). But other times, when the function is actually designed to work on characters, PHP has no idea that your text has multi-byte characters that are found with Unicode.

A good library to check into is phputf8. This rewrites all of the "bad" functions so you can safely work on UTF8 strings. There are extensions like the mbstring extension that try to do this for you, too, but I prefer using the library because it's more portable (but I write mass-market products, so that's important for me). But phputf8 can use mbstring behind the scenes, anyway, to increase performance.

Christopher Nadeau
+1  A: 

Good goal to have from the start - based on the nature of your site, I've found lots of resources about this by Googling - you're not the first to deal with it, of course.

The mystical PHP6 is supposed to have all this straightened out, right?

You can pretty much set utf-8 as the global default charset for mysql at the server level and it will default properly to the more granular levels.

le dorfier
+49  A: 

Storage:

  • Specify utf8_unicode_ci (or equivalent) collation on all tables and text columns in your database. This makes MySQL physically store and retrieve values natively in UTF-8.

Retrieval:

  • In PHP, in whatever DB wrapper you use, you'll need to set the connection charset to utf8. This way, MySQL does no conversion from its native UTF-8 when it hands data off to PHP.
  • Note that if you don't use a DB wrapper, you'll probably have to issue a query to tell MySQL to give you results in UTF-8: SET NAMES 'utf8' (as soon as you connect).

Delivery:

  • You've got to tell PHP to deliver the proper headers to the client, so text will be interpreted as UTF-8. In PHP, you can use the default_charset php.ini option, or manually issue the Content-Type header yourself, which is just more work but has the same effect.

Submission:

  • You want all data sent to you by browsers to be in UTF-8. Unfortunately, the only way to reliably do this is add the accept-charset attribute to all your <form> tags: <form ... accept-charset="UTF-8">.
  • Note that the W3C HTML spec says that clients "should" default to sending forms back to the server in whatever charset the server served, but this is apparently only a recommendation, hence the need for being explicit on every single <form> tag.
  • Although, on that front, you'll still want to verify every submitted string as being valid UTF-8 before you try to store it or use it anywhere. PHP's mb_check_encoding() does the trick, but you have to use it religiously.

Processing:

  • This is, unfortunately, the hard part. You need to make sure that every time you process a UTF-8 string, you do so safely. Easiest way to do this is by making extensive use of PHP's mbstring extension.
  • PHP's string operations are NOT by default UTF-8 safe. There are some things you can safely do with normal PHP string operations (like concatenation), but for most things you should use the equivalent mbstring function.
  • To know what you're doing (read: not mess it up), you really need to know UTF-8 and how it works on the lowest possible level. Check out any of the links from utf8.com for some good resources to learn everything you need to know.
  • Also, I feel like this should be said somewhere, even though it may seem obvious: every PHP or HTML file you'll be serving should be encoded in valid UTF-8.
chazomaticus
collation is not the same as encoding. Be sure to set the encoding to utf8, in your database. Collation is less important.
troelskn
It's my understanding that if you specify the collation as utf8_*, it automatically encodes as utf8 as well. Is this wrong?
chazomaticus
I'm not wrong: COLLATE implies CHARACTER SET. See e.g. http://dev.mysql.com/doc/refman/5.0/en/charset-database.html.
chazomaticus
Cool - didn't know that.
troelskn
The ‘submit forms in the same encoding as the page they came from’ recommendation does work in every browser since Unicode was invented. `accept-charset="UTF-8"`, unfortunately, does not. In IE, this will make submissions use UTF-8 not as a replacement encoding, but as a backup encoding, for fields that can't be encoded in the primary (page) encoding. And it doesn't tell you which encoding it used, so you can't recover the information reliably. On pages that are already UTF-8 it has no effect; on pages with other charsets it is actively harmful. Avoid.
bobince
Regarding `SET NAMES` please have a read of http://ilia.ws/archives/103-mysql_real_escape_string-versus-Prepared-Statements.html and http://docs.php.net/mysql_set_charset (if you're using php-mysql and not prepared statements)
VolkerK
+1  A: 

Unicode support in PHP is still a huge mess. While it's capable of converting an ISO8859 string (which it uses internally) to utf8, it lacks the capability to work with unicode strings natively, which means all the string processing functions will mangle and corrupt your strings. So you have to either use a separate library for proper utf8 support, or rewrite all the string handling functions yourself.

The easy part is just specifying the charset in HTTP headers and in the databsae and such, but none of that matters if your PHP code doesn't output valid UTF8. That's the hard part, and PHP gives you virtually no help there. (I think PHP6 is supposed to fix the worst of this, but that's still a while away)

jalf
+3  A: 
mercator
Thanks! And, good call.
chazomaticus
Shouldn't it also be possible to specify the charset in the HTTP headers? Probably needs some config option for the webserver...
oliver
@oliver: Yes you can send it in the HTTP header, but it's better to send it in the content because if the client saves the file, it'll always save the meta tag. A HTTP header is likely to just disappear unless the browser is smart enough to copy it into a meta tag in the saved file.
Ant P.
Also, make sure that line is the first child of head element (before any Unicode stuff). The browser may reinterpret the page after hitting that meta element described above.
alex
A: 

if you're too lazy to do those written above... you could just create your tables as you wish and make your forms accept their default browser set encodings then use this function to parse all inputs

foreach($_POST as $varname => $value) {
   $$varname = utf8_decode(clean($value));
}
lock