Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 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 |
Darren Hill | a2ae657 | 2011-09-01 07:36:26 -0400 | [diff] [blame] | 8 | * @author ExpressionEngine Dev Team |
| 9 | * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc. |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html |
| 11 | * @link http://codeigniter.com |
Darren Hill | a2ae657 | 2011-09-01 07:36:26 -0400 | [diff] [blame] | 12 | * @since Version 2.0 |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | |
| 17 | /** |
| 18 | * Native PHP session management driver |
| 19 | * |
| 20 | * This is the driver that uses the native PHP $_SESSION array through the Session driver library. |
| 21 | * |
| 22 | * @package CodeIgniter |
| 23 | * @subpackage Libraries |
| 24 | * @category Sessions |
Darren Hill | 00fcb54 | 2011-09-12 07:57:04 -0400 | [diff] [blame] | 25 | * @author ExpressionEngine Dev Team |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 26 | */ |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 27 | class CI_Session_native extends CI_Session_driver { |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 28 | /** |
| 29 | * Initialize session driver object |
| 30 | * |
Darren Hill | a2ae657 | 2011-09-01 07:36:26 -0400 | [diff] [blame] | 31 | * @access protected |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 32 | * @return void |
| 33 | */ |
| 34 | protected function initialize() |
| 35 | { |
| 36 | // Get config parameters |
| 37 | $config = array(); |
| 38 | $CI =& get_instance(); |
dchill42 | 2642920 | 2012-07-31 10:55:07 -0400 | [diff] [blame] | 39 | $prefs = array( |
| 40 | 'sess_cookie_name', |
| 41 | 'sess_expire_on_close', |
| 42 | 'sess_expiration', |
| 43 | 'sess_match_ip', |
| 44 | 'sess_match_useragent', |
dchill42 | f79afb5 | 2012-08-08 12:03:46 -0400 | [diff] [blame] | 45 | 'sess_time_to_update', |
dchill42 | 2642920 | 2012-07-31 10:55:07 -0400 | [diff] [blame] | 46 | 'cookie_prefix', |
| 47 | 'cookie_path', |
| 48 | 'cookie_domain' |
| 49 | ); |
| 50 | foreach ($prefs as $key) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 51 | { |
dchill42 | 2642920 | 2012-07-31 10:55:07 -0400 | [diff] [blame] | 52 | $config[$key] = isset($this->_parent->params[$key]) ? $this->_parent->params[$key] : |
| 53 | $CI->config->item($key); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | // Set session name, if specified |
| 57 | if ($config['sess_cookie_name']) |
| 58 | { |
dchill42 | aee9265 | 2012-08-26 21:45:35 -0400 | [diff] [blame] | 59 | // Differentiate name from cookie driver with '_id' suffix |
| 60 | $name = $config['sess_cookie_name'].'_id'; |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 61 | if ($config['cookie_prefix']) |
| 62 | { |
| 63 | // Prepend cookie prefix |
| 64 | $name = $config['cookie_prefix'].$name; |
| 65 | } |
| 66 | session_name($name); |
| 67 | } |
| 68 | |
| 69 | // Set expiration, path, and domain |
| 70 | $expire = 7200; |
| 71 | $path = '/'; |
| 72 | $domain = ''; |
| 73 | if ($config['sess_expiration'] !== FALSE) |
| 74 | { |
| 75 | // Default to 2 years if expiration is "0" |
| 76 | $expire = ($config['sess_expiration'] == 0) ? (60*60*24*365*2) : $config['sess_expiration']; |
| 77 | } |
| 78 | if ($config['cookie_path']) |
| 79 | { |
| 80 | // Use specified path |
| 81 | $path = $config['cookie_path']; |
| 82 | } |
| 83 | if ($config['cookie_domain']) |
| 84 | { |
| 85 | // Use specified domain |
| 86 | $domain = $config['cookie_domain']; |
| 87 | } |
| 88 | session_set_cookie_params($config['sess_expire_on_close'] ? 0 : $expire, $path, $domain); |
| 89 | |
| 90 | // Start session |
| 91 | session_start(); |
| 92 | |
| 93 | // Check session expiration, ip, and agent |
| 94 | $now = time(); |
| 95 | $destroy = FALSE; |
| 96 | if (isset($_SESSION['last_activity']) && ($_SESSION['last_activity'] + $expire) < $now) |
| 97 | { |
| 98 | // Expired - destroy |
| 99 | $destroy = TRUE; |
| 100 | } |
| 101 | else if ($config['sess_match_ip'] == TRUE && isset($_SESSION['ip_address']) && |
| 102 | $_SESSION['ip_address'] != $CI->input->ip_address()) |
| 103 | { |
| 104 | // IP doesn't match - destroy |
| 105 | $destroy = TRUE; |
| 106 | } |
| 107 | else if ($config['sess_match_useragent'] == TRUE && isset($_SESSION['user_agent']) && |
| 108 | $_SESSION['user_agent'] != trim(substr($CI->input->user_agent(), 0, 50))) |
| 109 | { |
| 110 | // Agent doesn't match - destroy |
| 111 | $destroy = TRUE; |
| 112 | } |
| 113 | |
| 114 | // Destroy expired or invalid session |
| 115 | if ($destroy) |
| 116 | { |
| 117 | // Clear old session and start new |
| 118 | $this->sess_destroy(); |
| 119 | session_start(); |
| 120 | } |
| 121 | |
dchill42 | f79afb5 | 2012-08-08 12:03:46 -0400 | [diff] [blame] | 122 | // Check for update time |
| 123 | if ($config['sess_time_to_update'] && isset($_SESSION['last_activity']) && |
| 124 | ($_SESSION['last_activity'] + $config['sess_time_to_update']) < $now) |
| 125 | { |
| 126 | // Regenerate ID, but don't destroy session |
| 127 | $this->sess_regenerate(FALSE); |
| 128 | } |
| 129 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 130 | // Set activity time |
| 131 | $_SESSION['last_activity'] = $now; |
| 132 | |
| 133 | // Set matching values as required |
| 134 | if ($config['sess_match_ip'] == TRUE && !isset($_SESSION['ip_address'])) |
| 135 | { |
| 136 | // Store user IP address |
| 137 | $_SESSION['ip_address'] = $CI->input->ip_address(); |
| 138 | } |
| 139 | if ($config['sess_match_useragent'] == TRUE && !isset($_SESSION['user_agent'])) |
| 140 | { |
| 141 | // Store user agent string |
| 142 | $_SESSION['user_agent'] = trim(substr($CI->input->user_agent(), 0, 50)); |
| 143 | } |
dchill42 | f79afb5 | 2012-08-08 12:03:46 -0400 | [diff] [blame] | 144 | |
| 145 | // Make session ID available |
| 146 | $_SESSION['session_id'] = session_id(); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Save the session data |
| 151 | * |
Darren Hill | a2ae657 | 2011-09-01 07:36:26 -0400 | [diff] [blame] | 152 | * @access public |
| 153 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 154 | */ |
| 155 | public function sess_save() |
| 156 | { |
| 157 | // Nothing to do - changes to $_SESSION are automatically saved |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Destroy the current session |
| 162 | * |
Darren Hill | a2ae657 | 2011-09-01 07:36:26 -0400 | [diff] [blame] | 163 | * @access public |
| 164 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 165 | */ |
| 166 | public function sess_destroy() |
| 167 | { |
| 168 | // Cleanup session |
| 169 | $_SESSION = array(); |
| 170 | $name = session_name(); |
| 171 | if (isset($_COOKIE[$name])) |
| 172 | { |
| 173 | // Clear session cookie |
| 174 | $params = session_get_cookie_params(); |
| 175 | setcookie($name, '', time() - 42000, $params['path'], $params['domain']); |
| 176 | unset($_COOKIE[$name]); |
| 177 | } |
| 178 | session_destroy(); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Regenerate the current session |
| 183 | * |
| 184 | * Regenerate the session id |
| 185 | * |
Darren Hill | a2ae657 | 2011-09-01 07:36:26 -0400 | [diff] [blame] | 186 | * @access public |
dchill42 | 77ee3fd | 2012-07-24 11:50:01 -0400 | [diff] [blame] | 187 | * @param boolean Destroy session data flag (default: FALSE) |
Darren Hill | a2ae657 | 2011-09-01 07:36:26 -0400 | [diff] [blame] | 188 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 189 | */ |
dchill42 | 77ee3fd | 2012-07-24 11:50:01 -0400 | [diff] [blame] | 190 | public function sess_regenerate($destroy = FALSE) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 191 | { |
| 192 | // Just regenerate id, passing destroy flag |
| 193 | session_regenerate_id($destroy); |
dchill42 | f79afb5 | 2012-08-08 12:03:46 -0400 | [diff] [blame] | 194 | $_SESSION['session_id'] = session_id(); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Get a reference to user data array |
| 199 | * |
Darren Hill | a2ae657 | 2011-09-01 07:36:26 -0400 | [diff] [blame] | 200 | * @access public |
| 201 | * @return array Reference to userdata |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 202 | */ |
| 203 | public function &get_userdata() |
| 204 | { |
| 205 | // Just return reference to $_SESSION |
| 206 | return $_SESSION; |
| 207 | } |
| 208 | } |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 209 | |
| 210 | /* End of file Session_native.php */ |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 211 | /* Location: ./system/libraries/Session/drivers/Session_native.php */ |