Wednesday, October 14, 2015

Create clean encoded URL variables

If you try to use strings with characters '@,=,+....' etc in URLs in CodeIgniter, it will throw 'URL Disallowed character' error. So you can't pass an email address which is like 'name@email.com' in the CodeIgniter URL.
So the workaround is to encode the desired string, in this case 'name@email.com' pass it to the URL and decode it wherever necessary.

$string = "name@email.com";
$enc = rtrim(base64_encode($string),'=');    //encoded string which can be used in URL

$dec = base64_decode($enc); //decoded string

The function base64_encode($string) encodes the string into 'bmFtZUBlbWFpbC5jb20=' which still contains an '=' character. So use the function rtrim to get rid of that '='.

No comments: