blob: c97e15356f12da359b13c0027ae090f21d21ae91 [file] [log] [blame]
Darren Hillc4e266b2011-08-30 15:40:27 -04001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +03005 * 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 Hillc4e266b2011-08-30 15:40:27 -040018 *
19 * @package CodeIgniter
Andrey Andreev9ffcee62012-09-05 16:25:16 +030020 * @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 Hillc4e266b2011-08-30 15:40:27 -040023 * @link http://codeigniter.com
Andrey Andreev9ffcee62012-09-05 16:25:16 +030024 * @since Version 1.0
Darren Hillc4e266b2011-08-30 15:40:27 -040025 * @filesource
26 */
27
Darren Hillc4e266b2011-08-30 15:40:27 -040028/**
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 Andreev9ffcee62012-09-05 16:25:16 +030036 * @author EllisLab Dev Team
Darren Hillc4e266b2011-08-30 15:40:27 -040037 */
Darren Hill5073a372011-08-31 13:54:19 -040038class CI_Session_native extends CI_Session_driver {
Andrey Andreev9ffcee62012-09-05 16:25:16 +030039
Darren Hillc4e266b2011-08-30 15:40:27 -040040 /**
41 * Initialize session driver object
42 *
Darren Hillc4e266b2011-08-30 15:40:27 -040043 * @return void
44 */
45 protected function initialize()
46 {
47 // Get config parameters
48 $config = array();
49 $CI =& get_instance();
dchill4226429202012-07-31 10:55:07 -040050 $prefs = array(
51 'sess_cookie_name',
52 'sess_expire_on_close',
53 'sess_expiration',
54 'sess_match_ip',
55 'sess_match_useragent',
dchill42f79afb52012-08-08 12:03:46 -040056 'sess_time_to_update',
dchill4226429202012-07-31 10:55:07 -040057 'cookie_prefix',
58 'cookie_path',
59 'cookie_domain'
60 );
Andrey Andreev9ffcee62012-09-05 16:25:16 +030061
dchill4226429202012-07-31 10:55:07 -040062 foreach ($prefs as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -040063 {
Andrey Andreev9ffcee62012-09-05 16:25:16 +030064 $config[$key] = isset($this->_parent->params[$key])
65 ? $this->_parent->params[$key]
66 : $CI->config->item($key);
Darren Hillc4e266b2011-08-30 15:40:27 -040067 }
68
69 // Set session name, if specified
70 if ($config['sess_cookie_name'])
71 {
dchill42aee92652012-08-26 21:45:35 -040072 // Differentiate name from cookie driver with '_id' suffix
73 $name = $config['sess_cookie_name'].'_id';
Darren Hillc4e266b2011-08-30 15:40:27 -040074 if ($config['cookie_prefix'])
75 {
76 // Prepend cookie prefix
77 $name = $config['cookie_prefix'].$name;
78 }
79 session_name($name);
80 }
81
82 // Set expiration, path, and domain
83 $expire = 7200;
84 $path = '/';
85 $domain = '';
86 if ($config['sess_expiration'] !== FALSE)
87 {
88 // Default to 2 years if expiration is "0"
89 $expire = ($config['sess_expiration'] == 0) ? (60*60*24*365*2) : $config['sess_expiration'];
90 }
Andrey Andreev9ffcee62012-09-05 16:25:16 +030091
Darren Hillc4e266b2011-08-30 15:40:27 -040092 if ($config['cookie_path'])
93 {
94 // Use specified path
95 $path = $config['cookie_path'];
96 }
Andrey Andreev9ffcee62012-09-05 16:25:16 +030097
Darren Hillc4e266b2011-08-30 15:40:27 -040098 if ($config['cookie_domain'])
99 {
100 // Use specified domain
101 $domain = $config['cookie_domain'];
102 }
103 session_set_cookie_params($config['sess_expire_on_close'] ? 0 : $expire, $path, $domain);
104
105 // Start session
106 session_start();
107
108 // Check session expiration, ip, and agent
109 $now = time();
110 $destroy = FALSE;
111 if (isset($_SESSION['last_activity']) && ($_SESSION['last_activity'] + $expire) < $now)
112 {
113 // Expired - destroy
114 $destroy = TRUE;
115 }
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300116 elseif ($config['sess_match_ip'] === TRUE && isset($_SESSION['ip_address'])
117 && $_SESSION['ip_address'] !== $CI->input->ip_address())
Darren Hillc4e266b2011-08-30 15:40:27 -0400118 {
119 // IP doesn't match - destroy
120 $destroy = TRUE;
121 }
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300122 elseif ($config['sess_match_useragent'] === TRUE && isset($_SESSION['user_agent'])
123 && $_SESSION['user_agent'] !== trim(substr($CI->input->user_agent(), 0, 50)))
Darren Hillc4e266b2011-08-30 15:40:27 -0400124 {
125 // Agent doesn't match - destroy
126 $destroy = TRUE;
127 }
128
129 // Destroy expired or invalid session
130 if ($destroy)
131 {
132 // Clear old session and start new
133 $this->sess_destroy();
134 session_start();
135 }
136
dchill42f79afb52012-08-08 12:03:46 -0400137 // Check for update time
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300138 if ($config['sess_time_to_update'] && isset($_SESSION['last_activity'])
139 && ($_SESSION['last_activity'] + $config['sess_time_to_update']) < $now)
dchill42f79afb52012-08-08 12:03:46 -0400140 {
141 // Regenerate ID, but don't destroy session
142 $this->sess_regenerate(FALSE);
143 }
144
Darren Hillc4e266b2011-08-30 15:40:27 -0400145 // Set activity time
146 $_SESSION['last_activity'] = $now;
147
148 // Set matching values as required
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300149 if ($config['sess_match_ip'] === TRUE && ! isset($_SESSION['ip_address']))
Darren Hillc4e266b2011-08-30 15:40:27 -0400150 {
151 // Store user IP address
152 $_SESSION['ip_address'] = $CI->input->ip_address();
153 }
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300154
155 if ($config['sess_match_useragent'] === TRUE && ! isset($_SESSION['user_agent']))
Darren Hillc4e266b2011-08-30 15:40:27 -0400156 {
157 // Store user agent string
158 $_SESSION['user_agent'] = trim(substr($CI->input->user_agent(), 0, 50));
159 }
dchill42f79afb52012-08-08 12:03:46 -0400160
161 // Make session ID available
162 $_SESSION['session_id'] = session_id();
Darren Hillc4e266b2011-08-30 15:40:27 -0400163 }
164
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300165 // ------------------------------------------------------------------------
166
Darren Hillc4e266b2011-08-30 15:40:27 -0400167 /**
168 * Save the session data
169 *
Darren Hilla2ae6572011-09-01 07:36:26 -0400170 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400171 */
172 public function sess_save()
173 {
174 // Nothing to do - changes to $_SESSION are automatically saved
175 }
176
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300177 // ------------------------------------------------------------------------
178
Darren Hillc4e266b2011-08-30 15:40:27 -0400179 /**
180 * Destroy the current session
181 *
Darren Hilla2ae6572011-09-01 07:36:26 -0400182 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400183 */
184 public function sess_destroy()
185 {
186 // Cleanup session
187 $_SESSION = array();
188 $name = session_name();
189 if (isset($_COOKIE[$name]))
190 {
191 // Clear session cookie
192 $params = session_get_cookie_params();
193 setcookie($name, '', time() - 42000, $params['path'], $params['domain']);
194 unset($_COOKIE[$name]);
195 }
196 session_destroy();
197 }
198
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300199 // ------------------------------------------------------------------------
200
Darren Hillc4e266b2011-08-30 15:40:27 -0400201 /**
202 * Regenerate the current session
203 *
204 * Regenerate the session id
205 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300206 * @param bool Destroy session data flag (default: FALSE)
Darren Hilla2ae6572011-09-01 07:36:26 -0400207 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400208 */
dchill4277ee3fd2012-07-24 11:50:01 -0400209 public function sess_regenerate($destroy = FALSE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400210 {
211 // Just regenerate id, passing destroy flag
212 session_regenerate_id($destroy);
dchill42f79afb52012-08-08 12:03:46 -0400213 $_SESSION['session_id'] = session_id();
Darren Hillc4e266b2011-08-30 15:40:27 -0400214 }
215
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300216 // ------------------------------------------------------------------------
217
Darren Hillc4e266b2011-08-30 15:40:27 -0400218 /**
219 * Get a reference to user data array
220 *
Darren Hilla2ae6572011-09-01 07:36:26 -0400221 * @return array Reference to userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400222 */
223 public function &get_userdata()
224 {
225 // Just return reference to $_SESSION
226 return $_SESSION;
227 }
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300228
Darren Hillc4e266b2011-08-30 15:40:27 -0400229}
Darren Hillc4e266b2011-08-30 15:40:27 -0400230
231/* End of file Session_native.php */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300232/* Location: ./system/libraries/Session/drivers/Session_native.php */