javascript's escape and encodeURI vs. PHP $_POST

I just stumbled upon an odd encoding issue with a web application.

Basically, data is coming into our PHP application through a Javascript's XMLHttpRequest (ajax). The data is sent as a standard form encoding (application/x-www-form-urlencoded), and picked up by PHP using the $_POST array. Any strings in form POST request are 'urlencoded', also known as Percent-encoding. As an example, this will turn a space into the often-seen %20.

Normally everything in the $_POST and $_GET arrays is already decoded, so when you're dealing with these arrays you don't really have to think about this. This time however, I was dealing with some non-latin unicode characters and for some reason they were never decoded and ended up in de database as raw url-encoded strings.

Doing a bit of research led me to the following: normally any special character is encoded as %XX, X and X being 2 hexadecimal values. These values simply represent bytes. The values I got were different altogether and took the form %uXXXX. I just assumed this was part of standard uri-encoding for unicode characters, so I was still a bit shook-up to see that PHP didn't just pick them up.

After a bit of research, I found out that the unicode representation was rejected by W3c, which is probably also why the PHP authors decided to not implement this. Javascript actually has 2 different methods to do percent-encoding, namely:

escape("☢"); // returns %u2622
encodeURI("☢"); // returns %E2%98%A2

Guess which one we were using?

Even though the %u syntax is arguably better to represent unicode characters, W3c seems to have voted against the syntax for backwards compatibility reasons. Before this happened the escape method was already adopted in javascript which in turn caused me to stumble upon this problem and write an article about it.

The more you know..


8 Responses to javascript's escape and encodeURI vs. PHP $_POST

  1. 1039 Rob 2010-01-07 11:58 am

    Wow imagine that, once again a web developer is completely screwed because of stupid people who use stupid charecters in their own special stupid languages!

    Here in Belgium with two languages we deal with these sorts of problems all the time. That's why next to my keyboard on my desk I have a big red button labeled:

    "Kill all french speaking people and their stupid language"

    I press it at least once per day. If I close my eyes and see the nukes going off I actually start to feel better!

    regardless sending text to PHP from a JS via Ajax there are some weird encode issues. I remember having this issue with the pipe character '|'. I thought in addition to encodeURI there was also another encodeXXX function floating around did the correct thing.

  2. 1040 Evert 2010-01-07 1:07 pm

    Well, I think the original character sets were rather discriminatory against non-english language.

    This isn't just about a few accented characters from french, but includes chinese, japanese, etc..

    Ignoring there's people who don't just use the 26 letter alphabet kinda reduces your market.

  3. 1041 Geoff 2010-01-07 4:22 pm

    Great information... good article...
    but what's with link to the article... in the article? Seems a bit loopy (pun intended) and redundant.

  4. 1042 Evert 2010-01-07 4:31 pm

    Yea sorry about that.. I was a bit tired and it made me giggle. I was hoping it would throw people off :)

  5. 1043 Rene Pot 2010-01-07 6:52 pm

    Might come in handy someday! Thanks!

  6. 1044 Md. Arif Ul Hoque 2010-01-08 7:13 am

    Thanks for great post.

  7. 1038 Ben Klang 2010-01-09 4:10 am

    From my reading of encodeURI() it does encode most characters, but not several important ones. Perhaps most importantly, encodeURI() does not encode ampersand (&) where escape() does. For at least my uses, this makes encodeURI() not a drop-in replacement for escape. However, there is encodeURIComponent() that does cover the remaining characters. So far I think this is a winner, but perhaps someone will correct me.

  8. 1329 Evert 2010-04-13 10:41 am

    Ben,

    A little late but you were right. I changed encodeURI to encodeURIComponent, after finding out it had issues with

    You win



About

My name is Evert, and I've been writing semi-regularly on this blog since 2006.

I'm currently available for contract work.

more info.

Subscribe

Dropbox

Dropbox is a simple cross-platform online backup and sync application. The first 2GB of space is free, and both you and me get an extra 250MB extra space if you sign up through this link.