blob: 356deb4dcbe444eaf04c3d16133c5295af9ba21c [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',
45 'cookie_prefix',
46 'cookie_path',
47 'cookie_domain'
48 );
49 foreach ($prefs as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -040050 {
dchill4226429202012-07-31 10:55:07 -040051 $config[$key] = isset($this->_parent->params[$key]) ? $this->_parent->params[$key] :
52 $CI->config->item($key);
Darren Hillc4e266b2011-08-30 15:40:27 -040053 }
54
55 // Set session name, if specified
56 if ($config['sess_cookie_name'])
57 {
58 $name = $config['sess_cookie_name'];
59 if ($config['cookie_prefix'])
60 {
61 // Prepend cookie prefix
62 $name = $config['cookie_prefix'].$name;
63 }
64 session_name($name);
65 }
66
67 // Set expiration, path, and domain
68 $expire = 7200;
69 $path = '/';
70 $domain = '';
71 if ($config['sess_expiration'] !== FALSE)
72 {
73 // Default to 2 years if expiration is "0"
74 $expire = ($config['sess_expiration'] == 0) ? (60*60*24*365*2) : $config['sess_expiration'];
75 }
76 if ($config['cookie_path'])
77 {
78 // Use specified path
79 $path = $config['cookie_path'];
80 }
81 if ($config['cookie_domain'])
82 {
83 // Use specified domain
84 $domain = $config['cookie_domain'];
85 }
86 session_set_cookie_params($config['sess_expire_on_close'] ? 0 : $expire, $path, $domain);
87
88 // Start session
89 session_start();
90
91 // Check session expiration, ip, and agent
92 $now = time();
93 $destroy = FALSE;
94 if (isset($_SESSION['last_activity']) && ($_SESSION['last_activity'] + $expire) < $now)
95 {
96 // Expired - destroy
97 $destroy = TRUE;
98 }
99 else if ($config['sess_match_ip'] == TRUE && isset($_SESSION['ip_address']) &&
100 $_SESSION['ip_address'] != $CI->input->ip_address())
101 {
102 // IP doesn't match - destroy
103 $destroy = TRUE;
104 }
105 else if ($config['sess_match_useragent'] == TRUE && isset($_SESSION['user_agent']) &&
106 $_SESSION['user_agent'] != trim(substr($CI->input->user_agent(), 0, 50)))
107 {
108 // Agent doesn't match - destroy
109 $destroy = TRUE;
110 }
111
112 // Destroy expired or invalid session
113 if ($destroy)
114 {
115 // Clear old session and start new
116 $this->sess_destroy();
117 session_start();
118 }
119
120 // Set activity time
121 $_SESSION['last_activity'] = $now;
122
123 // Set matching values as required
124 if ($config['sess_match_ip'] == TRUE && !isset($_SESSION['ip_address']))
125 {
126 // Store user IP address
127 $_SESSION['ip_address'] = $CI->input->ip_address();
128 }
129 if ($config['sess_match_useragent'] == TRUE && !isset($_SESSION['user_agent']))
130 {
131 // Store user agent string
132 $_SESSION['user_agent'] = trim(substr($CI->input->user_agent(), 0, 50));
133 }
134 }
135
136 /**
137 * Save the session data
138 *
Darren Hilla2ae6572011-09-01 07:36:26 -0400139 * @access public
140 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400141 */
142 public function sess_save()
143 {
144 // Nothing to do - changes to $_SESSION are automatically saved
145 }
146
147 /**
148 * Destroy the current session
149 *
Darren Hilla2ae6572011-09-01 07:36:26 -0400150 * @access public
151 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400152 */
153 public function sess_destroy()
154 {
155 // Cleanup session
156 $_SESSION = array();
157 $name = session_name();
158 if (isset($_COOKIE[$name]))
159 {
160 // Clear session cookie
161 $params = session_get_cookie_params();
162 setcookie($name, '', time() - 42000, $params['path'], $params['domain']);
163 unset($_COOKIE[$name]);
164 }
165 session_destroy();
166 }
167
168 /**
169 * Regenerate the current session
170 *
171 * Regenerate the session id
172 *
Darren Hilla2ae6572011-09-01 07:36:26 -0400173 * @access public
dchill4277ee3fd2012-07-24 11:50:01 -0400174 * @param boolean Destroy session data flag (default: FALSE)
Darren Hilla2ae6572011-09-01 07:36:26 -0400175 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400176 */
dchill4277ee3fd2012-07-24 11:50:01 -0400177 public function sess_regenerate($destroy = FALSE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400178 {
179 // Just regenerate id, passing destroy flag
180 session_regenerate_id($destroy);
181 }
182
183 /**
184 * Get a reference to user data array
185 *
Darren Hilla2ae6572011-09-01 07:36:26 -0400186 * @access public
187 * @return array Reference to userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400188 */
189 public function &get_userdata()
190 {
191 // Just return reference to $_SESSION
192 return $_SESSION;
193 }
194}
Darren Hillc4e266b2011-08-30 15:40:27 -0400195
196/* End of file Session_native.php */
Darren Hill5073a372011-08-31 13:54:19 -0400197/* Location: ./system/libraries/Session/drivers/Session_native.php */