Search code examples
urlcodeignitersecuritycodeigniter-url

encrypting uri segments in Codeigniter


I want to encrypt my uri segments that are passed as IDs to query into database tables.

For example I'm using

$id=urlencode($this->encrypt->encode($user['id']));

which is decode on another controller using

$id_decrypt=$this->encrypt->decode(urldecode($id));

I have tested the encryption and decription and it seems to work. However on trying the urls eg.

http://localhost/app_name/index.php/profile/view_profile/b98N98YNqoEA7yI1tavIY1s51RhiSHKGCFarU4A6XgFUMB%2BI3KwiEA23h1XITmkq1qPABqGs8e1sdP16v4og8g%3D%3D

on some of the url it will work as expected but on some it will give browser error 404 (Object not found). On deleting the encrypted segment, I can access the index function. What could be the possible cause of this?


Solution

  • Within the file ./application/config/config.php there is a section for permitted uri chars. The default characters are:

    $config['permitted_uri_chars'] = 'a-z 0-9~%.:_-;
    

    In some projects, I have changed this too:

    $config['permitted_uri_chars'] = 'a-z 0-9~%.:_\'+,-=';
    

    However, as the CodeIgniter Engineers state

    DO NOT CHANGE THIS UNLESS YOU FULLY UNDERSTAND THE REPERCUSSIONS!!