Welcome Guest, Not a member yet? Register   Sign In
URI-safe base64
#1

[eluser]TheFuzzy0ne[/eluser]
I wanted to be able to pass base64 string through the URI, but whenever it contained a '/', '+' or '=' it either broke my app, or moaned about invalid characters.

The same applied to sending encrypted strings. These also broke my application, or used invalid URI characters.

I designed two simple functions to strip out the illegal characters, and replace them with something that was allowed, and of course a function to do the reverse. I also have to use this in place of urlencode(), because for some reason, my server decodes the encoded URL automatically.

Here are the functions:
./system/application/helpers/encode_helper.php
Code:
<?php

/**
* Encodes a string as base64, and sanitizes it for use in a CI URI.
*
* @param string $str The string to encode
* @return string
*/
function url_base64_encode($str = '')
{
    return strtr(base64_encode($str), '+=/', '.-~');
}

/**
* Decodes a base64 string that was encoded by url_base64_encode.
*
* @param object $str The base64 string to decode.
* @return string
*/
function url_base64_decode($str = '')
{
    return base64_decode(strtr($str, '.-~', '+=/'));
}

// End of file: encode_helper.php
// Location: ./system/application/helpers/encode_helper.php

I hope someone finds them useful.


Messages In This Thread
URI-safe base64 - by El Forum - 03-05-2009, 11:16 AM
URI-safe base64 - by El Forum - 06-22-2009, 11:43 AM
URI-safe base64 - by El Forum - 09-22-2009, 01:01 AM
URI-safe base64 - by El Forum - 09-22-2009, 01:15 AM



Theme © iAndrew 2016 - Forum software by © MyBB