ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

CI session database, lot of data

January 24, 2012 6:59am

Subscribe [3]
  • #1 / Jan 24, 2012 6:59am

    broadband

    20 posts

    I have some temporary data which I decided to save in flashdata (for the next request).

    There will be lot of data, more than 4 kB (which is maximum browser cookie size), so I decided to use
    database.

    $config['sess_cookie_name']     = 'session';
    $config['sess_expiration']        = 7200;
    $config['sess_expire_on_close']   = true;
    $config['sess_encrypt_cookie']    = FALSE;
    $config['sess_use_database']    = true;
    $config['sess_table_name']      = 'ci_session';
    $config['sess_match_ip']        = FALSE;
    $config['sess_match_useragent']  = false;
    $config['sess_time_to_update']   = 300;

    This way the cookie on the client side, does not contain value, only session_id, expire, .... not user_data.

    For testing purposes I tried to save a lot of session user_data in database. Second method test2() does not show anything. Has anybody experienced the same?

    public function test()
      {
        $string = "test";
        for($i = 0; $i < 10;$i++) $string .= $string;
    
        $this->load->library('session');
        $this->session->set_userdata(array('test' => $string));
      }

     

    public function test2()
      {
        $this->session->userdata('test');
      }

    If I try with php native sessions:

    public function test()
      {
        $string = "test";
        for($i = 0; $i < 10;$i++) $string .= $string;
    
        session_start();
        $_SESSION['test'] = $string;
      }
    public function test2()
      {
        session_start();
        echo $_SESSION['test'];
      }

     

  • #2 / Jan 24, 2012 7:07am

    broadband

    20 posts

    I checked in sql database, the user data is correct. I also “debugged” system/libraries/Session.php and I only found maybe the serialize/unserialize methods maybe causing the problem.

    The php native sessions code works.

  • #3 / Jan 24, 2012 7:18am

    broadband

    20 posts

    The problem only occurs when saving lots of data. String mb_strlen() = 80000. With $_SESSION it works.

  • #4 / Jan 24, 2012 1:39pm

    InsiteFX

    6819 posts

    Read the bottom of this table!

    -- ------------------------------------------------------------------------
    
    --
    -- Table structure for CodeIgniter ci_sessions.
    --
    DROP TABLE IF EXISTS `ci_sessions`;
    
    CREATE TABLE IF NOT EXISTS  `ci_sessions` (
      `session_id`    varchar(40)           DEFAULT '0' NOT NULL,
      `ip_address`    varchar(16)           DEFAULT '0' NOT NULL,
      `user_agent`    varchar(120)                      NOT NULL,
      `last_activity` int(10)      unsigned DEFAULT 0   NOT NULL,
      `user_data`     text,
      PRIMARY KEY (`session_id`),
      KEY `last_activity_idx` (`last_activity`)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ;
    
    -- ------------------------------------------------------------------------
    --  `user_data` text,       COMMENT - maximum length of 65535 characters.
    --  `user_data` mediumtext, COMMENT - maximum length of 16777215 characters.
    --  `user_data` longtext,   COMMENT - maximum length of 4294967295 characters.
    -- ------------------------------------------------------------------------

     

  • #5 / Jan 24, 2012 1:59pm

    CroNiX

    4713 posts

    Also, you wrote different code for your 2 examples.

    When you used $_SESSION, you ran session_start(); in each method before trying to set or read session data.

    When you used CI session, you only loaded session in the test() method.  You also need to load session in test2(), like you did for your $_SESSION.  session_start() gets run when you load the session library.  This is one of the libraries that should be autoloaded.

  • #6 / Jan 25, 2012 3:33am

    broadband

    20 posts

    Thank you for the reply and the solution. The problem was user_data datatype. When I changed

    ALTER TABLE `ci_session` CHANGE `user_data` `user_data` LONGTEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL

    everything worked fine.

    And also yes I forgot to add $this->load->library(‘session’); in method test2()

    public function test2()
      {
        $this->session->userdata('test');
      }

    I just noticed when using database there is quite a performance hit.

  • #7 / Jan 25, 2012 3:45am

    broadband

    20 posts

    public function test2()
      {
        $this->load->library('session')
        $this->session->userdata('test');
      }

    Not able to edit previous post since:

    Error Message
    
    Due to a heavy increase of spam within our community, the privilege of being able to add links to content is currently reserved for active community members and those who have purchased our software.
  • #8 / Jan 25, 2012 8:48am

    InsiteFX

    6819 posts

    There’s only two libraries that I autoload and those are the database and sessions because they are always used!

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases