Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to encode/decode url in codeigniter

I am sending a link to my user's email to activate his account by click that link. example link: http://test.com/welcome/make_active_user/($user_id)

then I encode $user_id in my controller, after that my link looks like the following one

http://test.com/welcome/make_active_user/17elQOjxrOXDsZdDZVFY7JFrB0TJFojdS+5OTpiIq/XXIaVrDfVWQ7xgOXXqGsURIFe/Udvm/XHrNWtbZXFA2g==

upto this everything is fine. Now i want to decode the $user_id, but "/" symbol create problem. codeigniter took only those characters before the "/" symbol. How can i get a output of encoded user_id without "/"

I use "$this->encrypt->encode($user_id,$key);" in my controller

Please help

like image 475
Sumon Avatar asked Mar 23 '15 13:03

Sumon


3 Answers

Just pass your encoded $user_id to the php function urlencode() before sending in a link like this:

$user_id_link = urlencode($user_id);

And decode the string you get back from the link with url decode() like this:

$user_id = urldecode($user_id_link);
like image 143
MDeuerlein Avatar answered Oct 17 '22 06:10

MDeuerlein


You need to modify the encrypt class to encode url safe strings.

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);
    }
}

Then you can create a $url_safe link

Grab the encryption key from your config

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

Create a url safe string by passing true as the third parameter

$encoded_url_safe_string = urlencode($this->encrypt->encode('secret', $key, true));

You will need to use rawurldecode to decode the string

$decoded_url_safe_string = rawurldecode($encoded_url_safe_string, $key);
like image 24
Philip Avatar answered Oct 17 '22 08:10

Philip


I faced the same problem in the past, codeigniter encrypt->encode()has disallowed chars for URL but i did the following to solve the problem

  1. first encode() your $user_id
  2. simply replace the disallowed chars from the encrypted string with allowed one.
  3. now before decoding replace those chars back to original one
  4. decode() your $user_id

here is the code:

$this->encrypt->encode($user_id,$key);
$user_id = strtr($user_id,array('+' => '.', '=' => '-', '/' => '~'));
//your email body (link the user id here)

now time for decoding

$user_id = strtr($user_id,array('.' => '+', '-' => '=', '~' => '/'));
$this->encrypt->decode($user_id,$key);
like image 44
Masoud Mustamandi Avatar answered Oct 17 '22 07:10

Masoud Mustamandi