Encrypted parameter in URL using CodeIgniter

Related searches

Hi I am making an app using CodeIgniter.

I want to encrypt the numeric id into an encrypted string.





I tried the built-in Encryption lib


but it generates different encrypted strings for each page load, this is not what I want, I want permalink just like Youtube video ID.

I need the encrypted string also decryptedable.

did you set $config['encryption_key'] = "Test@123"; in config file


you have to pass the key after string

$this->encrypt->encode(6, 'Test@123')

$this->encrypt->decode(6, 'Test@123')

i have extend the core library for this

create a file name MY_Encrypt.php with and put this file in application\libraries

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

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



Now you can use



this will have same result.

Encrypted parameter in URL using CodeIgniter, Thanks for watching. In this video, we will learn how to encrypt and decrypt URLs in Duration: 7:43 Posted: Jun 2, 2018 Selamat Pagi teman-teman, semoga kalian tetap bersemangat dan maksimal dalam melewati segala cobaan ya hehehe. Kali ini aku bakal berbagi konsep tentang enkripsi menggunakan library yang ada di Codeigniter kemudian, aku bakal berbagi contoh pengaplikasian enkripsi pada parameter url untuk mengeksekusi stored procedure yang ada.

Codeigniter provides a library for this, you can just add in autoload.php file and you can use below lines for encode and decode. May be you will encode same value for ex. "$a" multiple times then encoded value $encodedA comes different but when you will go to decode that you will always get $decodedA= 123. $a = '123';$encodedA = $this->encrypt->encode($a);$decodedA = $this->encrypt->decode($encodedA);

how to encrypt and decrypt url in CodeIgniter, It encrypted using the Mcrypt PHP extension, which is required for the Encrypt You can optionally pass your encryption key via the second parameter if you� CodeIgniter’s OpenSSL handler uses the AES-256-CTR cipher. The key your configuration provides is used to derive two other keys, one for encryption and one for authentication. This is achieved by way of a technique known as an HMAC-based Key Derivation Function (HKDF).

Simpliest way out of this scenario is :

to encode: $url_val=base64_encode($this->encryption->encrypt($var_to_encode));

to decode: $var_to_encode=$this->encryption->decrypt(base64_decode($url_val));

Test function

function test() {
        if ($y == $i) { //$y != $i cross test
            echo "$y&ltbr&gt;$x&ltbr&gt;&ltbr&gt;";

Cosidering config/config.php has fallwowing set:

$config['encryption_key'] ="key_here"


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

Encrypt Class — CodeIgniter 3.1.11 documentation, Message Length; Using the Encryption Service Directly If you pass parameters as the second argument, the key element will be used as the� Generally this encryption is opted by the developers to make the data secured like for password, URL, credit card numbers and so on. If you are using CodeIgniter than to encrypt the data becomes much more easier as compared to native PHP code. As C.I. provides its own encryption class which you can use to encrypt or decrypt the data.

for generate encrypt id just like Youtube watch id i use Hashids Spark for CodeIgniter


the purpose of this helper is implementing Hashids to generate hashes (like YouTube or Bitly) from numbers to obfuscate database IDs.

installation just like at instruction i modify the hashids_helper.php at line 20

require_once FCPATH . 'sparks/sk-hashids/' . HASHIDS_VERSION . '/vendor/Hashids.php';


require_once FCPATH . 'vendor/Hashids.php';  //according to your Hashids path

Encryption Service — CodeIgniter 4.0.4 documentation, Enables you to re-encode data that was originally encrypted with CodeIgniter 1.x to be compatible with the Encrypt library in CodeIgniter 2.x. It is only necessary to use this method if you have encrypted data stored permanently such as in a file or database and are on a server that supports Mcrypt.

Instead of trying with encode and decode you can covert the id as SHA1. sha1 will generate the alphanumeric key, so you will not get url support error also. no need to encode-decode. this will generate a secured encrypted code.

$random_key = sha1($leadDetails->lead_number);

Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question.Provide details and share your research! But avoid …. Asking for help, clarification, or responding to other answers.

If you need little stronger, and "encrypt" numbers, too -- I suggest to use Rot47: rot47.net – olegarch Nov 7 '13 at 0:12 This served the purpose and I was able to encrypt numbers also. Thanks for the solution.

The URI Class provides methods that help you retrieve information from your URI strings. If you use URI routing, you can also retrieve information about the re-routed segments. The optional second parameter defaults to NULL and allows you to set the return value of this method when the requested URI

Before using this library in our working code for data encrypt and decrypt, first we have to set the encryption key in our Codeigniter framework. This key is used by different library and helper of Codeigniter like Session, Encrypt etc. By using this key it has cryptographic process and based on this key it has encrypt and decrypt data. That

  • Is the encryption level important, or it's just an aesthetic thing? Try base 64 maybe
  • for security reason I want to encrypt the id
  • it will generate the url safe string which is a good point, but (I think) question is about the permalink means the encoded string for 6 should be same every time when we encrypt 6. so that a user can use it as a permalink or absolute link
  • Yes it will be same and it will only change if you pass different key for encoding and decoding
  • @RanaSoyab, I tried your code but I am getting error undefined property: user_control::$encrypt
  • Should I add the file name in the config file?
  • Is not an explantion needed anwser..... It's only the new library... for everyone as i that fall in this tread, the anwsers in here doesnt work on php 7.. after 2 hours of searching arround how can be done i found that was been a codeigniter update and have a new library... the -1 is really needed for try to help the people who have php7? thank you then ;)