blob: 04c985574bdcc2cdc69ad8262084b3ecd50ff2db [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 *
5 * An open source application development framework for PHP 5.1.6 or newer
6 *
7 * @package CodeIgniter
Darren Hilla2ae6572011-09-01 07:36:26 -04008 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc.
Darren Hillc4e266b2011-08-30 15:40:27 -040010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Darren Hilla2ae6572011-09-01 07:36:26 -040012 * @since Version 2.0
Darren Hillc4e266b2011-08-30 15:40:27 -040013 * @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 Hill00fcb542011-09-12 07:57:04 -040025 * @author ExpressionEngine Dev Team
Darren Hillc4e266b2011-08-30 15:40:27 -040026 */
Darren Hill5073a372011-08-31 13:54:19 -040027class CI_Session_native extends CI_Session_driver {
Darren Hillc4e266b2011-08-30 15:40:27 -040028 /**
29 * Initialize session driver object
30 *
Darren Hilla2ae6572011-09-01 07:36:26 -040031 * @access protected
Darren Hillc4e266b2011-08-30 15:40:27 -040032 * @return void
33 */
34 protected function initialize()
35 {
36 // Get config parameters
37 $config = array();
38 $CI =& get_instance();
dchill4226429202012-07-31 10:55:07 -040039 $prefs = array(
40 'sess_cookie_name',
41 'sess_expire_on_close',
42 'sess_expiration',
43 'sess_match_ip',
44 'sess_match_useragent',
dchill42f79afb52012-08-08 12:03:46 -040045 'sess_time_to_update',
dchill4226429202012-07-31 10:55:07 -040046 'cookie_prefix',
47 'cookie_path',
48 'cookie_domain'
49 );
50 foreach ($prefs as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -040051 {
dchill4226429202012-07-31 10:55:07 -040052 $config[$key] = isset($this->_parent->params[$key]) ? $this->_parent->params[$key] :
53 $CI->config->item($key);
Darren Hillc4e266b2011-08-30 15:40:27 -040054 }
55
56 // Set session name, if specified
57 if ($config['sess_cookie_name'])
58 {
59 $name = $config['sess_cookie_name'];
60 if ($config['cookie_prefix'])
61 {
62 // Prepend cookie prefix
63 $name = $config['cookie_prefix'].$name;
64 }
65 session_name($name);
66 }
67
68 // Set expiration, path, and domain
69 $expire = 7200;
70 $path = '/';
71 $domain = '';
72 if ($config['sess_expiration'] !== FALSE)
73 {
74 // Default to 2 years if expiration is "0"
75 $expire = ($config['sess_expiration'] == 0) ? (60*60*24*365*2) : $config['sess_expiration'];
76 }
77 if ($config['cookie_path'])
78 {
79 // Use specified path
80 $path = $config['cookie_path'];
81 }
82 if ($config['cookie_domain'])
83 {
84 // Use specified domain
85 $domain = $config['cookie_domain'];
86 }
87 session_set_cookie_params($config['sess_expire_on_close'] ? 0 : $expire, $path, $domain);
88
89 // Start session
90 session_start();
91
92 // Check session expiration, ip, and agent
93 $now = time();
94 $destroy = FALSE;
95 if (isset($_SESSION['last_activity']) && ($_SESSION['last_activity'] + $expire) < $now)
96 {
97 // Expired - destroy
98 $destroy = TRUE;
99 }
100 else if ($config['sess_match_ip'] == TRUE && isset($_SESSION['ip_address']) &&
101 $_SESSION['ip_address'] != $CI->input->ip_address())
102 {
103 // IP doesn't match - destroy
104 $destroy = TRUE;
105 }
106 else if ($config['sess_match_useragent'] == TRUE && isset($_SESSION['user_agent']) &&
107 $_SESSION['user_agent'] != trim(substr($CI->input->user_agent(), 0, 50)))
108 {
109 // Agent doesn't match - destroy
110 $destroy = TRUE;
111 }
112
113 // Destroy expired or invalid session
114 if ($destroy)
115 {
116 // Clear old session and start new
117 $this->sess_destroy();
118 session_start();
119 }
120
dchill42f79afb52012-08-08 12:03:46 -0400121 // Check for update time
122 if ($config['sess_time_to_update'] && isset($_SESSION['last_activity']) &&
123 ($_SESSION['last_activity'] + $config['sess_time_to_update']) < $now)
124 {
125 // Regenerate ID, but don't destroy session
126 $this->sess_regenerate(FALSE);
127 }
128
Darren Hillc4e266b2011-08-30 15:40:27 -0400129 // Set activity time
130 $_SESSION['last_activity'] = $now;
131
132 // Set matching values as required
133 if ($config['sess_match_ip'] == TRUE && !isset($_SESSION['ip_address']))
134 {
135 // Store user IP address
136 $_SESSION['ip_address'] = $CI->input->ip_address();
137 }
138 if ($config['sess_match_useragent'] == TRUE && !isset($_SESSION['user_agent']))
139 {
140 // Store user agent string
141 $_SESSION['user_agent'] = trim(substr($CI->input->user_agent(), 0, 50));
142 }
dchill42f79afb52012-08-08 12:03:46 -0400143
144 // Make session ID available
145 $_SESSION['session_id'] = session_id();
Darren Hillc4e266b2011-08-30 15:40:27 -0400146 }
147
148 /**
149 * Save the session data
150 *
Darren Hilla2ae6572011-09-01 07:36:26 -0400151 * @access public
152 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400153 */
154 public function sess_save()
155 {
156 // Nothing to do - changes to $_SESSION are automatically saved
157 }
158
159 /**
160 * Destroy the current session
161 *
Darren Hilla2ae6572011-09-01 07:36:26 -0400162 * @access public
163 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400164 */
165 public function sess_destroy()
166 {
167 // Cleanup session
168 $_SESSION = array();
169 $name = session_name();
170 if (isset($_COOKIE[$name]))
171 {
172 // Clear session cookie
173 $params = session_get_cookie_params();
174 setcookie($name, '', time() - 42000, $params['path'], $params['domain']);
175 unset($_COOKIE[$name]);
176 }
177 session_destroy();
178 }
179
180 /**
181 * Regenerate the current session
182 *
183 * Regenerate the session id
184 *
Darren Hilla2ae6572011-09-01 07:36:26 -0400185 * @access public
dchill4277ee3fd2012-07-24 11:50:01 -0400186 * @param boolean Destroy session data flag (default: FALSE)
Darren Hilla2ae6572011-09-01 07:36:26 -0400187 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400188 */
dchill4277ee3fd2012-07-24 11:50:01 -0400189 public function sess_regenerate($destroy = FALSE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400190 {
191 // Just regenerate id, passing destroy flag
192 session_regenerate_id($destroy);
dchill42f79afb52012-08-08 12:03:46 -0400193 $_SESSION['session_id'] = session_id();
Darren Hillc4e266b2011-08-30 15:40:27 -0400194 }
195
196 /**
197 * Get a reference to user data array
198 *
Darren Hilla2ae6572011-09-01 07:36:26 -0400199 * @access public
200 * @return array Reference to userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400201 */
202 public function &get_userdata()
203 {
204 // Just return reference to $_SESSION
205 return $_SESSION;
206 }
207}
Darren Hillc4e266b2011-08-30 15:40:27 -0400208
209/* End of file Session_native.php */
Darren Hill5073a372011-08-31 13:54:19 -0400210/* Location: ./system/libraries/Session/drivers/Session_native.php */