blob: 4ce355606f76f9e1cc6ce7112bb6aaaa3724120a [file] [log] [blame]
dchill423169f262012-08-11 20:12:05 -04001##############
2Session Driver
3##############
Derek Jones8ede1a22011-10-05 13:34:52 -05004
5The Session class permits you maintain a user's "state" and track their
dchill423169f262012-08-11 20:12:05 -04006activity while they browse your site. CodeIgniter offers two default
7session drivers: the classic `Cookie Driver`_, and the `Native Driver`_,
8which supports usage of the native PHP Session mechanism. In addition,
9you may create your own `Custom Drivers`_ to store session data however
10you wish, while still taking advantage of the features of the Session class.
Derek Jones8ede1a22011-10-05 13:34:52 -050011
Andrey Andreevcc042092014-01-03 17:08:27 +020012.. contents::
13 :local:
14
15.. raw:: html
16
17 <div class="custom-index container"></div>
18
Derek Jones8ede1a22011-10-05 13:34:52 -050019Initializing a Session
20======================
21
22Sessions will typically run globally with each page load, so the session
dchill423169f262012-08-11 20:12:05 -040023class must either be :doc:`initialized <../general/drivers>` in your
Derek Jones8ede1a22011-10-05 13:34:52 -050024:doc:`controller <../general/controllers>` constructors, or it can be
25:doc:`auto-loaded <../general/autoloader>` by the system. For the most
26part the session class will run unattended in the background, so simply
27initializing the class will cause it to read, create, and update
28sessions.
29
30To initialize the Session class manually in your controller constructor,
Michael5d4131b2013-09-30 12:22:29 +030031use the ``$this->load->driver`` function::
Derek Jones8ede1a22011-10-05 13:34:52 -050032
dchill423169f262012-08-11 20:12:05 -040033 $this->load->driver('session');
Derek Jones8ede1a22011-10-05 13:34:52 -050034
Michael5d4131b2013-09-30 12:22:29 +030035Once loaded, the Sessions library object will be available using::
36
Andrey Andreevcc042092014-01-03 17:08:27 +020037 $this->session
Derek Jones8ede1a22011-10-05 13:34:52 -050038
39How do Sessions work?
40=====================
41
42When a page is loaded, the session class will check to see if valid
dchill423169f262012-08-11 20:12:05 -040043session data exists in the user's session. If sessions data does **not**
44exist (or if it has expired) a new session will be created and saved.
45If a session does exist, its information will be updated. With each update,
46the session_id will be regenerated.
Derek Jones8ede1a22011-10-05 13:34:52 -050047
48It's important for you to understand that once initialized, the Session
49class runs automatically. There is nothing you need to do to cause the
50above behavior to happen. You can, as you'll see below, work with
51session data or even add your own data to a user's session, but the
52process of reading, writing, and updating a session is automatic.
53
54What is Session Data?
55=====================
56
57A *session*, as far as CodeIgniter is concerned, is simply an array
58containing the following information:
59
Michael1c7438f2013-10-06 15:21:21 +030060- The user's unique Session ID (this is a statistically random string
Derek Jones8ede1a22011-10-05 13:34:52 -050061 with very strong entropy, hashed with MD5 for portability, and
62 regenerated (by default) every five minutes)
Michael1c7438f2013-10-06 15:21:21 +030063- The user's IP Address
64- The user's User Agent data (the first 120 characters of the browser
Derek Jones8ede1a22011-10-05 13:34:52 -050065 data string)
Michael1c7438f2013-10-06 15:21:21 +030066- The "last activity" time stamp.
Derek Jones8ede1a22011-10-05 13:34:52 -050067
68The above data is stored in a cookie as a serialized array with this
69prototype::
70
Derek Jones4b83d912011-10-05 15:42:30 -050071 [array]
72 (
Michael5d4131b2013-09-30 12:22:29 +030073 'session_id' => random hash,
74 'ip_address' => 'string - user IP address',
75 'user_agent' => 'string - user agent data',
76 'last_activity' => timestamp
Derek Jones4b83d912011-10-05 15:42:30 -050077 )
Derek Jones8ede1a22011-10-05 13:34:52 -050078
dchill423169f262012-08-11 20:12:05 -040079.. note:: Sessions are only updated every five minutes by default to
80 reduce processor load. If you repeatedly reload a page you'll notice
81 that the "last activity" time only updates if five minutes or more has
82 passed since the last time the cookie was written. This time is
83 configurable by changing the $config['sess_time_to_update'] line in
84 your system/config/config.php file.
Derek Jones8ede1a22011-10-05 13:34:52 -050085
86Retrieving Session Data
87=======================
88
89Any piece of information from the session array is available using the
90following function::
91
92 $this->session->userdata('item');
93
94Where item is the array index corresponding to the item you wish to
95fetch. For example, to fetch the session ID you will do this::
96
97 $session_id = $this->session->userdata('session_id');
98
dchill423169f262012-08-11 20:12:05 -040099.. note:: The function returns NULL if the item you are
Derek Jones8ede1a22011-10-05 13:34:52 -0500100 trying to access does not exist.
101
102Adding Custom Session Data
103==========================
104
105A useful aspect of the session array is that you can add your own data
106to it and it will be stored in the user's cookie. Why would you want to
107do this? Here's one example:
108
109Let's say a particular user logs into your site. Once authenticated, you
dchill423169f262012-08-11 20:12:05 -0400110could add their username and email address to the session, making
Derek Jones8ede1a22011-10-05 13:34:52 -0500111that data globally available to you without having to run a database
112query when you need it.
113
114To add your data to the session array involves passing an array
115containing your new data to this function::
116
117 $this->session->set_userdata($array);
118
119Where $array is an associative array containing your new data. Here's an
120example::
121
Derek Jones4b83d912011-10-05 15:42:30 -0500122 $newdata = array(
Michael5d4131b2013-09-30 12:22:29 +0300123 'username' => 'johndoe',
124 'email' => 'johndoe@some-site.com',
125 'logged_in' => TRUE
126 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500127
Derek Jones4b83d912011-10-05 15:42:30 -0500128 $this->session->set_userdata($newdata);
Derek Jones8ede1a22011-10-05 13:34:52 -0500129
Michael5d4131b2013-09-30 12:22:29 +0300130If you want to add userdata one value at a time, ``set_userdata()`` also
Derek Jones8ede1a22011-10-05 13:34:52 -0500131supports this syntax.
132
133::
134
135 $this->session->set_userdata('some_name', 'some_value');
136
Michael5d4131b2013-09-30 12:22:29 +0300137If you want to verify that a userdata value exists, call ``has_userdata()``.
Derek Jones8ede1a22011-10-05 13:34:52 -0500138
dchill423169f262012-08-11 20:12:05 -0400139::
140
141 $this->session->has_userdata('some_name');
Derek Jones8ede1a22011-10-05 13:34:52 -0500142
143Retrieving All Session Data
144===========================
145
146An array of all userdata can be retrieved as follows::
147
148 $this->session->all_userdata()
149
150And returns an associative array like the following::
151
Derek Jones4b83d912011-10-05 15:42:30 -0500152 Array
153 (
Michael5d4131b2013-09-30 12:22:29 +0300154 [session_id] => 4a5a5dca22728fb0a84364eeb405b601
155 [ip_address] => 127.0.0.1
156 [user_agent] => Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7;
157 [last_activity] => 1303142623
Derek Jones4b83d912011-10-05 15:42:30 -0500158 )
Derek Jones8ede1a22011-10-05 13:34:52 -0500159
160Removing Session Data
161=====================
162
163Just as set_userdata() can be used to add information into a session,
164unset_userdata() can be used to remove it, by passing the session key.
165For example, if you wanted to remove 'some_name' from your session
166information::
167
168 $this->session->unset_userdata('some_name');
169
170
171This function can also be passed an associative array of items to unset.
172
173::
174
Derek Jones4b83d912011-10-05 15:42:30 -0500175 $array_items = array('username' => '', 'email' => '');
176
177 $this->session->unset_userdata($array_items);
Derek Jones8ede1a22011-10-05 13:34:52 -0500178
179
180Flashdata
181=========
182
183CodeIgniter supports "flashdata", or session data that will only be
184available for the next server request, and are then automatically
185cleared. These can be very useful, and are typically used for
186informational or status messages (for example: "record 2 deleted").
187
dchill423169f262012-08-11 20:12:05 -0400188.. note:: Flash variables are prefaced with "flash\_" so avoid this prefix
189 in your own session names.
Derek Jones8ede1a22011-10-05 13:34:52 -0500190
191To add flashdata::
192
193 $this->session->set_flashdata('item', 'value');
194
195
Michael5d4131b2013-09-30 12:22:29 +0300196You can also pass an array to ``set_flashdata()``, in the same manner as
197``set_userdata()``.
Derek Jones8ede1a22011-10-05 13:34:52 -0500198
199To read a flashdata variable::
200
201 $this->session->flashdata('item');
Johnathan Croom4beca5c2012-11-23 18:32:46 -0700202
Mike Funk7c26fab2012-02-24 09:45:02 -0500203An array of all flashdata can be retrieved as follows::
204
205 $this->session->all_flashdata();
Derek Jones8ede1a22011-10-05 13:34:52 -0500206
207
208If you find that you need to preserve a flashdata variable through an
Michael5d4131b2013-09-30 12:22:29 +0300209additional request, you can do so using the ``keep_flashdata()`` function.
Johnathan Croom4beca5c2012-11-23 18:32:46 -0700210You can either pass a single item or an array of flashdata items to keep.
Derek Jones8ede1a22011-10-05 13:34:52 -0500211
212::
213
214 $this->session->keep_flashdata('item');
Johnathan Croom4beca5c2012-11-23 18:32:46 -0700215 $this->session->keep_flashdata(array('item1', 'item2', 'item3'));
Derek Jones8ede1a22011-10-05 13:34:52 -0500216
Michael5d4131b2013-09-30 12:22:29 +0300217.. note:: The function will return NULL if the item cannot be found.
218
dchill423169f262012-08-11 20:12:05 -0400219Tempdata
220========
221
222CodeIgniter also supports "tempdata", or session data with a specific
223expiration time. After the value expires, or the session expires or is
224deleted, the value is automatically removed.
225
226To add tempdata::
227
228 $expire = 300; // Expire in 5 minutes
229
230 $this->session->set_tempdata('item', 'value', $expire);
231
Michael5d4131b2013-09-30 12:22:29 +0300232You can also pass an array to ``set_tempdata()``::
dchill423169f262012-08-11 20:12:05 -0400233
234 $tempdata = array('newuser' => TRUE, 'message' => 'Thanks for joining!');
235
236 $this->session->set_tempdata($tempdata, '', $expire);
237
238.. note:: If the expiration is omitted or set to 0, the default expiration of
239 5 minutes will be used.
240
241To read a tempdata variable::
242
243 $this->session->tempdata('item');
244
245If you need to remove a tempdata value before it expires,
Michael5d4131b2013-09-30 12:22:29 +0300246use ``unset_tempdata()``::
dchill423169f262012-08-11 20:12:05 -0400247
248 $this->session->unset_tempdata('item');
249
250Destroying a Session
251====================
252
253To clear the current session::
254
255 $this->session->sess_destroy();
256
257.. note:: This function should be the last one called, and even flash
258 variables will no longer be available. If you only want some items
Michael5d4131b2013-09-30 12:22:29 +0300259 destroyed and not all, use ``unset_userdata()``.
dchill423169f262012-08-11 20:12:05 -0400260
261Session Preferences
262===================
263
264You'll find the following Session related preferences in your
265application/config/config.php file:
266
267=========================== =============== =========================== ==========================================================================
268Preference Default Options Description
269=========================== =============== =========================== ==========================================================================
270**sess_driver** cookie cookie/native/*custom* The initial session driver to load.
271**sess_valid_drivers** cookie, native None Additional valid drivers which may be loaded.
272**sess_cookie_name** ci_session None The name you want the session cookie saved as (data for Cookie driver or
273 session ID for Native driver).
274**sess_expiration** 7200 None The number of seconds you would like the session to last. The default
275 value is 2 hours (7200 seconds). If you would like a non-expiring
276 session set the value to zero: 0
277**sess_expire_on_close** FALSE TRUE/FALSE (boolean) Whether to cause the session to expire automatically when the browser
278 window is closed.
279**sess_encrypt_cookie** FALSE TRUE/FALSE (boolean) Whether to encrypt the session data (Cookie driver only).
280**sess_use_database** FALSE TRUE/FALSE (boolean) Whether to save the session data to a database. You must create the
281 table before enabling this option (Cookie driver only).
282**sess_table_name** ci_sessions Any valid SQL table name The name of the session database table (Cookie driver only).
283**sess_time_to_update** 300 Time in seconds This options controls how often the session class will regenerate itself
284 and create a new session id.
285**sess_match_ip** FALSE TRUE/FALSE (boolean) Whether to match the user's IP address when reading the session data.
286 Note that some ISPs dynamically changes the IP, so if you want a
287 non-expiring session you will likely set this to FALSE.
288**sess_match_useragent** TRUE TRUE/FALSE (boolean) Whether to match the User Agent when reading the session data.
289=========================== =============== =========================== ==========================================================================
290
291In addition to the values above, the cookie and native drivers apply the
292following configuration values shared by the :doc:`Input <input>` and
293:doc:`Security <security>` classes:
294
295=========================== =============== ==========================================================================
296Preference Default Description
297=========================== =============== ==========================================================================
298**cookie_prefix** '' Set a cookie name prefix in order to avoid name collisions
299**cookie_domain** '' The domain for which the session is applicable
300**cookie_path** / The path to which the session is applicable
301=========================== =============== ==========================================================================
302
303Session Drivers
304===============
305
306By default, the `Cookie Driver`_ is loaded when a session is initialized.
307However, any valid driver may be selected with the $config['sess_driver']
308line in your config.php file.
309
310The session driver library comes with the cookie and native drivers
311installed, and `Custom Drivers`_ may also be installed by the user.
312
313Typically, only one driver will be used at a time, but CodeIgniter does
314support loading multiple drivers. If a specific valid driver is called, it
315will be automatically loaded. Or, an additional driver may be explicitly
Michael5d4131b2013-09-30 12:22:29 +0300316loaded by ``calling load_driver()``::
dchill423169f262012-08-11 20:12:05 -0400317
318 $this->session->load_driver('native');
319
320The Session library keeps track of the most recently selected driver to call
321for driver methods. Normally, session class methods are called directly on
322the parent class, as illustrated above. However, any methods called through
323a specific driver will select that driver before invoking the parent method.
324
325So, alternation between multiple drivers can be achieved by specifying which
326driver to use for each call::
327
328 $this->session->native->set_userdata('foo', 'bar');
329
330 $this->session->cookie->userdata('foo');
331
332 $this->session->native->unset_userdata('foo');
333
334Notice in the previous example that the *native* userdata value 'foo'
335would be set to 'bar', which would NOT be returned by the call for
336the *cookie* userdata 'foo', nor would the *cookie* value be unset by
337the call to unset the *native* 'foo' value. The drivers maintain independent
338sets of values, regardless of key names.
339
340A specific driver may also be explicitly selected for use by pursuant
Michael5d4131b2013-09-30 12:22:29 +0300341methods with the ``select_driver()`` call::
dchill423169f262012-08-11 20:12:05 -0400342
343 $this->session->select_driver('native');
344
345 $this->session->userdata('item'); // Uses the native driver
346
347Cookie Driver
348-------------
349
350The Cookie driver stores session information for each user as serialized
351(and optionally encrypted) data in a cookie. It can also store the session
352data in a database table for added security, as this permits the session ID
353in the user's cookie to be matched against the stored session ID. By default
354only the cookie is saved. If you choose to use the database option you'll
355need to create the session table as indicated below.
356
357If you have the encryption option enabled, the serialized array will be
358encrypted before being stored in the cookie, making the data highly
359secure and impervious to being read or altered by someone. More info
360regarding encryption can be :doc:`found here <encryption>`, although
361the Session class will take care of initializing and encrypting the data
362automatically.
363
364.. note:: Even if you are not using encrypted sessions, you must set
365 an :doc:`encryption key <./encryption>` in your config file which is used
366 to aid in preventing session data manipulation.
367
368.. note:: Cookies can only hold 4KB of data, so be careful not to exceed
369 the capacity. The encryption process in particular produces a longer
370 data string than the original so keep careful track of how much data you
371 are storing.
Derek Jones8ede1a22011-10-05 13:34:52 -0500372
373Saving Session Data to a Database
dchill423169f262012-08-11 20:12:05 -0400374^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Derek Jones8ede1a22011-10-05 13:34:52 -0500375
376While the session data array stored in the user's cookie contains a
377Session ID, unless you store session data in a database there is no way
378to validate it. For some applications that require little or no
379security, session ID validation may not be needed, but if your
380application requires security, validation is mandatory. Otherwise, an
381old session could be restored by a user modifying their cookies.
382
383When session data is available in a database, every time a valid session
384is found in the user's cookie, a database query is performed to match
385it. If the session ID does not match, the session is destroyed. Session
386IDs can never be updated, they can only be generated when a new session
387is created.
388
389In order to store sessions, you must first create a database table for
390this purpose. Here is the basic prototype (for MySQL) required by the
Derek Jones4b83d912011-10-05 15:42:30 -0500391session class::
Derek Jones8ede1a22011-10-05 13:34:52 -0500392
Derek Jones4b83d912011-10-05 15:42:30 -0500393 CREATE TABLE IF NOT EXISTS `ci_sessions` (
394 session_id varchar(40) DEFAULT '0' NOT NULL,
Andrey Andreev5a257182012-06-10 06:18:14 +0300395 ip_address varchar(45) DEFAULT '0' NOT NULL,
Derek Jones4b83d912011-10-05 15:42:30 -0500396 user_agent varchar(120) NOT NULL,
397 last_activity int(10) unsigned DEFAULT 0 NOT NULL,
398 user_data text NOT NULL,
Andrey Andreeve2afc882012-11-01 01:35:34 +0200399 PRIMARY KEY (session_id, ip_address, user_agent),
Derek Jones4b83d912011-10-05 15:42:30 -0500400 KEY `last_activity_idx` (`last_activity`)
401 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500402
403.. note:: By default the table is called ci_sessions, but you can name
404 it anything you want as long as you update the
405 application/config/config.php file so that it contains the name you have
406 chosen. Once you have created your database table you can enable the
407 database option in your config.php file as follows::
408
409 $config['sess_use_database'] = TRUE;
410
411 Once enabled, the Session class will store session data in the DB.
412
413 Make sure you've specified the table name in your config file as well::
414
415 $config['sess_table_name'] = 'ci_sessions';
416
dchill423169f262012-08-11 20:12:05 -0400417.. note:: The Cookie driver has built-in garbage collection which clears
Derek Jones8ede1a22011-10-05 13:34:52 -0500418 out expired sessions so you do not need to write your own routine to do
419 it.
420
dchill423169f262012-08-11 20:12:05 -0400421Native Driver
422-------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500423
dchill423169f262012-08-11 20:12:05 -0400424The Native driver relies on native PHP sessions to store data in the
425$_SESSION superglobal array. All stored values continue to be available
426through $_SESSION, but flash- and temp- data items carry special prefixes.
Derek Jones8ede1a22011-10-05 13:34:52 -0500427
dchill423169f262012-08-11 20:12:05 -0400428Custom Drivers
429--------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500430
dchill423169f262012-08-11 20:12:05 -0400431You may also :doc:`create your own <../general/creating_drivers>` custom
432session drivers. A session driver basically manages an array of name/value
433pairs with some sort of storage mechanism.
Derek Jones8ede1a22011-10-05 13:34:52 -0500434
Michael5d4131b2013-09-30 12:22:29 +0300435To make a new driver, extend CI_Session_driver. Overload the ``initialize()``
dchill423169f262012-08-11 20:12:05 -0400436method and read or create session data. Then implement a save handler to
437write changed data to storage (sess_save), a destroy handler to remove
dchill42b3816b72012-08-13 09:47:58 -0400438deleted data (sess_destroy), a regenerate handler to make a new session ID
439(sess_regenerate), and an access handler to expose the data (get_userdata).
440Your initial class might look like::
Derek Jones8ede1a22011-10-05 13:34:52 -0500441
dchill423169f262012-08-11 20:12:05 -0400442 class CI_Session_custom extends CI_Session_driver {
Andrey Andreevb57b2ad2014-01-03 12:02:08 +0200443
dchill423169f262012-08-11 20:12:05 -0400444 protected function initialize()
445 {
446 // Read existing session data or create a new one
447 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500448
dchill423169f262012-08-11 20:12:05 -0400449 public function sess_save()
450 {
451 // Save current data to storage
452 }
453
454 public function sess_destroy()
455 {
456 // Destroy the current session and clean up storage
457 }
458
459 public function sess_regenerate()
460 {
461 // Create new session ID
462 }
463
464 public function &get_userdata()
465 {
466 // Return a reference to your userdata array
467 }
Andrey Andreevb57b2ad2014-01-03 12:02:08 +0200468
dchill423169f262012-08-11 20:12:05 -0400469 }
470
Michael5d4131b2013-09-30 12:22:29 +0300471Notice that ``get_userdata()`` returns a reference so the parent library is
dchill423169f262012-08-11 20:12:05 -0400472accessing the same array the driver object is using. This saves memory
473and avoids synchronization issues during usage.
474
475Put your driver in the libraries/Session/drivers folder anywhere in your
476package paths. This includes the application directory, the system directory,
Michael5d4131b2013-09-30 12:22:29 +0300477or any path you add with ``$CI->load->add_package_path()``. Your driver must be
dchill423169f262012-08-11 20:12:05 -0400478named CI_Session_<name>, and your filename must be Session_<name>.php,
479preferably also capitalized, such as::
480
481 CI_Session_foo in libraries/Session/drivers/Session_foo.php
482
483Then specify the driver by setting 'sess_driver' in your config.php file or as a
484parameter when loading the CI_Session object::
485
486 $config['sess_driver'] = 'foo';
487
488OR::
489
490 $CI->load->driver('session', array('sess_driver' => 'foo'));
491
492The driver specified by 'sess_driver' is automatically included as a valid
493driver. However, if you want to make a custom driver available as an option
494without making it the initially loaded driver, set 'sess_valid_drivers' in
495your config.php file to an array including your driver name::
496
497 $config['sess_valid_drivers'] = array('sess_driver');
Michael1c7438f2013-10-06 15:21:21 +0300498
499
Michael1c7438f2013-10-06 15:21:21 +0300500***************
501Class Reference
502***************
503
504.. class:: CI_Session
505
Michaele0a631c2013-10-20 10:40:51 +0300506 .. method:: load_driver($driver)
507
508 :param string $driver: Driver name
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200509 :returns: object
Michaele0a631c2013-10-20 10:40:51 +0300510
511 Loads a session storage driver
512
513 .. method:: select_driver($driver)
514
515 :param string $driver: Driver name
516 :returns: void
517
518 Selects default session storage driver.
519
520 .. method:: sess_destroy()
521
522 Destroys current session
Michaele0a631c2013-10-20 10:40:51 +0300523
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200524 .. note:: This method should be the last one called, and even flash
525 variables will no longer be available after it is used.
526 If you only want some items destroyed and not all, use
527 ``unset_userdata()``.
Michaele0a631c2013-10-20 10:40:51 +0300528
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200529 .. method:: sess_regenerate([$destroy = FALSE])
530
531 :param bool $destroy: Whether to destroy session data
Michaele0a631c2013-10-20 10:40:51 +0300532 :returns: void
533
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200534 Regenerate the current session data.
Michaele0a631c2013-10-20 10:40:51 +0300535
536 .. method:: userdata($item)
537
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200538 :param string $item: Session item name
Michaele0a631c2013-10-20 10:40:51 +0300539 :returns: string
540
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200541 Returns a string containing the value of the passed item or NULL if the item is not found.
542 Example::
Michaele0a631c2013-10-20 10:40:51 +0300543
544 $this->session->userdata('user');
545 //returns example@example.com considering the set_userdata example.
546
547 .. method:: all_userdata()
548
549 :returns: array
550
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200551 Retruns an array with all of the session userdata items.
Michaele0a631c2013-10-20 10:40:51 +0300552
553 .. method:: all_flashdata()
554
555 :returns: array
556
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200557 Retruns an array with all of the session flashdata items.
Michael1c7438f2013-10-06 15:21:21 +0300558
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200559 .. method:: set_userdata($newdata[, $newval = ''])
Michael1c7438f2013-10-06 15:21:21 +0300560
561 :param mixed $newdata: Item name or array of items
562 :param mixed $newval: Item value or empty string (not required if $newdata is array)
563 :returns: void
564
565 Sets items into session example usages::
566
567 $this->session->set_userdata('user', 'example@example.com');
568 // adds item user with value example@example.com to the session
569
570 $this->session->set_userdata(array('user'=>'example@example.com'));
571 // does the same as the above example - adds item user with value example@example.com to the session
572
Michael1c7438f2013-10-06 15:21:21 +0300573 .. method:: unset_userdata($item)
574
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200575 :param mixed $item: Item name or an array containing multiple items
Michael1c7438f2013-10-06 15:21:21 +0300576 :returns: void
577
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200578 Unsets previously set items from the session. Example::
Michael1c7438f2013-10-06 15:21:21 +0300579
580 $this->session->unset_userdata('user');
581 //unsets 'user' from session data.
582
583 $this->session->unset_userdata(array('user', 'useremail'));
Michaele0a631c2013-10-20 10:40:51 +0300584 //unsets both 'user' and 'useremail' from the session data.
Michael1c7438f2013-10-06 15:21:21 +0300585
586 .. method:: has_userdata($item)
587
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200588 :param string $item: Item name
Michael1c7438f2013-10-06 15:21:21 +0300589 :returns: bool
590
591 Checks if an item exists in the session.
592
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200593 .. method:: set_flashdata($newdata[, $newval = ''])
Michael1c7438f2013-10-06 15:21:21 +0300594
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200595 :param mixed $newdata: Item name or an array of items
Michael1c7438f2013-10-06 15:21:21 +0300596 :param mixed $newval: Item value or empty string (not required if $newdata is array)
597 :returns: void
598
599 Sets items into session flashdata example usages::
600
601 $this->session->set_flashdata('message', 'Test message.');
602 // adds item 'message' with value 'Test message.' to the session flashdata
603
604 $this->session->set_flashdata(array('message'=>'Test message.'));
605 // does the same as the above example - adds item 'message' with value 'Test message.'
606 to the session flashdata
607
Michaele0a631c2013-10-20 10:40:51 +0300608 .. method:: keep_flashdata($item)
609
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200610 :param mixed $item: Item name or an array containing multiple flashdata items
Michaele0a631c2013-10-20 10:40:51 +0300611 :returns: void
612
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200613 Keeps items into flashdata for one more request.
Michaele0a631c2013-10-20 10:40:51 +0300614
Michael1c7438f2013-10-06 15:21:21 +0300615 .. method:: flashdata($item)
616
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200617 :param string $item: Flashdata item name
Michael1c7438f2013-10-06 15:21:21 +0300618 :returns: string
619
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200620 Returns a string containing the value of the passed item or NULL if the item is not found.
621 Example::
Michael1c7438f2013-10-06 15:21:21 +0300622
623 $this->session->flashdata('message');
624 //returns 'Test message.' considering the set_flashdata example.
625
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200626 .. method:: set_tempdata($newdata[, $newval = ''[, $expire = 0]])
Michael1c7438f2013-10-06 15:21:21 +0300627
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200628 :param mixed $newdata: Item name or array containing multiple items
Michael1c7438f2013-10-06 15:21:21 +0300629 :param string $newval: Item value or empty string (not required if $newdata is array)
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200630 :param int $expire: Lifetime in seconds (0 for default)
Michael1c7438f2013-10-06 15:21:21 +0300631 :returns: void
632
633 Sets items into session tempdata example::
634
635 $this->session->set_tempdata('message', 'Test message.', '60');
636 // adds item 'message' with value 'Test message.' to the session tempdata for 60 seconds
637
638 $this->session->set_tempdata(array('message'=>'Test message.'));
639 // does the same as the above example - adds item 'message' with value 'Test message.'
640 to the session tempdata for the default value of
641
Michael1c7438f2013-10-06 15:21:21 +0300642 .. method:: unset_tempdata($item)
643
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200644 :param mixed $item: Item name or an array containing multiple items
Michael1c7438f2013-10-06 15:21:21 +0300645 :returns: void
646
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200647 Unsets previously set items from tempdata. Example::
Michael1c7438f2013-10-06 15:21:21 +0300648
649 $this->session->unset_tempdata('user');
650 //unsets 'user' from tempdata.
651
652 $this->session->unset_tempdata(array('user', 'useremail'));
Michaele0a631c2013-10-20 10:40:51 +0300653 //unsets both 'user' and 'useremail' from the tempdata.
Michael1c7438f2013-10-06 15:21:21 +0300654
Michaele0a631c2013-10-20 10:40:51 +0300655 .. method:: tempdata($item)
Michael1c7438f2013-10-06 15:21:21 +0300656
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200657 :param string $item: Tempdata item name
Michaele0a631c2013-10-20 10:40:51 +0300658 :returns: string
Michael1c7438f2013-10-06 15:21:21 +0300659
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200660 Returns a string containing the value of the passed item or NULL if the item is not found.
661 Example::
Michael1c7438f2013-10-06 15:21:21 +0300662
Michaele0a631c2013-10-20 10:40:51 +0300663 $this->session->tempdata('message');
Andrey Andreevb57b2ad2014-01-03 12:02:08 +0200664 //returns 'Test message.' considering the set_tempdata example.