URL parameter encoding and decoding using base64

Some time we need ro pass some parameter in the URL to the web site users.
Do the following thing for encrypting and decrypting URL parameter.

* URL Friendly version of base64_encode.
function encode($input)
    return strtr(base64_encode($input), '+/=', '-_a');

* URL Friendly version of base64_decode.
function decode($input)
    return base64_decode(strtr($input, '-_a', '+/='));

Just and example. In the email template here is a link called “unsubscribe”. The actual link is like that = http://yourdomain.com/index/unsubscribe/13668.

here Domian name = http://yourdomain.com.
index = Controller name.
unsubscribe = function / action name.
13668 = user id.

If we send this link to the user then he might know than 13668 is his user id or some other id. I think this is the risk. So how can we send an encrypted URL (parameter)-

URL need to send – http://yourdomain.com/index/unsubscribe/13668

$id = 13668;
$enc_id = strtr(base64_encode($id), ‘+/=’, ‘-_a’); //output = MTM2Njga

Now we can send this link with encrypted id.


How can we recover it?

get the id from the URL and then do the following thing.

$id = “MTM2Njga”;

$dec_id = base64_decode(strtr($id, ‘-_a’, ‘+/=’)); // output = 13668

So we get the actual id. Now we can use this id in our next query.