Welcome Guest, Not a member yet? Register   Sign In
Object in session in MY_Controller clears the session completely.
#1

[eluser]dakipro[/eluser]
Hi, I noticed some strange behavior with latest version of CI2 (2.0.2).
I have MY_Controller that I want to use to pre-load some stuff for each controller, and when I put this in its __constructor()

Code:
$this->session->set_userdata('arrayData',$someArray);


All works fine if that array contains strings or ints or whatever, but if that array contains some objects, all that was in session up to this point gets blank, and array with objects is populated in session as expected.
It took me hours to track this down, I tried to use session in DB, I tried to encrypt it and all works fine with this array with objects, but it deletes all userdata that was in session up to this point.
I can try whatever you suggest, I can post all relevant code that might be needed, but if someone can try to replicate this on their own and confirm that would be awesome. Perhaps this is known bug, but I wasn't able to google it out.
I didn't try to put this code on some other places I just put it in MY_Controller, in its constructor (as that is where I need it, for all controllers).
Thanks in advance,
Dalibor
#2

[eluser]WanWizard[/eluser]
Using cookie-based sessions?

If so, then there's your problem, a cookie can only hold 4Kb of data, which you reach very quickly due to the encryption overhead. If not, is the database column 'user_data' large enough?

If the cookie or the payload gets corrupted due to truncation, the session is no longer valid and a new session will be created.
#3

[eluser]dakipro[/eluser]
Thanks, I checked the size and changed the database field for user_data to be longtext, still same issue. Session gets cleared the moment I add this object to the session, here is dump of the session

Code:
dump ($this->session) - File:  E:\www\application\controllers\admin\pages.php, line: 92;
stdClass Object
(
    [__CLASS__] => CI_Session
    [sess_encrypt_cookie] =>
    [sess_use_database] => 1
    [sess_table_name] => ci_sessions
    [sess_expiration] => 7200
    [sess_expire_on_close] =>
    [sess_match_ip] =>
    [sess_match_useragent] => 1
    [sess_cookie_name] => ci_session
    [cookie_prefix] =>
    [cookie_path] => /
    [cookie_domain] =>
    [cookie_secure] =>
    [sess_time_to_update] => 300
    [encryption_key] => CODETMP
    [flashdata_key] => flash
    [time_reference] => local
    [gc_probability] => 5
    [userdata] => Array
        (
            [session_id] => b5638e7c14d7e4d3766e427dcf3ad6d9
            [ip_address] => 127.0.0.1
            [user_agent] => Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Geck
            [last_activity] => 1305577013
            [languagesActive] => Array
                (
                    [languages_default] => stdClass Object
                        (
                            [__CLASS__] => models\Language
                            [id] => 2
                            [lang_name] => English
                            [lang_ext] => en
                            [ordering] => 2
                            [is_active] => 1
                            [is_default] => 1
                            [is_deletable] => 1
                            [field_rules] => Array
                                (
                                )

                            [prePersistDefaults] => Array
                                (
                                )

                            [mainObjName] => models\Language
                            [defaultSortingField] => ordering
                            [paginateResults] =>
                            [paginationLinksCreated] =>
                            [paginationConfig] =>
                            [qb] =>
                        )

                )

            [FlashNotice] => Array
                (
                    [info] => Array
                        (
                        )

                    [success] => Array
                        (
                        )

                    [warning] => Array
                        (
                        )

                    [error] => Array
                        (
                            [0] => validation_errors()
                            [1] => item_not_found_for_editing
                        )

                )

        )

    [CI] => stdClass Object
        (
            [__CLASS__] => Pages
            [mainObjName] => models\Page
        )

    [now] => 1305577038
)

After this is set in session, the moment redirection is done the entire part
Code:
[FlashNotice] => Array(
...
gets lost. This happens with flash_data as well, it gets lost if in MY_Controller __construct() I put this line
Code:
$laguagesData = array(
            'languages_default'=> $this->doctrine->em->getRepository('models\Language')->findBy(array('is_active'=>1))
        );
//this uses doctrine to get the object, its dump is visible in code I pasted above, it doesn't matter as it fails if I create plain object like
// 'languages_default'=> new models\Language();
$this->session->set_userdata('languagesActive',$laguagesData);

Again, if I remove this line from MY_Controller, or if I put this line
Code:
$laguagesData = array(
            'languages_default'=> 'Whatever string I want to put here'
        );
all works fine, session is preserved and this code exists on the next page
Code:
[FlashNotice] => Array
                (

After the old data is lost, I can confirm that session ID changes as well.

Here is the sample session cookie dump set in the browser
Code:
a:4:{s:10:"session_id";s:32:"c4297d0a493432155fb361828078c9a1";s:10:"ip_address";s:9:"127.0.0.1";s:10:"user_agent";s:50:"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Geck";s:13:"last_activity";i:1305577882;}fe3a99b94d7f4937d5edc6b335bfde18
//Note that this is not exact the same cookie from the dump above, but it is for the same code
#4

[eluser]WanWizard[/eluser]
You're storing Doctrine objects? Are you sure they can be serialized? Failing to unserialize() after loading the session can also cause a reset of the session.
#5

[eluser]dakipro[/eluser]
It is plain PHP object actually, when I create it with
$lang = new Lang();
it is (should be) plain php object then.
I will try with some object that is not related with doctrine at all and see the results.
I will also check if it has something to do with namespaces as well.
I think I will keep data in array for the time being, but I'm also pretty sure that unserialize() is failing at some point for some reason.

I will check it tomorrow and post if I figure something out, maybe it is related to the doctrine after all...
Thank you for your time,
Daki
#6

[eluser]dakipro[/eluser]
I can confirm that the problem is when object has namespace like this (example in my code in above post)
Code:
'languages_default'=> new models\demo(),
Then session is created from scratch (previous entries deleted, most likely unserialize fails)
When I put same object like this, same blank object, with one property or something, not related to doctrine at all

Code:
'languages_default'=> new demo(),
All works fine and as expected. Can anyone confirm this?
Thanks
#7

[eluser]dakipro[/eluser]
This is getting weirder and weirder. Even arrays that contain array that contain string with backslash "\" won't work Sad
When this is in session all works:
Code:
Array
(
    [session_id] => fddf9b4f2b268cec21c6d17d7bdad3c9
    [ip_address] => 127.0.0.1
    [user_agent] => Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Geck
    [last_activity] => 1305708475
    [languages_active] => Array
        (
            [0] => Array
                (
                    [id] => 2
                    [lang_ext] => sr
                    [mainObjName] => modelsLanguage
                )

        )

    [languages_active1] => adactiv\eLangsArr ///This line works
    [languages_count] => 1
)

But this doesn't

Code:
Array
(
    [session_id] => fddf9b4f2b268cec21c6d17d7bdad3c9
    [ip_address] => 127.0.0.1
    [user_agent] => Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Geck
    [last_activity] => 1305708475
    [languages_active] => Array
        (
            [0] => Array
                (
                    [id] => 2
                    [lang_ext] => sr
                    [mainObjName] => models\Language  //Nested array with backslass, doesnt' work
                )

        )

    [languages_active1] => adactiveLangsArr
    [languages_count] => 1
)

Nor does this:


Code:
Array
(
    [session_id] => fddf9b4f2b268cec21c6d17d7bdad3c9
    [ip_address] => 127.0.0.1
    [user_agent] => Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Geck
    [last_activity] => 1305708475
    [languages_active] => Array
        (
            [0] => Array
                (
                    [id] => 2
                    [lang_ext] => sr
                    [mainOb\jName] => modelsLanguage//Nested array with backslass, doesnt' work
                )

        )

    [languages_active1] => adactiveLangsArr
    [languages_count] => 1
)
All above are plain php arrays, no objects, no specific data, just plain backslash in multidimensional array.
Not sure if anyone encored this so far, but this is very very unreliable behavior for me. Not sure if this has to do something with my machine and my settings, this same example works fine when I try plain sessions on same server. I'm switching sessions engine for now to something else, but to be honest I'm getting a bit scared now.
Hopefully someone can confirm similar behavior.
Cheers
#8

[eluser]WanWizard[/eluser]
You're not using an alternative session library by any chance?
#9

[eluser]dakipro[/eluser]
Nop, plain CI2 (2.0.2) installed and its session library that comes with it by default. Using database storage for sessions
#10

[eluser]pegas[/eluser]
Hi,

I think this is the place where i can also post my issue.
I have an object that contains other objects like so:


Code:
class Module {
    
    private $id_modul;
    private $orar;
    private $zi;
    private $lista_coa;    
    
    public function Module($id_modul , $orar, $zi = null, $lista_coa = '') {
        $this->id_modul = $id_modul;
        $this->orar = $orar;
        $this->zi = $zi;
        $this->lista_coa = $lista_coa;
    }
    
    function getIdModul() {
        return $this->id_modul;
    }
/* ... */

And the Schedule class (which will be stored in the $orar variable)

Code:
class Schedule {
    
    private $id_orar;
    private $schoolYear;
    
    public function Schedule($id_orar = 0, $schoolYear = null) {
        $this->id_orar = $id_orar;        
        $this->schoolYear = $schoolYear;
    }
    
    function getIdOrar(){
        return $this->id_orar;
    }
    
    function setIdOrar($id_orar){
        $this->id_orar = $id_orar;
    }
/* ... */

Now these are both container objects so I use them only to make the code clearer. Thus the classes only have set-ers and get-ers

Now I have the model class which generates me an array of Modules like so:

Code:
function loadOrCreateModules($values,$week_days){
        $modules = array();
        $this->db->select('*')->from('orar')->where('id_an', $values[0]);
        $query = $this->db->get();
        if ($query->num_rows() > 0){
            foreach ($query->result() as $row){
                $this->db->select('*')->from('modul')->where('id_orar',$row->id_orar);
                $second_query = $this->db->get();
                foreach ($second_query->result() as $row){                                        
                    $modules[] = new Module($row->id_modul, new Schedule($values[0],null),
                                                $week_days[$row->id_zi-1], $row->lista_coa);
                }                                
            }
        }else{            
            /* ... */
        }
        return $modules;
    }

And this seems to work on getting the queries and everything in place.
Except is takes about 20 seconds for the process to complete, and i initially thought I was in a infinite loop.
But then I simply returned one module object to my view and then used print_r() to look at it, for anything "busted".

Here's what was in the object:

Code:
Module Object
(
    [id_modul:Module:private] => 2
    [orar:Module:private] => Schedule Object
        (
            [benchmark] => CI_Benchmark Object
                (
                    [marker] => Array
                        (
                            [total_execution_time_start] => 0.01810100 1306970736
                            [loading_time:_base_classes_start] => 0.01812200 1306970736
                            [loading_time:_base_classes_end] => 0.66743400 1306970736
                            [controller_execution_time_( schedule / showScheduleFor )_start] => 0.66748100 1306970736
                        )

                )

            [hooks] => CI_Hooks Object
                (
                    [enabled] =>
                    [hooks] => Array
                        (
                        )

                    [in_progress] =>
                )

            [config] => CI_Config Object
                (
                    [config] => Array
                        (
                            [base_url] => http://localhost/
                            [index_page] =>
                            [uri_protocol] => AUTO
                            [url_suffix] => .do
                            [language] => romanian
                            [charset] => UTF-8
                            [enable_hooks] =>
                            [subclass_prefix] => MY_
                            [permitted_uri_chars] => a-z 0-9~%.:_\-
                            [allow_get_array] => 1
                            [enable_query_strings] =>
                            [controller_trigger] => c
                            [function_trigger] => m
                            [directory_trigger] => d
                            [log_threshold] => 2
                            [log_path] =>
                            [log_date_format] => Y-m-d H:i:s
                            [cache_path] =>
                            [encryption_key] => ONb!41@{Z)pLs]u_a:_^:5nQGx_7G-dU
                            [sess_cookie_name] => ci_session
                            [sess_expiration] => 7200
                            [sess_expire_on_close] =>
                            [sess_encrypt_cookie] =>
                            [sess_use_database] =>
                            [sess_table_name] => ci_sessions
                            [sess_match_ip] =>
/* ... */

So the object is so big that CI or my server simply quits loading this huge piece of data.
There are about 30 objects of this kind so I wouldn't blame it Smile

I want to know why is this happening. Am I leaving something activated or am I using things wrong here ?

Please help/advise,
Thanks




Theme © iAndrew 2016 - Forum software by © MyBB