blob: 37da3445ac3c8f08eede21e8ee345b16b3bccf8c [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
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 2.0
13 * @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 Hill5073a372011-08-31 13:54:19 -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 *
31 * @access protected
32 * @return void
33 */
34 protected function initialize()
35 {
36 // Get config parameters
37 $config = array();
38 $CI =& get_instance();
39 foreach (array('sess_cookie_name', 'sess_expire_on_close', 'sess_expiration', 'sess_match_ip',
40 'sess_match_useragent', 'cookie_prefix', 'cookie_path', 'cookie_domain') as $key)
41 {
42 $config[$key] = isset($this->parent->params[$key]) ? $this->parent->params[$key] : $CI->config->item($key);
43 }
44
45 // Set session name, if specified
46 if ($config['sess_cookie_name'])
47 {
48 $name = $config['sess_cookie_name'];
49 if ($config['cookie_prefix'])
50 {
51 // Prepend cookie prefix
52 $name = $config['cookie_prefix'].$name;
53 }
54 session_name($name);
55 }
56
57 // Set expiration, path, and domain
58 $expire = 7200;
59 $path = '/';
60 $domain = '';
61 if ($config['sess_expiration'] !== FALSE)
62 {
63 // Default to 2 years if expiration is "0"
64 $expire = ($config['sess_expiration'] == 0) ? (60*60*24*365*2) : $config['sess_expiration'];
65 }
66 if ($config['cookie_path'])
67 {
68 // Use specified path
69 $path = $config['cookie_path'];
70 }
71 if ($config['cookie_domain'])
72 {
73 // Use specified domain
74 $domain = $config['cookie_domain'];
75 }
76 session_set_cookie_params($config['sess_expire_on_close'] ? 0 : $expire, $path, $domain);
77
78 // Start session
79 session_start();
80
81 // Check session expiration, ip, and agent
82 $now = time();
83 $destroy = FALSE;
84 if (isset($_SESSION['last_activity']) && ($_SESSION['last_activity'] + $expire) < $now)
85 {
86 // Expired - destroy
87 $destroy = TRUE;
88 }
89 else if ($config['sess_match_ip'] == TRUE && isset($_SESSION['ip_address']) &&
90 $_SESSION['ip_address'] != $CI->input->ip_address())
91 {
92 // IP doesn't match - destroy
93 $destroy = TRUE;
94 }
95 else if ($config['sess_match_useragent'] == TRUE && isset($_SESSION['user_agent']) &&
96 $_SESSION['user_agent'] != trim(substr($CI->input->user_agent(), 0, 50)))
97 {
98 // Agent doesn't match - destroy
99 $destroy = TRUE;
100 }
101
102 // Destroy expired or invalid session
103 if ($destroy)
104 {
105 // Clear old session and start new
106 $this->sess_destroy();
107 session_start();
108 }
109
110 // Set activity time
111 $_SESSION['last_activity'] = $now;
112
113 // Set matching values as required
114 if ($config['sess_match_ip'] == TRUE && !isset($_SESSION['ip_address']))
115 {
116 // Store user IP address
117 $_SESSION['ip_address'] = $CI->input->ip_address();
118 }
119 if ($config['sess_match_useragent'] == TRUE && !isset($_SESSION['user_agent']))
120 {
121 // Store user agent string
122 $_SESSION['user_agent'] = trim(substr($CI->input->user_agent(), 0, 50));
123 }
124 }
125
126 /**
127 * Save the session data
128 *
129 * @access public
130 * @return void
131 */
132 public function sess_save()
133 {
134 // Nothing to do - changes to $_SESSION are automatically saved
135 }
136
137 /**
138 * Destroy the current session
139 *
140 * @access public
141 * @return void
142 */
143 public function sess_destroy()
144 {
145 // Cleanup session
146 $_SESSION = array();
147 $name = session_name();
148 if (isset($_COOKIE[$name]))
149 {
150 // Clear session cookie
151 $params = session_get_cookie_params();
152 setcookie($name, '', time() - 42000, $params['path'], $params['domain']);
153 unset($_COOKIE[$name]);
154 }
155 session_destroy();
156 }
157
158 /**
159 * Regenerate the current session
160 *
161 * Regenerate the session id
162 *
163 * @access public
164 * @param boolean Destroy session data flag (default: false)
165 * @return void
166 */
167 public function sess_regenerate($destroy = false)
168 {
169 // Just regenerate id, passing destroy flag
170 session_regenerate_id($destroy);
171 }
172
173 /**
174 * Get a reference to user data array
175 *
176 * @access public
177 * @return array Reference to userdata
178 */
179 public function &get_userdata()
180 {
181 // Just return reference to $_SESSION
182 return $_SESSION;
183 }
184}
Darren Hill5073a372011-08-31 13:54:19 -0400185// END CI_Session_native Class
Darren Hillc4e266b2011-08-30 15:40:27 -0400186
187
188/* End of file Session_native.php */
Darren Hill5073a372011-08-31 13:54:19 -0400189/* Location: ./system/libraries/Session/drivers/Session_native.php */
Darren Hillc4e266b2011-08-30 15:40:27 -0400190?>