Skip to content
This repository

Update system/libraries/Session/drivers/Session_native.php #1859

Closed
wants to merge 1 commit into from

3 participants

Lucas Pinheiro Darren Hill Andrey Andreev
Lucas Pinheiro

Correction native Session.
Not saved in Internet Explorer

Lucas Pinheiro Update system/libraries/Session/drivers/Session_native.php
Correction native Session.
Not saved in Internet Explorer
d28b33f
Darren Hill

What line(s) required changes? What makes a difference for the Session cookie in IE?

Andrey Andreev narfbg closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Oct 08, 2012
Lucas Pinheiro Update system/libraries/Session/drivers/Session_native.php
Correction native Session.
Not saved in Internet Explorer
d28b33f
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 204 additions and 190 deletions. Show diff stats Hide diff stats

  1. +204 190 system/libraries/Session/drivers/Session_native.php
394 system/libraries/Session/drivers/Session_native.php
... ... @@ -1,4 +1,7 @@
1   -<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  1 +<?php
  2 +
  3 +if (!defined('BASEPATH'))
  4 + exit('No direct script access allowed');
2 5 /**
3 6 * CodeIgniter
4 7 *
@@ -28,7 +31,7 @@
28 31 /**
29 32 * Native PHP session management driver
30 33 *
31   - * This is the driver that uses the native PHP $_SESSION array through the Session driver library.
  34 + * This is the driver that uses the native PHP $_SESSION[$this->alias] array through the Session driver library.
32 35 *
33 36 * @package CodeIgniter
34 37 * @subpackage Libraries
@@ -37,194 +40,205 @@
37 40 */
38 41 class CI_Session_native extends CI_Session_driver {
39 42
40   - /**
41   - * Initialize session driver object
42   - *
43   - * @return void
44   - */
45   - protected function initialize()
46   - {
47   - // Get config parameters
48   - $config = array();
49   - $CI =& get_instance();
50   - $prefs = array(
51   - 'sess_cookie_name',
52   - 'sess_expire_on_close',
53   - 'sess_expiration',
54   - 'sess_match_ip',
55   - 'sess_match_useragent',
56   - 'sess_time_to_update',
57   - 'cookie_prefix',
58   - 'cookie_path',
59   - 'cookie_domain'
60   - );
61   -
62   - foreach ($prefs as $key)
63   - {
64   - $config[$key] = isset($this->_parent->params[$key])
65   - ? $this->_parent->params[$key]
66   - : $CI->config->item($key);
67   - }
68   -
69   - // Set session name, if specified
70   - if ($config['sess_cookie_name'])
71   - {
72   - // Differentiate name from cookie driver with '_id' suffix
73   - $name = $config['sess_cookie_name'].'_id';
74   - if ($config['cookie_prefix'])
75   - {
76   - // Prepend cookie prefix
77   - $name = $config['cookie_prefix'].$name;
78   - }
79   - session_name($name);
80   - }
81   -
82   - // Set expiration, path, and domain
83   - $expire = 7200;
84   - $path = '/';
85   - $domain = '';
86   - if ($config['sess_expiration'] !== FALSE)
87   - {
88   - // Default to 2 years if expiration is "0"
89   - $expire = ($config['sess_expiration'] == 0) ? (60*60*24*365*2) : $config['sess_expiration'];
90   - }
91   -
92   - if ($config['cookie_path'])
93   - {
94   - // Use specified path
95   - $path = $config['cookie_path'];
96   - }
97   -
98   - if ($config['cookie_domain'])
99   - {
100   - // Use specified domain
101   - $domain = $config['cookie_domain'];
102   - }
103   - session_set_cookie_params($config['sess_expire_on_close'] ? 0 : $expire, $path, $domain);
104   -
105   - // Start session
106   - session_start();
107   -
108   - // Check session expiration, ip, and agent
109   - $now = time();
110   - $destroy = FALSE;
111   - if (isset($_SESSION['last_activity']) && ($_SESSION['last_activity'] + $expire) < $now)
112   - {
113   - // Expired - destroy
114   - $destroy = TRUE;
115   - }
116   - elseif ($config['sess_match_ip'] === TRUE && isset($_SESSION['ip_address'])
117   - && $_SESSION['ip_address'] !== $CI->input->ip_address())
118   - {
119   - // IP doesn't match - destroy
120   - $destroy = TRUE;
121   - }
122   - elseif ($config['sess_match_useragent'] === TRUE && isset($_SESSION['user_agent'])
123   - && $_SESSION['user_agent'] !== trim(substr($CI->input->user_agent(), 0, 50)))
124   - {
125   - // Agent doesn't match - destroy
126   - $destroy = TRUE;
127   - }
128   -
129   - // Destroy expired or invalid session
130   - if ($destroy)
131   - {
132   - // Clear old session and start new
133   - $this->sess_destroy();
134   - session_start();
135   - }
136   -
137   - // Check for update time
138   - if ($config['sess_time_to_update'] && isset($_SESSION['last_activity'])
139   - && ($_SESSION['last_activity'] + $config['sess_time_to_update']) < $now)
140   - {
141   - // Regenerate ID, but don't destroy session
142   - $this->sess_regenerate(FALSE);
143   - }
144   -
145   - // Set activity time
146   - $_SESSION['last_activity'] = $now;
147   -
148   - // Set matching values as required
149   - if ($config['sess_match_ip'] === TRUE && ! isset($_SESSION['ip_address']))
150   - {
151   - // Store user IP address
152   - $_SESSION['ip_address'] = $CI->input->ip_address();
153   - }
154   -
155   - if ($config['sess_match_useragent'] === TRUE && ! isset($_SESSION['user_agent']))
156   - {
157   - // Store user agent string
158   - $_SESSION['user_agent'] = trim(substr($CI->input->user_agent(), 0, 50));
159   - }
160   -
161   - // Make session ID available
162   - $_SESSION['session_id'] = session_id();
163   - }
164   -
165   - // ------------------------------------------------------------------------
166   -
167   - /**
168   - * Save the session data
169   - *
170   - * @return void
171   - */
172   - public function sess_save()
173   - {
174   - // Nothing to do - changes to $_SESSION are automatically saved
175   - }
176   -
177   - // ------------------------------------------------------------------------
178   -
179   - /**
180   - * Destroy the current session
181   - *
182   - * @return void
183   - */
184   - public function sess_destroy()
185   - {
186   - // Cleanup session
187   - $_SESSION = array();
188   - $name = session_name();
189   - if (isset($_COOKIE[$name]))
190   - {
191   - // Clear session cookie
192   - $params = session_get_cookie_params();
193   - setcookie($name, '', time() - 42000, $params['path'], $params['domain']);
194   - unset($_COOKIE[$name]);
195   - }
196   - session_destroy();
197   - }
198   -
199   - // ------------------------------------------------------------------------
200   -
201   - /**
202   - * Regenerate the current session
203   - *
204   - * Regenerate the session id
205   - *
206   - * @param bool Destroy session data flag (default: FALSE)
207   - * @return void
208   - */
209   - public function sess_regenerate($destroy = FALSE)
210   - {
211   - // Just regenerate id, passing destroy flag
212   - session_regenerate_id($destroy);
213   - $_SESSION['session_id'] = session_id();
214   - }
215   -
216   - // ------------------------------------------------------------------------
217   -
218   - /**
219   - * Get a reference to user data array
220   - *
221   - * @return array Reference to userdata
222   - */
223   - public function &get_userdata()
224   - {
225   - // Just return reference to $_SESSION
226   - return $_SESSION;
227   - }
  43 + private $ci = null;
  44 + private $alias = null;
  45 +
  46 + /**
  47 + * Initialize session driver object
  48 + *
  49 + * @return void
  50 + */
  51 + public function __construct()
  52 + {
  53 + $this->ci = &get_instance();
  54 + //Nickname of session to avoid conflict on other sites
  55 + $this->alias = $this->ci->config->item('sess_cookie_name');
  56 + //checks if exite session start session
  57 + if (!isset($_SESSION[$this->alias]))
  58 + {
  59 + session_start();
  60 + }
  61 + }
  62 +
  63 + protected function initialize()
  64 + {
  65 + // Get config parameters
  66 + $config = array();
  67 + $prefs = array(
  68 + 'sess_cookie_name',
  69 + 'sess_expire_on_close',
  70 + 'sess_expiration',
  71 + 'sess_match_ip',
  72 + 'sess_match_useragent',
  73 + 'sess_time_to_update',
  74 + 'cookie_prefix',
  75 + 'cookie_path',
  76 + 'cookie_domain'
  77 + );
  78 +
  79 + foreach ($prefs as $key)
  80 + {
  81 + $config[$key] = isset($this->_parent->params[$key]) ? $this->_parent->params[$key] : $this->ci->config->item($key);
  82 + }
  83 +
  84 + // Set session name, if specified
  85 + if ($config['sess_cookie_name'])
  86 + {
  87 + // Differentiate name from cookie driver with '_id' suffix
  88 + $name = $config['sess_cookie_name'] . '_id';
  89 + if ($config['cookie_prefix'])
  90 + {
  91 + // Prepend cookie prefix
  92 + $name = $config['cookie_prefix'] . $name;
  93 + }
  94 + session_name($name);
  95 + }
  96 +
  97 + // Set expiration, path, and domain
  98 + $expire = 7200;
  99 + $path = '/';
  100 + $domain = '';
  101 + if ($config['sess_expiration'] !== FALSE)
  102 + {
  103 + // Default to 2 years if expiration is "0"
  104 + $expire = ($config['sess_expiration'] == 0) ? (60 * 60 * 24 * 365 * 2) : $config['sess_expiration'];
  105 + }
  106 +
  107 + if ($config['cookie_path'])
  108 + {
  109 + // Use specified path
  110 + $path = $config['cookie_path'];
  111 + }
  112 +
  113 + if ($config['cookie_domain'])
  114 + {
  115 + // Use specified domain
  116 + $domain = $config['cookie_domain'];
  117 + }
  118 + session_set_cookie_params($config['sess_expire_on_close'] ? 0 : $expire, $path, $domain);
  119 +
  120 + // Start session
  121 + //session_start();
  122 +
  123 + // Check session expiration, ip, and agent
  124 + $now = time();
  125 + $destroy = FALSE;
  126 + if (isset($_SESSION[$this->alias]['last_activity']) && ($_SESSION[$this->alias]['last_activity'] + $expire) < $now)
  127 + {
  128 + // Expired - destroy
  129 + $destroy = TRUE;
  130 + } elseif ($config['sess_match_ip'] === TRUE && isset($_SESSION[$this->alias]['ip_address'])
  131 + && $_SESSION[$this->alias]['ip_address'] !== $this->ci->input->ip_address())
  132 + {
  133 + // IP doesn't match - destroy
  134 + $destroy = TRUE;
  135 + } elseif ($config['sess_match_useragent'] === TRUE && isset($_SESSION[$this->alias]['user_agent'])
  136 + && $_SESSION[$this->alias]['user_agent'] !== trim($this->ci->input->user_agent()))
  137 + {
  138 + // Agent doesn't match - destroy
  139 + $destroy = TRUE;
  140 + }
  141 +
  142 + // Destroy expired or invalid session
  143 + if ($destroy)
  144 + {
  145 + // Clear old session and start new
  146 + $this->sess_destroy();
  147 + session_start();
  148 + }
  149 +
  150 + // Check for update time
  151 + if ($config['sess_time_to_update'] && isset($_SESSION[$this->alias]['last_activity'])
  152 + && ($_SESSION[$this->alias]['last_activity'] + $config['sess_time_to_update']) < $now)
  153 + {
  154 + // Regenerate ID, but don't destroy session
  155 + $this->sess_regenerate(FALSE);
  156 + }
  157 +
  158 + // Set activity time
  159 + $_SESSION[$this->alias]['last_activity'] = $now;
  160 +
  161 + // Set matching values as required
  162 + if ($config['sess_match_ip'] === TRUE && !isset($_SESSION[$this->alias]['ip_address']))
  163 + {
  164 + // Store user IP address
  165 + $_SESSION[$this->alias]['ip_address'] = $this->ci->input->ip_address();
  166 + }
  167 +
  168 + if ($config['sess_match_useragent'] === TRUE && !isset($_SESSION[$this->alias]['user_agent']))
  169 + {
  170 + // Store user agent string
  171 + $_SESSION[$this->alias]['user_agent'] = trim($this->ci->input->user_agent());
  172 + }
  173 +
  174 + // Make session ID available
  175 + $_SESSION[$this->alias]['session_id'] = session_id();
  176 + }
  177 +
  178 + // ------------------------------------------------------------------------
  179 +
  180 + /**
  181 + * Save the session data
  182 + *
  183 + * @return void
  184 + */
  185 + public function sess_save()
  186 + {
  187 + $_SESSION[$this->alias] = $this->all_userdata();
  188 + // Nothing to do - changes to $_SESSION[$this->alias] are automatically saved
  189 + }
  190 +
  191 + // ------------------------------------------------------------------------
  192 +
  193 + /**
  194 + * Destroy the current session
  195 + *
  196 + * @return void
  197 + */
  198 + public function sess_destroy()
  199 + {
  200 + // Cleanup session
  201 + $_SESSION[$this->alias] = array();
  202 + $name = session_name();
  203 + if (isset($_COOKIE[$name]))
  204 + {
  205 + // Clear session cookie
  206 + $params = session_get_cookie_params();
  207 + setcookie($name, '', time() - 42000, $params['path'], $params['domain']);
  208 + unset($_COOKIE[$name]);
  209 + }
  210 + session_destroy();
  211 + }
  212 +
  213 + // ------------------------------------------------------------------------
  214 +
  215 + /**
  216 + * Regenerate the current session
  217 + *
  218 + * Regenerate the session id
  219 + *
  220 + * @param bool Destroy session data flag (default: FALSE)
  221 + * @return void
  222 + */
  223 + public function sess_regenerate($destroy = FALSE)
  224 + {
  225 + // Just regenerate id, passing destroy flag
  226 + session_regenerate_id($destroy);
  227 + $_SESSION[$this->alias]['session_id'] = session_id();
  228 + }
  229 +
  230 + // ------------------------------------------------------------------------
  231 +
  232 + /**
  233 + * Get a reference to user data array
  234 + *
  235 + * @return array Reference to userdata
  236 + */
  237 + public function &get_userdata()
  238 + {
  239 + // Just return reference to $_SESSION[$this->alias]
  240 + return $_SESSION[$this->alias];
  241 + }
228 242
229 243 }
230 244

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.