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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With