Page 1 of 1

BASE64 Encoding in PHP

Posted: Fri Sep 04, 2009 2:41 am
by Tony
Encoding

Code: Select all

base64_encode ('451'); 
Result will be : NDUx

Decoding

Code: Select all

echo base64_decode('NDUx'); 
Use serialize with base64_encode to convert the more complex data in to network transferable data.
In .Net, this type of encoding is available at Viewstate

The problem here is, when Encoding a text into bas64, the '=' char is added and php scritpt is confused.

To avoid this, the following method can be used.

Code: Select all

function base64_url_encode($input) 
{ 
    return strtr(base64_encode($input), '+/=', '-_,'); 
} 

function base64_url_decode($input) 
{ 
    return base64_decode(strtr($input, '-_,', '+/=')); 
}