...

View Full Version : base64 encoding / JS and PHP



ghernando
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
http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_base64_decode/

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):
wqvQndCTwrs6INC/0L7Qu9C90L7QvNC+0YfQuNGPINCg0L7RgdGB0LjQuCDQvdC1INCx0YPQtNGD0YIg0L/RgNC40L7RgdGC0LDQvdC+0LLQu9C10L3RiyDQvdCwINCx0LvQuNC20LDQudGI0LXQuSDRgdC10YHRgdC40Lgg0J/QkNCh0JU=

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.

ess
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

http://bugs.php.net/bug.php?id=37244

~E

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

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum