View Full Version : base64 encoding / JS and PHP

09-25-2008, 11:21 PM
I'm trying to be able to pass UTF-8 data from the client using Javascript to PHP code on the server.
I've decide to base64 encode the data being passed.

In Javascript, I've tried using both of these:
http://www.webtoolkit.info/demo/javascript/base64/demo.html and

Both seem to be producing equivalent results.

My problem is with some non-English strings that I encode in Javascript, base64_decode() will fail in PHP.
The base64_decode in Javascript returns the same string.

For example, the Russian string:
НГ: полномочия России не будут приостановлены на ближайшей сессии ПАСЕ

base 64 encodes into (Javascript):

But that base64 string in php fails when I run base64_decode().
It returns false with no decoded string results.

I'd appreciate help or suggestions about this.

09-25-2008, 11:26 PM
Are you using PHP version 5.1.2. If so, there is a bug in base64_decode. please see



09-26-2008, 05:25 AM
The version I'm using of PHP is 5.2.3-1ubuntu6.3

I've been spinning my wheels and I can't quite isolate one character or groups of characters of the original string that are causing the problem.

I haven't seen any problems with English.

rnd me
09-26-2008, 08:37 AM
window.escape() needs no client code, is universally supported, is remarkably fast, and char independent.

might be worth extra 10-30% bandwidth to avoid headaches...

09-27-2008, 06:13 AM
escape() doesn't work with UTF-8.

rnd me
09-27-2008, 08:30 AM
escape() doesn't work with UTF-8.

in that case, encodeURIComponent() is about as well supported.
It's a bit slower than escape, but still a lot faster than anything written in javascript would be.