Sumon
Sumon

Reputation: 301

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

Upvotes: 8

Views: 35388

Answers (5)

Satyam Sundaram
Satyam Sundaram

Reputation: 1

Try This:

$query = urldecode($query);

$query = urlencode($query);

Upvotes: 0

Mike Ivars
Mike Ivars

Reputation: 21

I think your problem isn't with the '/', probably it's with '=' symbol.

To code and decode a url in CodeIgniter you can use any of this php native functions: http://php.net/manual/en/ref.url.php

But to avoid problems when decode the encoded url in CodeIgniter, you can add the special character that you require to use in each case (like '=' in your case) to $config['permitted_uri_chars'] in file application/config/config.php

Upvotes: 0

Masoud Mustamandi
Masoud Mustamandi

Reputation: 498

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

Upvotes: 3

MDeuerlein
MDeuerlein

Reputation: 700

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

Upvotes: 6

Philip
Philip

Reputation: 4592

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

Upvotes: 4

Related Questions