gRoberts

SO-11015585-Session.php

Jun 15th, 2012
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 19.85 KB | None | 0 0
  1. <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3.  * CodeIgniter
  4.  *
  5.  * An open source application development framework for PHP 5.1.6 or newer
  6.  *
  7.  * @package     CodeIgniter
  8.  * @author      ExpressionEngine Dev Team
  9.  * @copyright   Copyright (c) 2008 - 2011, EllisLab, Inc.
  10.  * @license     http://codeigniter.com/user_guide/license.html
  11.  * @link        http://codeigniter.com
  12.  * @since       Version 1.0
  13.  * @filesource
  14.  */
  15.  
  16. // ------------------------------------------------------------------------
  17.  
  18. /**
  19.  * Session Class
  20.  *
  21.  * @package     CodeIgniter
  22.  * @subpackage  Libraries
  23.  * @category    Sessions
  24.  * @author      ExpressionEngine Dev Team
  25.  * @link        http://codeigniter.com/user_guide/libraries/sessions.html
  26.  */
  27. class CI_Session {
  28.  
  29.     var $sess_encrypt_cookie        = FALSE;
  30.     var $sess_use_database          = FALSE;
  31.     var $sess_table_name            = '';
  32.     var $sess_expiration            = 7200;
  33.     var $sess_expire_on_close       = FALSE;
  34.     var $sess_match_ip              = FALSE;
  35.     var $sess_ignore_ip             = array();
  36.     var $sess_match_useragent       = TRUE;
  37.     var $sess_cookie_name           = 'ci_session';
  38.     var $cookie_prefix              = '';
  39.     var $cookie_path                = '';
  40.     var $cookie_domain              = '';
  41.     var $cookie_secure              = FALSE;
  42.     var $sess_time_to_update        = 300;
  43.     var $encryption_key             = '';
  44.     var $flashdata_key              = 'flash';
  45.     var $time_reference             = 'time';
  46.     var $gc_probability             = 5;
  47.     var $userdata                   = array();
  48.     var $CI;
  49.     var $now;
  50.  
  51.     /**
  52.      * Session Constructor
  53.      *
  54.      * The constructor runs the session routines automatically
  55.      * whenever the class is instantiated.
  56.      */
  57.     public function __construct($params = array())
  58.     {
  59.         log_message('debug', "Session Class Initialized");
  60.  
  61.         // Set the super object to a local variable for use throughout the class
  62.         $this->CI =& get_instance();
  63.  
  64.         // Set all the session preferences, which can either be set
  65.         // manually via the $params array above or via the config file
  66.         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', 'sess_ignore_ip') as $key)
  67.         {
  68.             $this->$key = (isset($params[$key])) ? $params[$key] : $this->CI->config->item($key);
  69.         }
  70.  
  71.         if ($this->encryption_key == '')
  72.         {
  73.             show_error('In order to use the Session class you are required to set an encryption key in your config file.');
  74.         }
  75.  
  76.         // Load the string helper so we can use the strip_slashes() function
  77.         $this->CI->load->helper('string');
  78.  
  79.         // Do we need encryption? If so, load the encryption class
  80.         if ($this->sess_encrypt_cookie == TRUE)
  81.         {
  82.             $this->CI->load->library('encrypt');
  83.         }
  84.  
  85.         // Are we using a database?  If so, load it
  86.         if ($this->sess_use_database === TRUE AND $this->sess_table_name != '')
  87.         {
  88.             $this->CI->load->database();
  89.         }
  90.  
  91.         // Set the "now" time.  Can either be GMT or server time, based on the
  92.         // config prefs.  We use this to set the "last activity" time
  93.         $this->now = $this->_get_time();
  94.  
  95.         // Set the session length. If the session expiration is
  96.         // set to zero we'll set the expiration two years from now.
  97.         if ($this->sess_expiration == 0)
  98.         {
  99.             $this->sess_expiration = (60*60*24*365*2);
  100.         }
  101.        
  102.         // Set the cookie name
  103.         $this->sess_cookie_name = $this->cookie_prefix.$this->sess_cookie_name;
  104.  
  105.         $ip_address = $this->CI->input->ip_address();
  106.         if(!in_array($ip_address, $this->sess_ignore_ip))
  107.         {
  108.             // Run the Session routine. If a session doesn't exist we'll
  109.             // create a new one.  If it does, we'll update it.
  110.             if ( ! $this->sess_read())
  111.             {
  112.                 $this->sess_create();
  113.             }
  114.             else
  115.             {
  116.                 $this->sess_update();
  117.             }
  118.         }
  119.         else
  120.         {
  121.             log_message('debug', "Session creation skipped for IP: '$ip_address'");
  122.         }
  123.  
  124.         // Delete 'old' flashdata (from last request)
  125.         $this->_flashdata_sweep();
  126.  
  127.         // Mark all new flashdata as old (data will be deleted before next request)
  128.         $this->_flashdata_mark();
  129.  
  130.         // Delete expired sessions if necessary
  131.         $this->_sess_gc();
  132.  
  133.         log_message('debug', "Session routines successfully run");
  134.     }
  135.  
  136.     // --------------------------------------------------------------------
  137.  
  138.     /**
  139.      * Fetch the current session data if it exists
  140.      *
  141.      * @access  public
  142.      * @return  bool
  143.      */
  144.     function sess_read()
  145.     {
  146.         // Fetch the cookie
  147.         $session = $this->CI->input->cookie($this->sess_cookie_name);
  148.  
  149.         // No cookie?  Goodbye cruel world!...
  150.         if ($session === FALSE)
  151.         {
  152.             log_message('debug', 'A session cookie was not found.');
  153.             return FALSE;
  154.         }
  155.  
  156.         // Decrypt the cookie data
  157.         if ($this->sess_encrypt_cookie == TRUE)
  158.         {
  159.             $session = $this->CI->encrypt->decode($session);
  160.         }
  161.         else
  162.         {
  163.             // encryption was not used, so we need to check the md5 hash
  164.             $hash    = substr($session, strlen($session)-32); // get last 32 chars
  165.             $session = substr($session, 0, strlen($session)-32);
  166.  
  167.             // Does the md5 hash match?  This is to prevent manipulation of session data in userspace
  168.             if ($hash !==  md5($session.$this->encryption_key))
  169.             {
  170.                 log_message('error', 'The session cookie data did not match what was expected. This could be a possible hacking attempt.');
  171.                 $this->sess_destroy();
  172.                 return FALSE;
  173.             }
  174.         }
  175.  
  176.         // Unserialize the session array
  177.         $session = $this->_unserialize($session);
  178.  
  179.         // Is the session data we unserialized an array with the correct format?
  180.         if ( ! is_array($session) OR ! isset($session['session_id']) OR ! isset($session['ip_address']) OR ! isset($session['user_agent']) OR ! isset($session['last_activity']))
  181.         {
  182.             $this->sess_destroy();
  183.             return FALSE;
  184.         }
  185.  
  186.         // Is the session current?
  187.         if (($session['last_activity'] + $this->sess_expiration) < $this->now)
  188.         {
  189.             $this->sess_destroy();
  190.             return FALSE;
  191.         }
  192.  
  193.         // Does the IP Match?
  194.         if ($this->sess_match_ip == TRUE AND $session['ip_address'] != $this->CI->input->ip_address())
  195.         {
  196.             $this->sess_destroy();
  197.             return FALSE;
  198.         }
  199.  
  200.         // Does the User Agent Match?
  201.         if ($this->sess_match_useragent == TRUE AND trim($session['user_agent']) != trim(substr($this->CI->input->user_agent(), 0, 120)))
  202.         {
  203.             $this->sess_destroy();
  204.             return FALSE;
  205.         }
  206.  
  207.         // Is there a corresponding session in the DB?
  208.         if ($this->sess_use_database === TRUE)
  209.         {
  210.             $this->CI->db->where('session_id', $session['session_id']);
  211.  
  212.             if ($this->sess_match_ip == TRUE)
  213.             {
  214.                 $this->CI->db->where('ip_address', $session['ip_address']);
  215.             }
  216.  
  217.             if ($this->sess_match_useragent == TRUE)
  218.             {
  219.                 $this->CI->db->where('user_agent', $session['user_agent']);
  220.             }
  221.  
  222.             $query = $this->CI->db->get($this->sess_table_name);
  223.  
  224.             // No result?  Kill it!
  225.             if ($query->num_rows() == 0)
  226.             {
  227.                 $this->sess_destroy();
  228.                 return FALSE;
  229.             }
  230.  
  231.             // Is there custom data?  If so, add it to the main session array
  232.             $row = $query->row();
  233.             if (isset($row->user_data) AND $row->user_data != '')
  234.             {
  235.                 $custom_data = $this->_unserialize($row->user_data);
  236.  
  237.                 if (is_array($custom_data))
  238.                 {
  239.                     foreach ($custom_data as $key => $val)
  240.                     {
  241.                         $session[$key] = $val;
  242.                     }
  243.                 }
  244.             }
  245.         }
  246.  
  247.         // Session is valid!
  248.         $this->userdata = $session;
  249.         unset($session);
  250.  
  251.         return TRUE;
  252.     }
  253.  
  254.     // --------------------------------------------------------------------
  255.  
  256.     /**
  257.      * Write the session data
  258.      *
  259.      * @access  public
  260.      * @return  void
  261.      */
  262.     function sess_write()
  263.     {
  264.         // Are we saving custom data to the DB?  If not, all we do is update the cookie
  265.         if ($this->sess_use_database === FALSE)
  266.         {
  267.             $this->_set_cookie();
  268.             return;
  269.         }
  270.  
  271.         // set the custom userdata, the session data we will set in a second
  272.         $custom_userdata = $this->userdata;
  273.         $cookie_userdata = array();
  274.  
  275.         // Before continuing, we need to determine if there is any custom data to deal with.
  276.         // Let's determine this by removing the default indexes to see if there's anything left in the array
  277.         // and set the session data while we're at it
  278.         foreach (array('session_id','ip_address','user_agent','last_activity') as $val)
  279.         {
  280.             unset($custom_userdata[$val]);
  281.             $cookie_userdata[$val] = $this->userdata[$val];
  282.         }
  283.  
  284.         // Did we find any custom data?  If not, we turn the empty array into a string
  285.         // since there's no reason to serialize and store an empty array in the DB
  286.         if (count($custom_userdata) === 0)
  287.         {
  288.             $custom_userdata = '';
  289.         }
  290.         else
  291.         {
  292.             // Serialize the custom data array so we can store it
  293.             $custom_userdata = $this->_serialize($custom_userdata);
  294.         }
  295.  
  296.         // Run the update query
  297.         $this->CI->db->where('session_id', $this->userdata['session_id']);
  298.         $this->CI->db->update($this->sess_table_name, array('last_activity' => $this->userdata['last_activity'], 'user_data' => $custom_userdata));
  299.  
  300.         // Write the cookie.  Notice that we manually pass the cookie data array to the
  301.         // _set_cookie() function. Normally that function will store $this->userdata, but
  302.         // in this case that array contains custom data, which we do not want in the cookie.
  303.         $this->_set_cookie($cookie_userdata);
  304.     }
  305.  
  306.     // --------------------------------------------------------------------
  307.  
  308.     /**
  309.      * Create a new session
  310.      *
  311.      * @access  public
  312.      * @return  void
  313.      */
  314.     function sess_create()
  315.     {
  316.         $sessid = '';
  317.         while (strlen($sessid) < 32)
  318.         {
  319.             $sessid .= mt_rand(0, mt_getrandmax());
  320.         }
  321.  
  322.         // To make the session ID even more secure we'll combine it with the user's IP
  323.         $sessid .= $this->CI->input->ip_address();
  324.  
  325.         $this->userdata = array(
  326.                             'session_id'    => md5(uniqid($sessid, TRUE)),
  327.                             'ip_address'    => $this->CI->input->ip_address(),
  328.                             'user_agent'    => substr($this->CI->input->user_agent(), 0, 120),
  329.                             'last_activity' => $this->now,
  330.                             'user_data'     => ''
  331.                             );
  332.  
  333.  
  334.         // Save the data to the DB if needed
  335.         if ($this->sess_use_database === TRUE)
  336.         {
  337.             $this->CI->db->query($this->CI->db->insert_string($this->sess_table_name, $this->userdata));
  338.         }
  339.  
  340.         // Write the cookie
  341.         $this->_set_cookie();
  342.     }
  343.  
  344.     // --------------------------------------------------------------------
  345.  
  346.     /**
  347.      * Update an existing session
  348.      *
  349.      * @access  public
  350.      * @return  void
  351.      */
  352.     function sess_update()
  353.     {
  354.         // We only update the session every five minutes by default
  355.         if (($this->userdata['last_activity'] + $this->sess_time_to_update) >= $this->now)
  356.         {
  357.             return;
  358.         }
  359.  
  360.         // Save the old session id so we know which record to
  361.         // update in the database if we need it
  362.         $old_sessid = $this->userdata['session_id'];
  363.         $new_sessid = '';
  364.         while (strlen($new_sessid) < 32)
  365.         {
  366.             $new_sessid .= mt_rand(0, mt_getrandmax());
  367.         }
  368.  
  369.         // To make the session ID even more secure we'll combine it with the user's IP
  370.         $new_sessid .= $this->CI->input->ip_address();
  371.  
  372.         // Turn it into a hash
  373.         $new_sessid = md5(uniqid($new_sessid, TRUE));
  374.  
  375.         // Update the session data in the session data array
  376.         $this->userdata['session_id'] = $new_sessid;
  377.         $this->userdata['last_activity'] = $this->now;
  378.  
  379.         // _set_cookie() will handle this for us if we aren't using database sessions
  380.         // by pushing all userdata to the cookie.
  381.         $cookie_data = NULL;
  382.  
  383.         // Update the session ID and last_activity field in the DB if needed
  384.         if ($this->sess_use_database === TRUE)
  385.         {
  386.             // set cookie explicitly to only have our session data
  387.             $cookie_data = array();
  388.             foreach (array('session_id','ip_address','user_agent','last_activity') as $val)
  389.             {
  390.                 $cookie_data[$val] = $this->userdata[$val];
  391.             }
  392.  
  393.             $this->CI->db->query($this->CI->db->update_string($this->sess_table_name, array('last_activity' => $this->now, 'session_id' => $new_sessid), array('session_id' => $old_sessid)));
  394.         }
  395.  
  396.         // Write the cookie
  397.         $this->_set_cookie($cookie_data);
  398.     }
  399.  
  400.     // --------------------------------------------------------------------
  401.  
  402.     /**
  403.      * Destroy the current session
  404.      *
  405.      * @access  public
  406.      * @return  void
  407.      */
  408.     function sess_destroy()
  409.     {
  410.         // Kill the session DB row
  411.         if ($this->sess_use_database === TRUE AND isset($this->userdata['session_id']))
  412.         {
  413.             $this->CI->db->where('session_id', $this->userdata['session_id']);
  414.             $this->CI->db->delete($this->sess_table_name);
  415.         }
  416.  
  417.         // Kill the cookie
  418.         setcookie(
  419.                     $this->sess_cookie_name,
  420.                     addslashes(serialize(array())),
  421.                     ($this->now - 31500000),
  422.                     $this->cookie_path,
  423.                     $this->cookie_domain,
  424.                     0
  425.                 );
  426.     }
  427.  
  428.     // --------------------------------------------------------------------
  429.  
  430.     /**
  431.      * Fetch a specific item from the session array
  432.      *
  433.      * @access  public
  434.      * @param   string
  435.      * @return  string
  436.      */
  437.     function userdata($item)
  438.     {
  439.         return ( ! isset($this->userdata[$item])) ? FALSE : $this->userdata[$item];
  440.     }
  441.  
  442.     // --------------------------------------------------------------------
  443.  
  444.     /**
  445.      * Fetch all session data
  446.      *
  447.      * @access  public
  448.      * @return  array
  449.      */
  450.     function all_userdata()
  451.     {
  452.         return $this->userdata;
  453.     }
  454.  
  455.     // --------------------------------------------------------------------
  456.  
  457.     /**
  458.      * Add or change data in the "userdata" array
  459.      *
  460.      * @access  public
  461.      * @param   mixed
  462.      * @param   string
  463.      * @return  void
  464.      */
  465.     function set_userdata($newdata = array(), $newval = '')
  466.     {
  467.         if (is_string($newdata))
  468.         {
  469.             $newdata = array($newdata => $newval);
  470.         }
  471.  
  472.         if (count($newdata) > 0)
  473.         {
  474.             foreach ($newdata as $key => $val)
  475.             {
  476.                 $this->userdata[$key] = $val;
  477.             }
  478.         }
  479.  
  480.         $this->sess_write();
  481.     }
  482.  
  483.     // --------------------------------------------------------------------
  484.  
  485.     /**
  486.      * Delete a session variable from the "userdata" array
  487.      *
  488.      * @access  array
  489.      * @return  void
  490.      */
  491.     function unset_userdata($newdata = array())
  492.     {
  493.         if (is_string($newdata))
  494.         {
  495.             $newdata = array($newdata => '');
  496.         }
  497.  
  498.         if (count($newdata) > 0)
  499.         {
  500.             foreach ($newdata as $key => $val)
  501.             {
  502.                 unset($this->userdata[$key]);
  503.             }
  504.         }
  505.  
  506.         $this->sess_write();
  507.     }
  508.  
  509.     // ------------------------------------------------------------------------
  510.  
  511.     /**
  512.      * Add or change flashdata, only available
  513.      * until the next request
  514.      *
  515.      * @access  public
  516.      * @param   mixed
  517.      * @param   string
  518.      * @return  void
  519.      */
  520.     function set_flashdata($newdata = array(), $newval = '')
  521.     {
  522.         if (is_string($newdata))
  523.         {
  524.             $newdata = array($newdata => $newval);
  525.         }
  526.  
  527.         if (count($newdata) > 0)
  528.         {
  529.             foreach ($newdata as $key => $val)
  530.             {
  531.                 $flashdata_key = $this->flashdata_key.':new:'.$key;
  532.                 $this->set_userdata($flashdata_key, $val);
  533.             }
  534.         }
  535.     }
  536.  
  537.     // ------------------------------------------------------------------------
  538.  
  539.     /**
  540.      * Keeps existing flashdata available to next request.
  541.      *
  542.      * @access  public
  543.      * @param   string
  544.      * @return  void
  545.      */
  546.     function keep_flashdata($key)
  547.     {
  548.         // 'old' flashdata gets removed.  Here we mark all
  549.         // flashdata as 'new' to preserve it from _flashdata_sweep()
  550.         // Note the function will return FALSE if the $key
  551.         // provided cannot be found
  552.         $old_flashdata_key = $this->flashdata_key.':old:'.$key;
  553.         $value = $this->userdata($old_flashdata_key);
  554.  
  555.         $new_flashdata_key = $this->flashdata_key.':new:'.$key;
  556.         $this->set_userdata($new_flashdata_key, $value);
  557.     }
  558.  
  559.     // ------------------------------------------------------------------------
  560.  
  561.     /**
  562.      * Fetch a specific flashdata item from the session array
  563.      *
  564.      * @access  public
  565.      * @param   string
  566.      * @return  string
  567.      */
  568.     function flashdata($key)
  569.     {
  570.         $flashdata_key = $this->flashdata_key.':old:'.$key;
  571.         return $this->userdata($flashdata_key);
  572.     }
  573.  
  574.     // ------------------------------------------------------------------------
  575.  
  576.     /**
  577.      * Identifies flashdata as 'old' for removal
  578.      * when _flashdata_sweep() runs.
  579.      *
  580.      * @access  private
  581.      * @return  void
  582.      */
  583.     function _flashdata_mark()
  584.     {
  585.         $userdata = $this->all_userdata();
  586.         foreach ($userdata as $name => $value)
  587.         {
  588.             $parts = explode(':new:', $name);
  589.             if (is_array($parts) && count($parts) === 2)
  590.             {
  591.                 $new_name = $this->flashdata_key.':old:'.$parts[1];
  592.                 $this->set_userdata($new_name, $value);
  593.                 $this->unset_userdata($name);
  594.             }
  595.         }
  596.     }
  597.  
  598.     // ------------------------------------------------------------------------
  599.  
  600.     /**
  601.      * Removes all flashdata marked as 'old'
  602.      *
  603.      * @access  private
  604.      * @return  void
  605.      */
  606.  
  607.     function _flashdata_sweep()
  608.     {
  609.         $userdata = $this->all_userdata();
  610.         foreach ($userdata as $key => $value)
  611.         {
  612.             if (strpos($key, ':old:'))
  613.             {
  614.                 $this->unset_userdata($key);
  615.             }
  616.         }
  617.  
  618.     }
  619.  
  620.     // --------------------------------------------------------------------
  621.  
  622.     /**
  623.      * Get the "now" time
  624.      *
  625.      * @access  private
  626.      * @return  string
  627.      */
  628.     function _get_time()
  629.     {
  630.         if (strtolower($this->time_reference) == 'gmt')
  631.         {
  632.             $now = time();
  633.             $time = mktime(gmdate("H", $now), gmdate("i", $now), gmdate("s", $now), gmdate("m", $now), gmdate("d", $now), gmdate("Y", $now));
  634.         }
  635.         else
  636.         {
  637.             $time = time();
  638.         }
  639.  
  640.         return $time;
  641.     }
  642.  
  643.     // --------------------------------------------------------------------
  644.  
  645.     /**
  646.      * Write the session cookie
  647.      *
  648.      * @access  public
  649.      * @return  void
  650.      */
  651.     function _set_cookie($cookie_data = NULL)
  652.     {
  653.         if (is_null($cookie_data))
  654.         {
  655.             $cookie_data = $this->userdata;
  656.         }
  657.  
  658.         // Serialize the userdata for the cookie
  659.         $cookie_data = $this->_serialize($cookie_data);
  660.  
  661.         if ($this->sess_encrypt_cookie == TRUE)
  662.         {
  663.             $cookie_data = $this->CI->encrypt->encode($cookie_data);
  664.         }
  665.         else
  666.         {
  667.             // if encryption is not used, we provide an md5 hash to prevent userside tampering
  668.             $cookie_data = $cookie_data.md5($cookie_data.$this->encryption_key);
  669.         }
  670.  
  671.         $expire = ($this->sess_expire_on_close === TRUE) ? 0 : $this->sess_expiration + time();
  672.  
  673.         // Set the cookie
  674.         setcookie(
  675.                     $this->sess_cookie_name,
  676.                     $cookie_data,
  677.                     $expire,
  678.                     $this->cookie_path,
  679.                     $this->cookie_domain,
  680.                     $this->cookie_secure
  681.                 );
  682.     }
  683.  
  684.     // --------------------------------------------------------------------
  685.  
  686.     /**
  687.      * Serialize an array
  688.      *
  689.      * This function first converts any slashes found in the array to a temporary
  690.      * marker, so when it gets unserialized the slashes will be preserved
  691.      *
  692.      * @access  private
  693.      * @param   array
  694.      * @return  string
  695.      */
  696.     function _serialize($data)
  697.     {
  698.         if (is_array($data))
  699.         {
  700.             foreach ($data as $key => $val)
  701.             {
  702.                 if (is_string($val))
  703.                 {
  704.                     $data[$key] = str_replace('\\', '{{slash}}', $val);
  705.                 }
  706.             }
  707.         }
  708.         else
  709.         {
  710.             if (is_string($data))
  711.             {
  712.                 $data = str_replace('\\', '{{slash}}', $data);
  713.             }
  714.         }
  715.  
  716.         return serialize($data);
  717.     }
  718.  
  719.     // --------------------------------------------------------------------
  720.  
  721.     /**
  722.      * Unserialize
  723.      *
  724.      * This function unserializes a data string, then converts any
  725.      * temporary slash markers back to actual slashes
  726.      *
  727.      * @access  private
  728.      * @param   array
  729.      * @return  string
  730.      */
  731.     function _unserialize($data)
  732.     {
  733.         $data = @unserialize(strip_slashes($data));
  734.  
  735.         if (is_array($data))
  736.         {
  737.             foreach ($data as $key => $val)
  738.             {
  739.                 if (is_string($val))
  740.                 {
  741.                     $data[$key] = str_replace('{{slash}}', '\\', $val);
  742.                 }
  743.             }
  744.  
  745.             return $data;
  746.         }
  747.  
  748.         return (is_string($data)) ? str_replace('{{slash}}', '\\', $data) : $data;
  749.     }
  750.  
  751.     // --------------------------------------------------------------------
  752.  
  753.     /**
  754.      * Garbage collection
  755.      *
  756.      * This deletes expired session rows from database
  757.      * if the probability percentage is met
  758.      *
  759.      * @access  public
  760.      * @return  void
  761.      */
  762.     function _sess_gc()
  763.     {
  764.         if ($this->sess_use_database != TRUE)
  765.         {
  766.             return;
  767.         }
  768.  
  769.         srand(time());
  770.         if ((rand() % 100) < $this->gc_probability)
  771.         {
  772.             $expire = $this->now - $this->sess_expiration;
  773.  
  774.             $this->CI->db->where("last_activity < {$expire}");
  775.             $this->CI->db->where_in('ip_address', $this->sess_ignore_ip);
  776.             $this->CI->db->delete($this->sess_table_name);
  777.  
  778.             log_message('debug', 'Session garbage collection performed.');
  779.         }
  780.     }
  781.  
  782.  
  783. }
  784. // END Session Class
  785.  
  786. /* End of file Session.php */
  787. /* Location: ./system/libraries/Session.php */
Advertisement
Add Comment
Please, Sign In to add comment