Advertisement
mariodian

Extend CI to allow http_only cookie

Oct 8th, 2014
434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.32 KB | None | 0 0
  1. <?php
  2.  
  3. class MY_Session extends CI_Session
  4. {
  5.     private $cookie_http_only = 0;
  6.    
  7.     /**
  8.      * Session Constructor
  9.      *
  10.      * The constructor runs the session routines automatically
  11.      * whenever the class is instantiated.
  12.      */
  13.     public function __construct($params = array())
  14.     {
  15.         log_message('debug', "Session Class Initialized");
  16.  
  17.         // Set the super object to a local variable for use throughout the class
  18.         $this->CI =& get_instance();
  19.  
  20.         // Set all the session preferences, which can either be set
  21.         // manually via the $params array above or via the config file
  22.         foreach (array('sess_encrypt_cookie', 'sess_use_database', 'sess_table_name', 'sess_expiration', 'sess_expire_on_close', 'sess_match_ip', 'sess_match_useragent', 'sess_cookie_name', 'cookie_path', 'cookie_domain', 'cookie_secure', 'sess_time_to_update', 'time_reference', 'cookie_prefix', 'encryption_key', 'cookie_http_only') as $key)
  23.         {
  24.             $this->$key = (isset($params[$key])) ? $params[$key] : $this->CI->config->item($key);
  25.         }
  26.  
  27.         if ($this->encryption_key == '')
  28.         {
  29.             show_error('In order to use the Session class you are required to set an encryption key in your config file.');
  30.         }
  31.  
  32.         // Load the string helper so we can use the strip_slashes() function
  33.         $this->CI->load->helper('string');
  34.  
  35.         // Do we need encryption? If so, load the encryption class
  36.         if ($this->sess_encrypt_cookie == TRUE)
  37.         {
  38.             $this->CI->load->library('encrypt');
  39.         }
  40.  
  41.         // Are we using a database?  If so, load it
  42.         if ($this->sess_use_database === TRUE AND $this->sess_table_name != '')
  43.         {
  44.             $this->CI->load->database();
  45.         }
  46.  
  47.         // Set the "now" time.  Can either be GMT or server time, based on the
  48.         // config prefs.  We use this to set the "last activity" time
  49.         $this->now = $this->_get_time();
  50.  
  51.         // Set the session length. If the session expiration is
  52.         // set to zero we'll set the expiration two years from now.
  53.         if ($this->sess_expiration == 0)
  54.         {
  55.             $this->sess_expiration = (60*60*24*365*2);
  56.         }
  57.        
  58.         // Set the cookie name
  59.         $this->sess_cookie_name = $this->cookie_prefix.$this->sess_cookie_name;
  60.  
  61.         // Run the Session routine. If a session doesn't exist we'll
  62.         // create a new one.  If it does, we'll update it.
  63.         if ( ! $this->sess_read())
  64.         {
  65.             $this->sess_create();
  66.         }
  67.         else
  68.         {
  69.             $this->sess_update();
  70.         }
  71.  
  72.         // Delete 'old' flashdata (from last request)
  73.         $this->_flashdata_sweep();
  74.  
  75.         // Mark all new flashdata as old (data will be deleted before next request)
  76.         $this->_flashdata_mark();
  77.  
  78.         // Delete expired sessions if necessary
  79.         $this->_sess_gc();
  80.  
  81.         log_message('debug', "Session routines successfully run");
  82.     }
  83.    
  84.     function _set_cookie($cookie_data = NULL)
  85.     {      
  86.         if (is_null($cookie_data))
  87.         {
  88.             $cookie_data = $this->userdata;
  89.         }
  90.  
  91.         // Serialize the userdata for the cookie
  92.         $cookie_data = $this->_serialize($cookie_data);
  93.  
  94.         if ($this->sess_encrypt_cookie == TRUE)
  95.         {
  96.             $cookie_data = $this->CI->encrypt->encode($cookie_data);
  97.         }
  98.  
  99.         $cookie_data .= hash_hmac('sha1', $cookie_data, $this->encryption_key);
  100.  
  101.         $expire = ($this->sess_expire_on_close === TRUE) ? 0 : $this->sess_expiration + time();
  102.  
  103.         // Set the cookie
  104.         setcookie(
  105.             $this->sess_cookie_name,
  106.             $cookie_data,
  107.             $expire,
  108.             $this->cookie_path,
  109.             $this->cookie_domain,
  110.             $this->cookie_secure,
  111.             $this->cookie_http_only
  112.         );
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement