I am having some issues with Codeigniter 3 session. On the previous version, it was working fine until I upgraded to CI3. I implemented uplodify which is a file/image uploader.
The code is too long to post here, I try to simplify it. Here is add photo view file:
<input id="file_upload" name="file_upload" type="file" />
<script type="text/javascript">
$(document).ready(function() {
browser_cookie = $.cookie('<?=$this->session->sess_cookie_name?>');
$('#file_upload').uploadify({
'debug' : false,
'uploader' : '<?=site_url("/photo/uploadFile")?>',
'swf' : '<?php echo base_url()?>uploadify/uploadify.swf',
'formData':{'bizid':<?=$biz->id?>,'browser_cookie':browser_cookie},
'buttonText' : 'Télécharger',
'fileTypeExts' : '*.jpg;',
'fileTypeDesc' : 'JPG Files',
'cancelImg' : '<?php echo base_url()?>uploadify/cancel.png',
'folder' : '<?php echo base_url()?>tmp',
'multi' : true,
'queueSizeLimit' : 4,
'auto' : true,
This code redirects to uploadFile method
'uploader' : '<?=site_url("/photo/uploadFile")?>',
This one fetch the serialized session.
browser_cookie = $.cookie('session->sess_cookie_name?>');
and stores it here.
This part is fine 'bizid':id?>.
'formData':{'bizid':id?>,'browser_cookie':browser_cookie},
Photo controller:
Class Photo extends CI_controller
{
// This method checks if user is logged in. It works fine..
public function upload()
{
if(!$this->tank_auth->is_logged_in())
{
redirect('/ucp/login/');
}
}
public function uploadFile()
{
//This one, is giving me hard time. The session gets lost.
if(!$this->tank_auth->is_logged_in())
{
$data['type'] = 0;
$data['msg'] = 'S\'il vous plaît connecter vous pour continuer';
exit(json_encode($data));
}
If I do
var_dump($this->tank_auth->get_username());
inside upload method, it returns the result. But if I do it inside uploadFile method it returns null.
Here is the query cookie function:
/*jshint eqnull:true */
/*!
* jQuery Cookie Plugin v1.1
* https://github.com/carhartl/jquery-cookie
*
* Copyright 2011, Klaus Hartl
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://www.opensource.org/licenses/mit-license.php
* http://www.opensource.org/licenses/GPL-2.0
*/
(function($, document) {
var pluses = /\+/g;
function raw(s) {
return s;
}
function decoded(s) {
return decodeURIComponent(s.replace(pluses, ' '));
}
$.cookie = function(key, value, options) {
// key and at least value given, set cookie...
if (arguments.length > 1 && (!/Object/.test(Object.prototype.toString.call(value)) || value == null)) {
options = $.extend({}, $.cookie.defaults, options);
if (value == null) {
options.expires = -1;
}
if (typeof options.expires === 'number') {
var days = options.expires, t = options.expires = new Date();
t.setDate(t.getDate() + days);
}
value = String(value);
return (document.cookie = [
encodeURIComponent(key), '=', options.raw ? value : encodeURIComponent(value),
options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
options.path ? '; path=' + options.path : '',
options.domain ? '; domain=' + options.domain : '',
options.secure ? '; secure' : ''
].join(''));
}
// key and possibly options given, get cookie...
options = value || $.cookie.defaults || {};
var decode = options.raw ? raw : decoded;
var cookies = document.cookie.split('; ');
for (var i = 0, parts; (parts = cookies[i] && cookies[i].split('=')); i++) {
if (decode(parts.shift()) === key) {
return decode(parts.join('='));
}
}
return null;
};
$.cookie.defaults = {};
})(jQuery, document);
How could I get the session to work inside uploadFile method?
Thank you
Uploadify doesn't pass the current session information. Codeigniter has changed the way cookies/sessions are handled. So, your jquery cookie fetch won't work. You try to carry the session like this:
'formData':{'bizid':<?=$biz->id?>,'browser_cookie':<?php echo 'your session here'?>},
Then in your controller, you can verify if the session exist. And if not, just set a new session then use it. This should work just fine.