blob: 8d5e51546652066b154fe99ce8559193fef37812 [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();
dchill4226429202012-07-31 10:55:07 -040049 $prefs = array(
50 'sess_cookie_name',
51 'sess_expire_on_close',
52 'sess_expiration',
53 'sess_match_ip',
54 'sess_match_useragent',
dchill42f79afb52012-08-08 12:03:46 -040055 'sess_time_to_update',
dchill4226429202012-07-31 10:55:07 -040056 'cookie_prefix',
57 'cookie_path',
58 'cookie_domain'
59 );
Andrey Andreev9ffcee62012-09-05 16:25:16 +030060
dchill4226429202012-07-31 10:55:07 -040061 foreach ($prefs as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -040062 {
Andrey Andreev9ffcee62012-09-05 16:25:16 +030063 $config[$key] = isset($this->_parent->params[$key])
64 ? $this->_parent->params[$key]
Andrey Andreev2e3e2302012-10-09 15:52:34 +030065 : $this->CI->config->item($key);
Darren Hillc4e266b2011-08-30 15:40:27 -040066 }
67
68 // Set session name, if specified
69 if ($config['sess_cookie_name'])
70 {
dchill42aee92652012-08-26 21:45:35 -040071 // Differentiate name from cookie driver with '_id' suffix
72 $name = $config['sess_cookie_name'].'_id';
Darren Hillc4e266b2011-08-30 15:40:27 -040073 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 Andreev9ffcee62012-09-05 16:25:16 +030090
Darren Hillc4e266b2011-08-30 15:40:27 -040091 if ($config['cookie_path'])
92 {
93 // Use specified path
94 $path = $config['cookie_path'];
95 }
Andrey Andreev9ffcee62012-09-05 16:25:16 +030096
Darren Hillc4e266b2011-08-30 15:40:27 -040097 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 Andreev9ffcee62012-09-05 16:25:16 +0300115 elseif ($config['sess_match_ip'] === TRUE && isset($_SESSION['ip_address'])
Andrey Andreev2e3e2302012-10-09 15:52:34 +0300116 && $_SESSION['ip_address'] !== $this->CI->input->ip_address())
Darren Hillc4e266b2011-08-30 15:40:27 -0400117 {
118 // IP doesn't match - destroy
119 $destroy = TRUE;
120 }
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300121 elseif ($config['sess_match_useragent'] === TRUE && isset($_SESSION['user_agent'])
Andrey Andreev2e3e2302012-10-09 15:52:34 +0300122 && $_SESSION['user_agent'] !== trim(substr($this->CI->input->user_agent(), 0, 50)))
Darren Hillc4e266b2011-08-30 15:40:27 -0400123 {
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
dchill42f79afb52012-08-08 12:03:46 -0400136 // Check for update time
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300137 if ($config['sess_time_to_update'] && isset($_SESSION['last_activity'])
138 && ($_SESSION['last_activity'] + $config['sess_time_to_update']) < $now)
dchill42f79afb52012-08-08 12:03:46 -0400139 {
140 // Regenerate ID, but don't destroy session
141 $this->sess_regenerate(FALSE);
142 }
143
Darren Hillc4e266b2011-08-30 15:40:27 -0400144 // Set activity time
145 $_SESSION['last_activity'] = $now;
146
147 // Set matching values as required
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300148 if ($config['sess_match_ip'] === TRUE && ! isset($_SESSION['ip_address']))
Darren Hillc4e266b2011-08-30 15:40:27 -0400149 {
150 // Store user IP address
Andrey Andreev2e3e2302012-10-09 15:52:34 +0300151 $_SESSION['ip_address'] = $this->CI->input->ip_address();
Darren Hillc4e266b2011-08-30 15:40:27 -0400152 }
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300153
154 if ($config['sess_match_useragent'] === TRUE && ! isset($_SESSION['user_agent']))
Darren Hillc4e266b2011-08-30 15:40:27 -0400155 {
156 // Store user agent string
Andrey Andreev2e3e2302012-10-09 15:52:34 +0300157 $_SESSION['user_agent'] = trim(substr($this->CI->input->user_agent(), 0, 50));
Darren Hillc4e266b2011-08-30 15:40:27 -0400158 }
dchill42f79afb52012-08-08 12:03:46 -0400159
160 // Make session ID available
161 $_SESSION['session_id'] = session_id();
Darren Hillc4e266b2011-08-30 15:40:27 -0400162 }
163
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300164 // ------------------------------------------------------------------------
165
Darren Hillc4e266b2011-08-30 15:40:27 -0400166 /**
167 * Save the session data
168 *
Darren Hilla2ae6572011-09-01 07:36:26 -0400169 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400170 */
171 public function sess_save()
172 {
173 // Nothing to do - changes to $_SESSION are automatically saved
174 }
175
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300176 // ------------------------------------------------------------------------
177
Darren Hillc4e266b2011-08-30 15:40:27 -0400178 /**
179 * Destroy the current session
180 *
Darren Hilla2ae6572011-09-01 07:36:26 -0400181 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400182 */
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 Andreev9ffcee62012-09-05 16:25:16 +0300198 // ------------------------------------------------------------------------
199
Darren Hillc4e266b2011-08-30 15:40:27 -0400200 /**
201 * Regenerate the current session
202 *
203 * Regenerate the session id
204 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300205 * @param bool Destroy session data flag (default: FALSE)
Darren Hilla2ae6572011-09-01 07:36:26 -0400206 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400207 */
dchill4277ee3fd2012-07-24 11:50:01 -0400208 public function sess_regenerate($destroy = FALSE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400209 {
210 // Just regenerate id, passing destroy flag
211 session_regenerate_id($destroy);
dchill42f79afb52012-08-08 12:03:46 -0400212 $_SESSION['session_id'] = session_id();
Darren Hillc4e266b2011-08-30 15:40:27 -0400213 }
214
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300215 // ------------------------------------------------------------------------
216
Darren Hillc4e266b2011-08-30 15:40:27 -0400217 /**
218 * Get a reference to user data array
219 *
Darren Hilla2ae6572011-09-01 07:36:26 -0400220 * @return array Reference to userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400221 */
222 public function &get_userdata()
223 {
224 // Just return reference to $_SESSION
225 return $_SESSION;
226 }
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300227
Darren Hillc4e266b2011-08-30 15:40:27 -0400228}
Darren Hillc4e266b2011-08-30 15:40:27 -0400229
230/* End of file Session_native.php */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300231/* Location: ./system/libraries/Session/drivers/Session_native.php */