Welcome Guest, Not a member yet? Register   Sign In
multiple session cookies with session library
#12

[eluser]skunkbad[/eluser]
I may have come up with a solution, but since I've never had problems with sessions, I'm not sure it will work or not. The solution does reduce the amount of cookies set to 1. I'm not big on using hooks, but after playing around for a couple hours, it's the only way I could do what I was trying to do. Hooks must be turned on in config/config. A post controller hook needs to be created in config/hooks.php. Please check it out and let me know if it works:

config/hooks.php
Code:
$hook['post_controller'][] = array(
'function' => 'finalize_session',
'filename' => 'finalize_session.php',
'filepath' => 'hooks'
);

MY_Session.php
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Session extends CI_Session {

private $cookie_data = array();

/**
  * Write the session cookie
  *
  * @access public
  * @return void
  */
public function _set_cookie($cookie_data = NULL)
{
  if (is_null($cookie_data))
  {
   $cookie_data = $this->userdata;
  }

  // Serialize the userdata for the cookie
  $cookie_data = $this->_serialize($cookie_data);

  if ($this->sess_encrypt_cookie == TRUE)
  {
   $cookie_data = $this->CI->encrypt->encode($cookie_data);
  }
  else
  {
   // if encryption is not used, we provide an md5 hash to prevent userside tampering
   $cookie_data = $cookie_data.md5($cookie_data.$this->encryption_key);
  }

  $this->cookie_data[] = $cookie_data;
}

/**
  * Finalize Session
  */
public function finalize_session()
{
  if( ! empty( $this->cookie_data ) )
  {
   $cookie_data = array_pop( $this->cookie_data );

   $expire = ($this->sess_expire_on_close === TRUE) ? 0 : $this->sess_expiration + time();

   // Set the cookie
   setcookie(
    $this->sess_cookie_name,
    $cookie_data,
    $expire,
    $this->cookie_path,
    $this->cookie_domain,
    $this->cookie_secure
   );

   $this->cookie_data = array();
  }
}

// -----------------------------------------------------------------------

}

/* End of file MY_Session.php */
/* Location: ./application/libraries/MY_Session.php */

hooks/finalize_session.php
Code:
<?php

function finalize_session()
{
$CI =& get_instance();

$CI->session->finalize_session();
}

In my testing, when sending ajax responses, I had to change how I would normally send my response. Normally I just echo json_encode( $response ); I found I had to use Output:Confusedet_output() instead.
Code:
// echo json_encode( $response );

$this->output->set_output( json_encode( $response ) );


Messages In This Thread
multiple session cookies with session library - by El Forum - 10-15-2012, 03:16 AM
multiple session cookies with session library - by El Forum - 10-15-2012, 10:59 AM
multiple session cookies with session library - by El Forum - 10-15-2012, 11:37 AM
multiple session cookies with session library - by El Forum - 10-15-2012, 11:47 AM
multiple session cookies with session library - by El Forum - 10-15-2012, 12:34 PM
multiple session cookies with session library - by El Forum - 10-15-2012, 01:10 PM
multiple session cookies with session library - by El Forum - 10-15-2012, 01:34 PM
multiple session cookies with session library - by El Forum - 10-15-2012, 01:41 PM
multiple session cookies with session library - by El Forum - 10-15-2012, 01:57 PM
multiple session cookies with session library - by El Forum - 10-15-2012, 01:58 PM
multiple session cookies with session library - by El Forum - 10-15-2012, 03:23 PM
multiple session cookies with session library - by El Forum - 10-15-2012, 07:06 PM
multiple session cookies with session library - by El Forum - 10-15-2012, 07:19 PM
multiple session cookies with session library - by El Forum - 10-15-2012, 07:23 PM
multiple session cookies with session library - by El Forum - 10-15-2012, 09:54 PM
multiple session cookies with session library - by El Forum - 10-16-2012, 05:43 AM
multiple session cookies with session library - by El Forum - 10-16-2012, 06:17 AM
multiple session cookies with session library - by El Forum - 10-16-2012, 07:18 AM
multiple session cookies with session library - by El Forum - 10-16-2012, 07:46 AM
multiple session cookies with session library - by El Forum - 10-16-2012, 11:42 PM
multiple session cookies with session library - by El Forum - 10-17-2012, 11:58 AM
multiple session cookies with session library - by El Forum - 10-17-2012, 01:02 PM
multiple session cookies with session library - by El Forum - 10-17-2012, 05:55 PM
multiple session cookies with session library - by El Forum - 10-18-2012, 06:25 AM
multiple session cookies with session library - by El Forum - 10-18-2012, 07:58 AM
multiple session cookies with session library - by El Forum - 10-18-2012, 08:30 AM
multiple session cookies with session library - by El Forum - 10-18-2012, 10:21 AM
multiple session cookies with session library - by El Forum - 10-18-2012, 11:33 AM



Theme © iAndrew 2016 - Forum software by © MyBB