Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter 2.x set_cipher and set_flashdata cannot be used together

When I try to use set_cipher and set_flashdata together in CodeIgniter, flashdata is not set and a php error is logged.

To reproduce this on a fresh CI:

Change the following config.php lines (changes from default CI config):

$config['sess_encrypt_cookie'] = TRUE;
$config['log_threshold'] = 1; // To log the error 
// Encryption key should be set, just set anything, for example:
$config['encryption_key'] = 'HjyePR4FPF70vHKaHTl8jZ0hSMgOu5bW';

Controller (Simplified as much as I could):

<?php

class Welcome extends CI_Controller {

 function __construct(){parent::__construct();}

 function index()
 {
 $this->load->library('session');
 $this->load->library('encrypt');

 $this->encrypt->set_cipher(MCRYPT_BLOWFISH);
 $this->encrypt->encode('message');

 $this->session->set_flashdata('item', 'value');
 }

}

Then simply open the controller in browser and refresh page, you will see flashdata is not set, and the following error is logged in CI application log folder if it has the right permission:

Severity: Notice --> unserialize(): Error at offset 0 of 286 bytes /Applications/MAMP/htdocs/final/system/libraries/Session.php 741

1. Is this a CodeIgniter bug or am i doing anything wrong?

2. What should I do for this to work?

P.S. had to change cipher because default encryption result was too long for a message I needed to encrypt, but I wanted to use default method for anything else CI uses

like image 590
Vladimir Avatar asked May 09 '15 18:05

Vladimir


1 Answers

A1: Both.

It's not a bug per se, but definitely bad design. The CI2 session library does depend on the same CI_Encrypt instance that is $this->encrypt. So, when the session library is already using it and you change the cipher in the middle of that process, it is indeed you breaking it.

A2: Load another instance of the Encryption library for your own usage.

Like this:

// You can use $this->encrypter afterwards
$this->load->library('encrypt', NULL, 'encrypter');

However, length of the resulting cipher-text is a really bad reason to change the encryption algorithm.

like image 165
Narf Avatar answered Oct 17 '22 06:10

Narf