Welcome Guest, Not a member yet? Register   Sign In
Cookie/session gets lost
#1

(This post was last modified: 03-02-2015, 11:39 PM by rocks.)

Hi there,

I've integrated uploadify (file/image uploader). It worked great with CI older versions. After the upgrade to CI 3, sessions have been completely rewritten, and I'm confused a bit.

'uploader'  : '<?=site_url("/photo/uploadFile")?>',

I check if a user is login or not

PHP Code:
    public function uploadFile()
    {
        
//check login
        
if(!$this->tank_auth->is_logged_in())
        {
            
$data['type'] = 0;
            
$data['msg'] = 'Please login to continue';
            exit(
json_encode($data));
        } 
The cookie/session is serialized, so I grab it with $.cookie function. But this doesn't work with CI3 and the $this->tank_auth->is_logged_in() returns FALSE.

What is the approach in caring cookie/session in this case?


Here is javascript in my view page.

Code:
<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,
Reply
#2

I don't know but if your code was working before upgrading, it might be a problem with the Tank Auth Library (don't know this library)
are you sure tank auth is working fine with CI 3?
Reply
#3

(03-03-2015, 01:50 AM)sintakonte Wrote: I don't know but if your code was working before upgrading, it might be a problem with the Tank Auth Library (don't know this library)
are you sure tank auth is working fine with CI 3?

Yes, Tank Auth library is working just fine.

When I var_dump the session before uploading it shows the session in CI 2.x.x.

PHP Code:
<?php var_dump($this->session->sess_cookie_name); ?>

But it doesn't in CI 3.

Is this deprecated $this->session->sess_cookie_name?
Reply
#4

Have you tried just getting it from config? Don't rely on undocumented or internal features/properties!! They can change without warning.
Code:
$this->config->item('sess_cookie_name');
Reply
#5

(This post was last modified: 03-03-2015, 12:10 PM by rocks.)

(03-03-2015, 08:44 AM)CroNiX Wrote: Have you tried just getting it from config? Don't rely on undocumented or internal features/properties!! They can change without warning.


Code:
$this->config->item('sess_cookie_name');

Yes I tried that.
Before the cookie/session looked like this:

ci_session
Code:
a%3A4%3A%7Bs3A%22[b]session_id[/b]%22%3Bs%3A32e47be9d1848e53f459063d14a4e6514b%22%3Bs%3A10%3A%22[b]ip_address[/b]%22%3Bs%3A3%3A%22%3A%3A1%22%3Bs%3A10%3A%22[b]user_agent[/b]%22%3Bs%3A82%3A%22Mozilla%2F5.0+%28Macintosh%3Bv%3A35.0%29+Gecko%2F20100101+Firefox%2F35.0%22%3Bs%3A13%3A%22[b]last_activity[/b]%22%3Bi%3A14253950237d5bc5393a9281301ed537c360b2b

With CI3 it looks like this:

ci_session
Code:
f8429902b1b0a615a208666386c525a4693fc606

The link Add pictures goes to photo/upload function, and there I check if user is logged in.
This one works fine.
PHP Code:
    public function upload()
    {
        if(!
$this->tank_auth->is_logged_in())
        {
            
redirect('/ucp/login/');
        } 


But in this method, the session gets lost.

This redirects to uploadFile() which is under the same class as the above function.
'uploader' : '<?php echo site_url("/photo/uploadFile")?>',

PHP Code:
    public function uploadFile()
    {
        
//check login
           
        
if(!$this->tank_auth->is_logged_in())
        {
            
$data['type'] = 0;
            
$data['msg'] = 'Please login to continue';
            exit(
json_encode($data));
        } 
Reply
#6

Here is the cookie function

Code:
/*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);
Reply




Theme © iAndrew 2016 - Forum software by © MyBB