8

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

PHP Worm...
  • 4,109
  • 1
  • 25
  • 48
Sumon
  • 301
  • 2
  • 4
  • 11
  • Check this link you will find what you want : http://stackoverflow.com/questions/6268873/codeignitor-urlencode-decode – Ahmed Ziani Mar 23 '15 at 14:13
  • not working, only took as a id before / symbol from the url – Sumon Mar 23 '15 at 14:45
  • you should use some encode method that does not produce any `/` `#` `&` and some others character like ending with `==`. I think you used base64 which will not work.I used bin2hex and hex2bin – Shaiful Islam Mar 23 '15 at 15:15

5 Answers5

6

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);
MDeuerlein
  • 690
  • 1
  • 8
  • 13
4

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);
Philip
  • 4,592
  • 2
  • 20
  • 28
  • Isn't your solution doing the same thing 2 times? Your modified MY_Encrypt class is de-/encoding the characters and the php functions urlencode() and url decode() are doing it again. – MDeuerlein Jan 22 '16 at 01:54
3

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

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

Mike Ivars
  • 21
  • 6
0

Try This:

$query = urldecode($query);

$query = urlencode($query);

  • Please clean up the duplicate code and add a short explanation of what your code does. Clean and clear answers are the best answers – Simas Joneliunas Jan 29 '20 at 05:43