CodeIgniter Forums
Displaying image from database corrupts CI session - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Displaying image from database corrupts CI session (/thread-44195.html)



Displaying image from database corrupts CI session - El Forum - 08-06-2011

[eluser]skunkbad[/eluser]
I don't normally store images in a database, but I have a need to store one temporarily. I first upload the image:

Code:
$tmp_name = $_FILES['userfile']['tmp_name'];
$handle = fopen( $tmp_name, "r" );
$image_string = base64_encode( fread( $handle, filesize($tmp_name) ) );
fclose($handle);

Then encrypt it:

Code:
$this->load->library('encrypt');
$this->encrypt->set_cipher( MCRYPT_BLOWFISH );
$key = sha1('mysecretkey');
$encrypted_image = $this->encrypt->encode( $image_string, $key );

The image is now an encrypted string and ready for the database update:

Code:
$this->db->where( 'record-id', $this->input->post('profile_id') );
$this->db->update( 'table_name' array( 'image' => $encrypted_image ) );

The image is stored in a longtext field.

When I want to see the image in another view, I have this in my controller:

Code:
$this->load->library('encrypt');
$this->encrypt->set_cipher( MCRYPT_BLOWFISH );
$key = sha1('mysecretkey');
$view_data['the_image'] = $this->encrypt->decode( $view_data['the_image'], $key );

And in the view displaying the image:

Code:
echo '<img  />';

The image displays fine, but every pageload after that I get the following error:

Code:
PHP E_NOTICE #8 - Date/Time: 8/6/2011 18:57:26
File: C:\xampp\htdocs\beta-cps\system\libraries\Session.php
Line: 722
unserialize() [function.unserialize]: Error at offset 0 of 319 bytes

It is definitely the displaying of the image that triggers the error, and the error persists on any page of the website until I clear my cookies and start over. Does anybody know what is happening here?


Displaying image from database corrupts CI session - El Forum - 08-06-2011

[eluser]pickupman[/eluser]
There's nothing showing from the code above where you are writing/reading from the session. At some point are you trying to save the base64 string for the profile image in the users session? If so, would the user_data column need to be made bigger, or have you tried that to eliminate that as an issue.


Displaying image from database corrupts CI session - El Forum - 08-06-2011

[eluser]skunkbad[/eluser]
[quote author="pickupman" date="1312703272"]There's nothing showing from the code above where you are writing/reading from the session. At some point are you trying to save the base64 string for the profile image in the users session? If so, would the user_data column need to be made bigger, or have you tried that to eliminate that as an issue.[/quote]

No, that's another confusing aspect to the problem. The image has nothing to do with the session. It's not being stored there, and not being retrieved from there.


Displaying image from database corrupts CI session - El Forum - 08-06-2011

[eluser]skunkbad[/eluser]
I figured out the problem. I was doing something else, triggered by the image retrieval, and it was apparently corrupting the session. What it was is that I use a custom CSRF library, and I was loading it after the data was being retrieved from the model, which apparently is not good.


Displaying image from database corrupts CI session - El Forum - 08-06-2011

[eluser]pickupman[/eluser]
Glad you figured it out then. Checked out Community Cart. Seems like a nice project, and I have written a number of custom modules for zencart but always wished there was something CI based. It's one of those animals you take on not realizing how much it really takes to roll this out for different setups. (ie. Seems like most of the urls have absolute paths hard coded making it fail when placed in a subdirectory.) Props to you for the hard work.


Displaying image from database corrupts CI session - El Forum - 08-06-2011

[eluser]skunkbad[/eluser]
[quote author="pickupman" date="1312706211"]Glad you figured it out then. Checked out Community Cart. Seems like a nice project, and I have written a number of custom modules for zencart but always wished there was something CI based. It's one of those animals you take on not realizing how much it really takes to roll this out for different setups. (ie. Seems like most of the urls have absolute paths hard coded making it fail when placed in a subdirectory.) Props to you for the hard work.[/quote]

Actually, I'm not able to dedicate any time to Community Cart for at least 6 to 8 months, but when I get done with what I'm working on now (actual work), then I plan to rebuild the entire application. I think the state it is in now is OK, and it works, but I think I could make it a lot better. Lately my Community Auth has been worked on, because sometimes when I'm taking a break from my real job I try to refine it. It's on bitbucket too. I make these things because I want to use them for my customer's websites. The more work I put in, the easier it is to get things done later. Glad if you find you can use them, or parts of them!