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.
#2

[eluser]djalilk[/eluser]
Thanks a lot , it is very helpful for me Smile
#3

[eluser]neomech[/eluser]
Quote:I also have to use this in place of urlencode(), because for some reason, my server decodes the encoded URL automatically.

I wonder if that's really what's happening, or if you had the same problem I did, which was that Firefox will "decode" it for you so it shows up pretty in your URL, but it's not actually been decoded. I suddenly remembered reading something about that, checked Internet Explorer, and voila, everything is showing up nice and url-stringy like it's supposed to. Silly Firefox! Wink
#4

[eluser]KuLi[/eluser]
You can use

$this->input->server('QUERY_STRING', TRUE)

After that you can explode the String.




Theme © iAndrew 2016 - Forum software by © MyBB