blob: 8ba8e749a16e2b7f4714153643e0cfd63eb8995c [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 {
dchill42aee92652012-08-26 21:45:35 -040059 // Differentiate name from cookie driver with '_id' suffix
60 $name = $config['sess_cookie_name'].'_id';
Darren Hillc4e266b2011-08-30 15:40:27 -040061 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
dchill42f79afb52012-08-08 12:03:46 -0400122 // 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 Hillc4e266b2011-08-30 15:40:27 -0400130 // 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 }
dchill42f79afb52012-08-08 12:03:46 -0400144
145 // Make session ID available
146 $_SESSION['session_id'] = session_id();
Darren Hillc4e266b2011-08-30 15:40:27 -0400147 }
148
149 /**
150 * Save the session data
151 *
Darren Hilla2ae6572011-09-01 07:36:26 -0400152 * @access public
153 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400154 */
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 Hilla2ae6572011-09-01 07:36:26 -0400163 * @access public
164 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400165 */
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 Hilla2ae6572011-09-01 07:36:26 -0400186 * @access public
dchill4277ee3fd2012-07-24 11:50:01 -0400187 * @param boolean Destroy session data flag (default: FALSE)
Darren Hilla2ae6572011-09-01 07:36:26 -0400188 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400189 */
dchill4277ee3fd2012-07-24 11:50:01 -0400190 public function sess_regenerate($destroy = FALSE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400191 {
192 // Just regenerate id, passing destroy flag
193 session_regenerate_id($destroy);
dchill42f79afb52012-08-08 12:03:46 -0400194 $_SESSION['session_id'] = session_id();
Darren Hillc4e266b2011-08-30 15:40:27 -0400195 }
196
197 /**
198 * Get a reference to user data array
199 *
Darren Hilla2ae6572011-09-01 07:36:26 -0400200 * @access public
201 * @return array Reference to userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400202 */
203 public function &get_userdata()
204 {
205 // Just return reference to $_SESSION
206 return $_SESSION;
207 }
208}
Darren Hillc4e266b2011-08-30 15:40:27 -0400209
210/* End of file Session_native.php */
Darren Hill5073a372011-08-31 13:54:19 -0400211/* Location: ./system/libraries/Session/drivers/Session_native.php */