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