Skip to content
World Wide Web Server edited this page Jul 4, 2012 · 17 revisions

Category:Libraries::Session

A MY_Session that includes a method to remove session cookie when browse closes.

  • Create MY_Session.php in your system\application\libraries folder and paste the following code. [code] <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); /**

  • MY_Session Class

  • Extends the core CI_Session giving it the ability to create

  • session cookies that expire when the browser closes */ class MY_Session extends CI_Session {

    /**
    
    • Cookie Monster eats up the session cookie just before browser closes!
    • Okay, fine! It works by creating a cookie that tells CI_Session to
    • create session cookies that expire when the browser closes
    • @access public
    • @return void */ function create_cookie_monster() { setcookie($this->sess_cookie_name.'_cm', 'munch', 0, $this->cookie_path, $this->cookie_domain, 0); $this->sess_time_to_update = -1; $this->sess_update(); }

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

    /**

    • Write the session cookie

    • @access public

    • @param string

    • @return void */ 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); }

      // Set the cookie setcookie( $this->sess_cookie_name, $cookie_data, // determine if session cookie expires on browser close isset($_COOKIE[$this->sess_cookie_name.'_cm']) ? 0 : $this->sess_expiration + time(), $this->cookie_path, $this->cookie_domain, 0 ); }

} [/code]

[h3]Example[/h3]

  • In your view, create a input[type="checkbox"]

[code] <input type="checkbox" name="remember_me" id="remember" value="true" /> [/code]

  • In your controller, call create_cookie_monster

[code] if ( ! $this->input->post('remember_me')) { $this->session->create_cookie_monster(); } [/code]

Clone this wiki locally