Home > Enterprise >  codeigniter: Encryption URL parameter
codeigniter: Encryption URL parameter

Time:05-31

I want to encrypt URL parameter like this:

https://hello.com/questions/k0jUhPcRBJshpiXLpUSug_NhgPk89O7eS-jerHk6go360U9rl8LazZo6DR6M1N4IqG0PYIwPyKhQ

I create encode and decode function in helper

Helper 

if ( ! function_exists('encode'))
    {
        function encode($str = NULL)
        {
            $ci =& get_instance();
            if( $ci->config->item('app_encrypt_mode') ==  TRUE){
                $ci->load->library('encrypt');
                $ci->encrypt->set_mode('MCRYPT_MODE_CFB');
                return $ci->encrypt->encode($str);
            } else {
                return $str;
            }

        }
    }

if ( ! function_exists('decode'))
    {
        function decode($str = NULL)
        {
            $ci =& get_instance();
            if( $ci->config->item('app_encrypt_mode') ==  TRUE){
                $ci->load->library('encrypt');
                $ci->encrypt->set_mode('MCRYPT_MODE_CFB');
                return $ci->encrypt->decode($str);
            } else {
                return $str;
            }
            
        }
    }

I used this function on view and controller to hide the URL parameter.

VIEW

 <a <?= site_url('Request/'.urlencode(encode($data['id']))); ?>">Click Me!</a>

<?php
$encode = urlencode(encode($data['id']);
var_dump('ENCODE:'.$encode);
var_dump('DECODE:'.decode(urldecode($encode));
?>

Value of $data['id'] is 33

and result from both var_dump also 33

var_dump('ENCODE:'.$encode); must return not 33 but random string

So what am i missing?

CodePudding user response:

If both of the var_dumps are returning 33, then the app_encrypt_mode config option either does not exist or is not set to TRUE, and is defaulting to return $str;

Also, the CI Encrypt library is deprecated, and you should use the Encryption library instead. ie :

$ci->encryption->encrypt($str); and $ci->encryption->decrypt($str);

  • Related