BASE64 Encoding in PHP

Post Reply
Tony
Lieutenant
Lieutenant
Posts: 86
Joined: Tue Jul 21, 2009 4:11 pm

BASE64 Encoding in PHP

Post by Tony » Fri Sep 04, 2009 2:41 am

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, '-_,', '+/=')); 
} 
 
Post Reply

Return to “PHP & MySQL”