Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid specific characters while encrypting in codeigniter?

I need to pass some encrypted value through URL. Is there any way to avoid some characters such as slash (/) in the value we are getting after encryption? Because in codeigniter, character such as slash is used for separating parameters in the URL. Please note that i don't want any suggestion to don't pass the encrypted string in URL :)

like image 813
Stranger Avatar asked Dec 27 '22 01:12

Stranger


2 Answers

Use the PHP urlencode function after encryption: http://php.net/manual/en/function.urlencode.php and use urldecode in the script that's processing the GET data.

like image 133
Daan Avatar answered Feb 21 '23 05:02

Daan


class MY_Encrypt extends CI_Encrypt
{

    function encode($string, $key="", $url_safe=TRUE)
    {
        $ret = parent::encode($string, $key);

        if ($url_safe)
        {
            $ret = strtr(
                    $ret,
                    array(
                        '+' => '.',
                        '=' => '-',
                        '/' => '~'
                    )
                );
        }

        return $ret;
    }


    function decode($string, $key="")
    {
        $string = strtr(
                $string,
                array(
                    '.' => '+',
                    '-' => '=',
                    '~' => '/'
                )
            );

        return parent::decode($string, $key);
    }
}

-

$key = $this->config->item('encryption_key');

$outboundlink = urlencode( $this->encrypt->encode($segment, $key, TRUE) );

$inboundlink  = rawurldecode( $this->encrypt->decode( $segment, $key) );
like image 21
Philip Avatar answered Feb 21 '23 05:02

Philip