blob: 7908badf8d58880d4c4dfc42ad221e5041f27bbc [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Darren Hillc4e266b2011-08-30 15:40:27 -04002/**
3 * CodeIgniter
4 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +03005 * An open source application development framework for PHP 5.2.4 or newer
6 *
7 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
Darren Hillc4e266b2011-08-30 15:40:27 -040018 *
19 * @package CodeIgniter
Andrey Andreev9ffcee62012-09-05 16:25:16 +030020 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreev9ffcee62012-09-05 16:25:16 +030022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Darren Hillc4e266b2011-08-30 15:40:27 -040023 * @link http://codeigniter.com
24 * @since Version 2.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Darren Hillc4e266b2011-08-30 15:40:27 -040028
Darren Hillc4e266b2011-08-30 15:40:27 -040029/**
Andrey Andreev9ffcee62012-09-05 16:25:16 +030030 * CodeIgniter Session Class
Darren Hillc4e266b2011-08-30 15:40:27 -040031 *
Darren Hillc4e266b2011-08-30 15:40:27 -040032 * @package CodeIgniter
33 * @subpackage Libraries
34 * @category Sessions
Andrey Andreev47a47fb2014-05-31 16:08:30 +030035 * @author Andrey Andreev
Darren Hillc4e266b2011-08-30 15:40:27 -040036 * @link http://codeigniter.com/user_guide/libraries/sessions.html
37 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +030038class CI_Session {
Andrey Andreev9ffcee62012-09-05 16:25:16 +030039
Andrey Andreev47a47fb2014-05-31 16:08:30 +030040 protected $_driver = 'files';
Andrey Andreevdfb39be2014-10-06 01:50:14 +030041 protected $_config;
Darren Hillc4e266b2011-08-30 15:40:27 -040042
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020043 // ------------------------------------------------------------------------
44
Darren Hillc4e266b2011-08-30 15:40:27 -040045 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +030046 * Class constructor
Darren Hillc4e266b2011-08-30 15:40:27 -040047 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +030048 * @param array $params Configuration parameters
Andrey Andreev2e3e2302012-10-09 15:52:34 +030049 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -040050 */
51 public function __construct(array $params = array())
52 {
Andrey Andreev2e3e2302012-10-09 15:52:34 +030053 // No sessions under CLI
Andrey Andreevf964b162013-11-12 17:04:55 +020054 if (is_cli())
Andrey Andreev2e3e2302012-10-09 15:52:34 +030055 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +030056 log_message('debug', 'Session: Initialization under CLI aborted.');
57 return;
58 }
59 elseif ((bool) ini_get('session.auto_start'))
60 {
61 log_message('error', 'Session: session.auto_start is enabled in php.ini. Aborting.');
62 return;
63 }
64 elseif ( ! empty($params['driver']))
65 {
66 $this->_driver = $params['driver'];
67 unset($params['driver']);
68 }
Andrey Andreevdfb39be2014-10-06 01:50:14 +030069 elseif ($driver = config_item('sess_driver'))
Andrey Andreev34b1ef52014-05-31 21:23:41 +030070 {
71 $this->_driver = $driver;
72 }
Andrey Andreevac4f4722014-06-02 11:16:32 +030073 // Note: BC workaround
74 elseif (config_item('sess_use_database'))
75 {
76 $this->_driver = 'database';
77 }
Andrey Andreev47a47fb2014-05-31 16:08:30 +030078
79 if (($class = $this->_ci_load_classes($this->_driver)) === FALSE)
80 {
Andrey Andreev2e3e2302012-10-09 15:52:34 +030081 return;
82 }
83
Andrey Andreevdfb39be2014-10-06 01:50:14 +030084 // Configuration ...
85 $this->_configure($params);
86
87 $class = new $class($this->_config);
Andrey Andreev47a47fb2014-05-31 16:08:30 +030088 if ($class instanceof SessionHandlerInterface)
Darren Hillc4e266b2011-08-30 15:40:27 -040089 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +030090 if (is_php('5.4'))
Darren Hillc4e266b2011-08-30 15:40:27 -040091 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +030092 session_set_save_handler($class, TRUE);
Darren Hillc4e266b2011-08-30 15:40:27 -040093 }
94 else
95 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +030096 session_set_save_handler(
97 array($class, 'open'),
98 array($class, 'close'),
99 array($class, 'read'),
100 array($class, 'write'),
101 array($class, 'destroy'),
102 array($class, 'gc')
103 );
104
105 register_shutdown_function('session_write_close');
106 }
107 }
108 else
109 {
110 log_message('error', "Session: Driver '".$this->_driver."' doesn't implement SessionHandlerInterface. Aborting.");
111 return;
112 }
113
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300114 // Work-around for PHP bug #66827 (https://bugs.php.net/bug.php?id=66827)
115 //
116 // The session ID sanitizer doesn't check for the value type and blindly does
117 // an implicit cast to string, which triggers an 'Array to string' E_NOTICE.
118 if (isset($_COOKIE[$this->_cookie_name]) && ! is_string($_COOKIE[$this->_cookie_name]))
119 {
120 unset($_COOKIE[$this->_cookie_name]);
121 }
122
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300123 session_start();
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300124
125 // Another work-around ... PHP doesn't seem to send the session cookie
126 // unless it is being currently created or regenerated
127 if (isset($_COOKIE[$this->_config['cookie_name']]) && $_COOKIE[$this->_config['cookie_name']] === session_id())
128 {
129 setcookie(
130 $this->_config['cookie_name'],
131 session_id(),
132 (empty($this->_config['cookie_lifetime']) ? 0 : time() + $this->_config['cookie_lifetime']),
133 $this->_config['cookie_path'],
134 $this->_config['cookie_domain'],
135 $this->_config['cookie_secure'],
136 TRUE
137 );
138 }
139
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300140 $this->_ci_init_vars();
141
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300142/*
143 Need to test if this is necessary for a custom driver or if it's only
144 relevant to PHP's own files handler.
145
146 https://bugs.php.net/bug.php?id=65475
147 do this after session is started:
148 if (is_php('5.5.2') && ! is_php('5.5.4'))
149 {
150 $session_id = session_id();
151 if ($_COOKIE[$this->_cookie_name] !== $session_id && file_exists(teh file))
152 {
153 unlink(<teh file>);
154 }
155 }
156*/
157
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300158 log_message('debug', "Session: Class initialized using '".$this->_driver."' driver.");
159 }
160
161 // ------------------------------------------------------------------------
162
163 protected function _ci_load_classes($driver)
164 {
165 // PHP 5.4 compatibility
166 interface_exists('SessionHandlerInterface', FALSE) OR require_once(BASEPATH.'libraries/Session/SessionHandlerInterface.php');
167
168 $prefix = config_item('subclass_prefix');
169
170 if ( ! class_exists('CI_Session_driver', FALSE))
171 {
Andrey Andreeve86603f2014-06-11 14:03:36 +0300172 require_once(
173 file_exists(APPPATH.'libraries/Session/Session_driver.php')
174 ? APPPATH.'libraries/Session/Session_driver.php'
175 : BASEPATH.'libraries/Session/Session_driver.php'
176 );
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300177
178 if (file_exists($file_path = APPPATH.'libraries/Session/'.$prefix.'Session_driver.php'))
179 {
180 require_once($file_path);
181 }
182 }
183
184 $class = 'Session_'.$driver.'_driver';
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300185 if ( ! class_exists('CI_'.$class, FALSE))
186 {
187 if (file_exists($file_path = APPPATH.'libraries/Session/drivers/'.$class.'.php') OR file_exists($file_path = BASEPATH.'libraries/Session/drivers/'.$class.'.php'))
188 {
189 require_once($file_path);
190 }
191
192 if ( ! class_exists('CI_'.$class, FALSE))
193 {
194 log_message('error', "Session: Configured driver '".$driver."' was not found. Aborting.");
195 return FALSE;
196 }
197 }
198
199 if ( ! class_exists($prefix.$class) && file_exists($file_path = APPPATH.'libraries/Session/drivers/'.$prefix.$class.'.php'))
200 {
201 require_once($file_path);
202 if (class_exists($prefix.$class, FALSE))
203 {
204 return $prefix.$class;
205 }
206 else
207 {
208 log_message('debug', 'Session: '.$prefix.$class.".php found but it doesn't declare class ".$prefix.$class.'.');
209 }
210 }
211
212 return 'CI_'.$class;
213 }
214
215 // ------------------------------------------------------------------------
216
217 /**
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300218 * Configuration
219 *
220 * Handle input parameters and configuration defaults
221 *
222 * @param array &$params Input parameters
223 * @return void
224 */
225 protected function _configure(&$params)
226 {
227 $expiration = config_item('sess_expiration');
228
229 if (isset($params['cookie_lifetime']))
230 {
231 $params['cookie_lifetime'] = (int) $params['cookie_lifetime'];
232 }
233 else
234 {
235 $params['cookie_lifetime'] = ( ! isset($expiration) && config_item('sess_expire_on_close'))
236 ? 0 : (int) $expiration;
237 }
238
239 isset($params['cookie_name']) OR $params['cookie_name'] = config_item('sess_cookie_name');
240 if (empty($params['cookie_name']))
241 {
242 $params['cookie_name'] = ini_get('session.name');
243 }
244 else
245 {
246 ini_set('session.name', $params['cookie_name']);
247 }
248
249 isset($params['cookie_path']) OR $params['cookie_path'] = config_item('cookie_path');
Andrey Andreev41b546d2014-10-06 03:01:22 +0300250 isset($params['cookie_domain']) OR $params['cookie_domain'] = config_item('cookie_domain');
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300251 isset($params['cookie_secure']) OR $params['cookie_secure'] = (bool) config_item('cookie_secure');
252
253 session_set_cookie_params(
254 $params['cookie_lifetime'],
255 $params['cookie_path'],
256 $params['cookie_domain'],
257 $params['cookie_secure'],
258 TRUE // HttpOnly; Yes, this is intentional and not configurable for security reasons
259 );
260
261 if (empty($expiration))
262 {
263 $params['expiration'] = (int) ini_get('session.gc_maxlifetime');
264 }
265 else
266 {
267 $params['expiration'] = (int) $expiration;
268 ini_set('session.gc_maxlifetime', $expiration);
269 }
270
271 $params['match_ip'] = (bool) (isset($params['match_ip']) ? $params['match_ip'] : config_item('sess_match_ip'));
272
273 isset($params['save_path']) OR $params['save_path'] = config_item('sess_save_path');
274
275 $this->_config = $params;
276
277 // Security is king
278 ini_set('session.use_trans_id', 0);
279 ini_set('session.use_strict_mode', 1);
280 ini_set('session.use_cookies', 1);
281 ini_set('session.use_only_cookies', 1);
282 ini_set('session.hash_function', 1);
283 ini_set('session.hash_bits_per_character', 4);
284 }
285
286 // ------------------------------------------------------------------------
287
288 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300289 * Handle temporary variables
290 *
291 * Clears old "flash" data, marks the new one for deletion and handles
292 * "temp" data deletion.
293 *
294 * @return void
295 */
296 protected function _ci_init_vars()
297 {
298 if ( ! empty($_SESSION['__ci_vars']))
299 {
300 $current_time = time();
301
302 foreach ($_SESSION['__ci_vars'] as $key => &$value)
303 {
304 if ($value === 'new')
305 {
306 $_SESSION['__ci_vars'][$key] = 'old';
307 }
308 // Hacky, but 'old' will (implicitly) always be less than time() ;)
309 // DO NOT move this above the 'new' check!
310 elseif ($value < $current_time)
311 {
312 unset($_SESSION[$key], $_SESSION['__ci_vars'][$key]);
313 }
314 }
315
316 if (empty($_SESSION['__ci_vars']))
317 {
318 unset($_SESSION['__ci_vars']);
Darren Hillc4e266b2011-08-30 15:40:27 -0400319 }
320 }
321 }
322
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300323 // ------------------------------------------------------------------------
324
Darren Hillc4e266b2011-08-30 15:40:27 -0400325 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300326 * Mark as flash
327 *
328 * @param mixed $key Session data key(s)
329 * @return bool
330 */
331 public function mark_as_flash($key)
332 {
333 if (is_array($key))
334 {
335 for ($i = 0, $c = count($key); $i < $c; $i++)
336 {
337 if ( ! isset($_SESSION[$key[$i]]))
338 {
339 return FALSE;
340 }
341 }
342
343 $new = array_fill_keys($key, 'new');
344
345 $_SESSION['__ci_vars'] = isset($_SESSION['__ci_vars'])
346 ? array_merge($_SESSION['__ci_vars'], $new)
347 : $new;
348
349 return TRUE;
350 }
351
352 if ( ! isset($_SESSION[$key]))
353 {
354 return FALSE;
355 }
356
357 $_SESSION['__ci_vars'][$key] = 'new';
358 return TRUE;
359 }
360
361 // ------------------------------------------------------------------------
362
363 /**
364 * Get flash keys
365 *
366 * @return array
367 */
368 public function get_flash_keys()
369 {
370 if ( ! isset($_SESSION['__ci_vars']))
371 {
372 return array();
373 }
374
375 $keys = array();
376 foreach (array_keys($_SESSION['__ci_vars']) as $key)
377 {
378 is_int($_SESSION['__ci_vars'][$key]) OR $keys[] = $key;
379 }
380
381 return $keys;
382 }
383
384 // ------------------------------------------------------------------------
385
386 /**
387 * Unmark flash
388 *
389 * @param mixed $key Session data key(s)
390 * @return void
391 */
392 public function unmark_flash($key)
393 {
394 if (empty($_SESSION['__ci_vars']))
395 {
396 return;
397 }
398
399 is_array($key) OR $key = array($key);
400
401 foreach ($key as $k)
402 {
403 if (isset($_SESSION['__ci_vars'][$k]) && ! is_int($_SESSION['__ci_vars'][$k]))
404 {
405 unset($_SESSION['__ci_vars'][$k]);
406 }
407 }
408
409 if (empty($_SESSION['__ci_vars']))
410 {
411 unset($_SESSION['__ci_vars']);
412 }
413 }
414
415 // ------------------------------------------------------------------------
416
417 /**
418 * Mark as temp
419 *
420 * @param mixed $key Session data key(s)
421 * @param int $ttl Time-to-live in seconds
422 * @return bool
423 */
424 public function mark_as_temp($key, $ttl = 300)
425 {
426 $ttl += time();
427
428 if (is_array($key))
429 {
430 $temp = array();
431
432 foreach ($key as $k => $v)
433 {
434 // Do we have a key => ttl pair, or just a key?
435 if (is_int($k))
436 {
437 $k = $v;
438 $v = $ttl;
439 }
440 else
441 {
442 $v += time();
443 }
444
445 if ( ! isset($_SESSION[$k]))
446 {
447 return FALSE;
448 }
449
450 $temp[$k] = $ts;
451 }
452
453 $_SESSION['__ci_vars'] = isset($_SESSION['__ci_vars'])
454 ? array_merge($_SESSION['__ci_vars'], $temp)
455 : $temp;
456
457 return TRUE;
458 }
459
460 if ( ! isset($_SESSION[$key]))
461 {
462 return FALSE;
463 }
464
465 $_SESSION['__ci_vars'][$key] = $ttl;
466 return TRUE;
467 }
468
469 // ------------------------------------------------------------------------
470
471 /**
472 * Get temp keys
473 *
474 * @return array
475 */
476 public function get_temp_keys()
477 {
478 if ( ! isset($_SESSION['__ci_vars']))
479 {
480 return array();
481 }
482
483 $keys = array();
484 foreach (array_keys($_SESSION['__ci_vars']) as $key)
485 {
486 is_int($_SESSION['__ci_vars'][$key]) && $keys[] = $key;
487 }
488
489 return $keys;
490 }
491
492 // ------------------------------------------------------------------------
493
494 /**
495 * Unmark flash
496 *
497 * @param mixed $key Session data key(s)
498 * @return void
499 */
500 public function unmark_temp($key)
501 {
502 if (empty($_SESSION['__ci_vars']))
503 {
504 return;
505 }
506
507 is_array($key) OR $key = array($key);
508
509 foreach ($key as $k)
510 {
511 if (isset($_SESSION['__ci_vars'][$k]) && is_int($_SESSION['__ci_vars'][$k]))
512 {
513 unset($_SESSION['__ci_vars'][$k]);
514 }
515 }
516
517 if (empty($_SESSION['__ci_vars']))
518 {
519 unset($_SESSION['__ci_vars']);
520 }
521 }
522
523 // ------------------------------------------------------------------------
524
525 /**
526 * __get()
527 *
528 * @param string $key 'session_id' or a session data key
529 * @return mixed
530 */
531 public function __get($key)
532 {
533 // Note: Keep this order the same, just in case somebody wants to
534 // use 'session_id' as a session data key, for whatever reason
535 if (isset($_SESSION[$key]))
536 {
537 return $_SESSION[$key];
538 }
539 elseif ($key === 'session_id')
540 {
541 return session_id();
542 }
543
544 return NULL;
545 }
546
547 // ------------------------------------------------------------------------
548
549 /**
550 * __set()
551 *
552 * @param string $key Session data key
553 * @param mixed $value Session data value
554 * @return void
555 */
556 public function __set($key, $value)
557 {
558 $_SESSION[$key] = $value;
559 }
560
561 // ------------------------------------------------------------------------
562
563 /**
564 * Session destroy
565 *
566 * Legacy CI_Session compatibility method
Darren Hillc4e266b2011-08-30 15:40:27 -0400567 *
Darren Hill5073a372011-08-31 13:54:19 -0400568 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400569 */
570 public function sess_destroy()
571 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300572 session_destroy();
Darren Hillc4e266b2011-08-30 15:40:27 -0400573 }
574
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300575 // ------------------------------------------------------------------------
576
Darren Hillc4e266b2011-08-30 15:40:27 -0400577 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300578 * Session regenerate
Darren Hillc4e266b2011-08-30 15:40:27 -0400579 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300580 * Legacy CI_Session compatibility method
581 *
582 * @param bool $destroy Destroy old session data flag
Darren Hill5073a372011-08-31 13:54:19 -0400583 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400584 */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300585 public function sess_regenerate($destroy = FALSE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400586 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300587 session_regenerate_id($destroy);
Darren Hillc4e266b2011-08-30 15:40:27 -0400588 }
589
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300590 // ------------------------------------------------------------------------
591
Darren Hillc4e266b2011-08-30 15:40:27 -0400592 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300593 * Get userdata reference
Darren Hillc4e266b2011-08-30 15:40:27 -0400594 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300595 * Legacy CI_Session compatibility method
596 *
597 * @returns array
Darren Hillc4e266b2011-08-30 15:40:27 -0400598 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300599 public function &get_userdata()
Darren Hillc4e266b2011-08-30 15:40:27 -0400600 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300601 return $_SESSION;
602 }
603
604 // ------------------------------------------------------------------------
605
606 /**
607 * Userdata (fetch)
608 *
609 * Legacy CI_Session compatibility method
610 *
611 * @param string $key Session data key
612 * @return mixed Session data value or NULL if not found
613 */
614 public function userdata($key = NULL)
615 {
616 if (isset($key))
Andrey Andreevecc260e2014-01-24 14:20:13 +0200617 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300618 return isset($_SESSION[$key]) ? $_SESSION[$key] : NULL;
619 }
620 elseif (empty($_SESSION))
621 {
622 return array();
Andrey Andreevecc260e2014-01-24 14:20:13 +0200623 }
624
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300625 $userdata = array();
626 $_exclude = array_merge(
Andrey Andreevef417862014-06-04 21:28:13 +0300627 array('__ci_vars'),
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300628 $this->get_flash_keys(),
629 $this->get_temp_keys()
630 );
Darren Hillc4e266b2011-08-30 15:40:27 -0400631
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300632 foreach (array_keys($_SESSION) as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400633 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300634 if ( ! in_array($key, $_exclude, TRUE))
Darren Hillc4e266b2011-08-30 15:40:27 -0400635 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300636 $userdata[$key] = $_SESSION[$key];
Darren Hillc4e266b2011-08-30 15:40:27 -0400637 }
638 }
639
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300640 return $userdata;
Darren Hillc4e266b2011-08-30 15:40:27 -0400641 }
642
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300643 // ------------------------------------------------------------------------
644
Darren Hillc4e266b2011-08-30 15:40:27 -0400645 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300646 * Set userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400647 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300648 * Legacy CI_Session compatibility method
649 *
650 * @param mixed $data Session data key or an associative array
651 * @param mixed $value Value to store
Darren Hill5073a372011-08-31 13:54:19 -0400652 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400653 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300654 public function set_userdata($data, $value = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400655 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300656 if (is_array($data))
Darren Hillc4e266b2011-08-30 15:40:27 -0400657 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300658 foreach ($data as $key => &$value)
Darren Hillc4e266b2011-08-30 15:40:27 -0400659 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300660 $_SESSION[$key] = $value;
Johnathan Croom8d8543d2012-11-25 10:36:57 -0700661 }
662
663 return;
Johnathan Croom4beca5c2012-11-23 18:32:46 -0700664 }
Darren Hillc4e266b2011-08-30 15:40:27 -0400665
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300666 $_SESSION[$data] = $value;
Darren Hillc4e266b2011-08-30 15:40:27 -0400667 }
668
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300669 // ------------------------------------------------------------------------
670
Darren Hillc4e266b2011-08-30 15:40:27 -0400671 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300672 * Unset userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400673 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300674 * Legacy CI_Session compatibility method
675 *
676 * @param mixed $data Session data key(s)
677 * @return void
678 */
679 public function unset_userdata($key)
680 {
681 if (is_array($key))
682 {
683 foreach ($key as $k)
684 {
Andrey Andreevd069b9b2014-09-16 10:18:16 +0300685 unset($_SESSION[$k]);
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300686 }
687
688 return;
689 }
690
691 unset($_SESSION[$key]);
692 }
693
694 // ------------------------------------------------------------------------
695
696 /**
697 * All userdata (fetch)
698 *
699 * Legacy CI_Session compatibility method
700 *
701 * @return array $_SESSION, excluding flash data items
702 */
703 public function all_userdata()
704 {
705 return $this->userdata();
706 }
707
708 // ------------------------------------------------------------------------
709
710 /**
711 * Has userdata
712 *
713 * Legacy CI_Session compatibility method
714 *
715 * @param string $key Session data key
716 * @return bool
717 */
718 public function has_userdata($key)
719 {
720 return isset($_SESSION[$key]);
721 }
722
723 // ------------------------------------------------------------------------
724
725 /**
726 * Flashdata (fetch)
727 *
728 * Legacy CI_Session compatibility method
729 *
730 * @param string $key Session data key
731 * @return mixed Session data value or NULL if not found
Darren Hillc4e266b2011-08-30 15:40:27 -0400732 */
Andrey Andreevecc260e2014-01-24 14:20:13 +0200733 public function flashdata($key = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400734 {
Andrey Andreevecc260e2014-01-24 14:20:13 +0200735 if (isset($key))
736 {
Andrey Andreevef417862014-06-04 21:28:13 +0300737 return (isset($_SESSION['__ci_vars'], $_SESSION['__ci_vars'][$key], $_SESSION[$key]) && ! is_int($_SESSION['__ci_vars'][$key]))
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300738 ? $_SESSION[$key]
739 : NULL;
Andrey Andreevecc260e2014-01-24 14:20:13 +0200740 }
741
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300742 $flashdata = array();
743
Andrey Andreevef417862014-06-04 21:28:13 +0300744 if ( ! empty($_SESSION['__ci_vars']))
Andrey Andreevecc260e2014-01-24 14:20:13 +0200745 {
Andrey Andreevef417862014-06-04 21:28:13 +0300746 foreach ($_SESSION['__ci_vars'] as $key => &$value)
Andrey Andreevecc260e2014-01-24 14:20:13 +0200747 {
Andrey Andreevef417862014-06-04 21:28:13 +0300748 is_int($value) OR $flashdata[$key] = $_SESSION[$key];
Andrey Andreevecc260e2014-01-24 14:20:13 +0200749 }
750 }
751
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300752 return $flashdata;
Darren Hillc4e266b2011-08-30 15:40:27 -0400753 }
754
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300755 // ------------------------------------------------------------------------
756
Darren Hillc4e266b2011-08-30 15:40:27 -0400757 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300758 * Set flashdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400759 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300760 * Legacy CI_Session compatibiliy method
761 *
762 * @param mixed $data Session data key or an associative array
763 * @param mixed $value Value to store
Darren Hill5073a372011-08-31 13:54:19 -0400764 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400765 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300766 public function set_flashdata($data, $value = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400767 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300768 $this->set_userdata($data, $value);
Andrey Andreevc6e50982014-10-26 21:27:28 +0200769 $this->mark_as_flash(is_array($data) ? array_keys($data) : $data);
Darren Hillc4e266b2011-08-30 15:40:27 -0400770 }
771
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300772 // ------------------------------------------------------------------------
773
Darren Hillc4e266b2011-08-30 15:40:27 -0400774 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300775 * Keep flashdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400776 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300777 * Legacy CI_Session compatibility method
778 *
779 * @param mixed $key Session data key(s)
Darren Hill5073a372011-08-31 13:54:19 -0400780 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400781 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300782 public function keep_flashdata($key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400783 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300784 $this->mark_as_flash($key);
Darren Hillc4e266b2011-08-30 15:40:27 -0400785 }
786
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300787 // ------------------------------------------------------------------------
788
Darren Hillc4e266b2011-08-30 15:40:27 -0400789 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300790 * Temp data (fetch)
Darren Hillc4e266b2011-08-30 15:40:27 -0400791 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300792 * Legacy CI_Session compatibility method
793 *
794 * @param string $key Session data key
795 * @return mixed Session data value or NULL if not found
Darren Hillc4e266b2011-08-30 15:40:27 -0400796 */
Andrey Andreevecc260e2014-01-24 14:20:13 +0200797 public function tempdata($key = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400798 {
Andrey Andreevecc260e2014-01-24 14:20:13 +0200799 if (isset($key))
800 {
Andrey Andreevef417862014-06-04 21:28:13 +0300801 return (isset($_SESSION['__ci_vars'], $_SESSION['__ci_vars'][$key], $_SESSION[$key]) && is_int($_SESSION['__ci_vars'][$key]))
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300802 ? $_SESSION[$key]
803 : NULL;
Andrey Andreevecc260e2014-01-24 14:20:13 +0200804 }
805
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300806 $tempdata = array();
807
Andrey Andreevef417862014-06-04 21:28:13 +0300808 if ( ! empty($_SESSION['__ci_vars']))
Andrey Andreevecc260e2014-01-24 14:20:13 +0200809 {
Andrey Andreevef417862014-06-04 21:28:13 +0300810 foreach ($_SESSION['__ci_vars'] as $key => &$value)
Andrey Andreevecc260e2014-01-24 14:20:13 +0200811 {
Andrey Andreevef417862014-06-04 21:28:13 +0300812 is_int($value) && $tempdata[$key] = $_SESSION[$key];
Andrey Andreevecc260e2014-01-24 14:20:13 +0200813 }
814 }
815
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300816 return $tempdata;
Darren Hillc4e266b2011-08-30 15:40:27 -0400817 }
818
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300819 // ------------------------------------------------------------------------
820
Darren Hillc4e266b2011-08-30 15:40:27 -0400821 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300822 * Set tempdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400823 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300824 * Legacy CI_Session compatibility method
825 *
826 * @param mixed $data Session data key or an associative array of items
827 * @param mixed $value Value to store
828 * @param int $ttl Time-to-live in seconds
Darren Hillc4e266b2011-08-30 15:40:27 -0400829 * @return void
830 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300831 public function set_tempdata($data, $value = NULL, $ttl = 300)
Darren Hillc4e266b2011-08-30 15:40:27 -0400832 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300833 $this->set_userdata($data, $value);
834 $this->mark_as_temp($data, $ttl);
Darren Hillc4e266b2011-08-30 15:40:27 -0400835 }
836
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300837 // ------------------------------------------------------------------------
838
Darren Hillc4e266b2011-08-30 15:40:27 -0400839 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300840 * Unset tempdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400841 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300842 * Legacy CI_Session compatibility method
843 *
844 * @param mixed $data Session data key(s)
Darren Hillc4e266b2011-08-30 15:40:27 -0400845 * @return void
846 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300847 public function unset_tempdata($key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400848 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300849 $this->unmark_temp($key);
Darren Hillc4e266b2011-08-30 15:40:27 -0400850 }
851
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300852}
Darren Hillc4e266b2011-08-30 15:40:27 -0400853
854/* End of file Session.php */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300855/* Location: ./system/libraries/Session/Session.php */