blob: 010b464d30084ada0a51d8764e4cbb33bbaccfc1 [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
Andrey Andreev69e1b4f2014-01-07 17:29:25 +0200403Or if you're using PostgreSQL::
404
405 CREATE TABLE ci_sessions (
406 session_id varchar(40) DEFAULT '0' NOT NULL,
407 ip_address varchar(45) DEFAULT '0' NOT NULL,
408 user_agent varchar(120) NOT NULL,
409 last_activity bigint DEFAULT 0 NOT NULL,
410 user_data text NOT NULL,
411 PRIMARY KEY (session_id)
412 );
413
414 CREATE INDEX last_activity_idx ON ci_sessions(last_activity);
415
Derek Jones8ede1a22011-10-05 13:34:52 -0500416.. note:: By default the table is called ci_sessions, but you can name
417 it anything you want as long as you update the
Andrey Andreev69e1b4f2014-01-07 17:29:25 +0200418 *application/config/config.php* file so that it contains the name
419 you have chosen. Once you have created your database table you
420 can enable the database option in your config.php file as follows::
Derek Jones8ede1a22011-10-05 13:34:52 -0500421
422 $config['sess_use_database'] = TRUE;
423
424 Once enabled, the Session class will store session data in the DB.
425
426 Make sure you've specified the table name in your config file as well::
427
428 $config['sess_table_name'] = 'ci_sessions';
429
dchill423169f262012-08-11 20:12:05 -0400430.. note:: The Cookie driver has built-in garbage collection which clears
Derek Jones8ede1a22011-10-05 13:34:52 -0500431 out expired sessions so you do not need to write your own routine to do
432 it.
433
dchill423169f262012-08-11 20:12:05 -0400434Native Driver
435-------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500436
dchill423169f262012-08-11 20:12:05 -0400437The Native driver relies on native PHP sessions to store data in the
438$_SESSION superglobal array. All stored values continue to be available
439through $_SESSION, but flash- and temp- data items carry special prefixes.
Derek Jones8ede1a22011-10-05 13:34:52 -0500440
dchill423169f262012-08-11 20:12:05 -0400441Custom Drivers
442--------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500443
dchill423169f262012-08-11 20:12:05 -0400444You may also :doc:`create your own <../general/creating_drivers>` custom
445session drivers. A session driver basically manages an array of name/value
446pairs with some sort of storage mechanism.
Derek Jones8ede1a22011-10-05 13:34:52 -0500447
Michael5d4131b2013-09-30 12:22:29 +0300448To make a new driver, extend CI_Session_driver. Overload the ``initialize()``
dchill423169f262012-08-11 20:12:05 -0400449method and read or create session data. Then implement a save handler to
450write changed data to storage (sess_save), a destroy handler to remove
dchill42b3816b72012-08-13 09:47:58 -0400451deleted data (sess_destroy), a regenerate handler to make a new session ID
452(sess_regenerate), and an access handler to expose the data (get_userdata).
453Your initial class might look like::
Derek Jones8ede1a22011-10-05 13:34:52 -0500454
dchill423169f262012-08-11 20:12:05 -0400455 class CI_Session_custom extends CI_Session_driver {
Andrey Andreevb57b2ad2014-01-03 12:02:08 +0200456
dchill423169f262012-08-11 20:12:05 -0400457 protected function initialize()
458 {
459 // Read existing session data or create a new one
460 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500461
dchill423169f262012-08-11 20:12:05 -0400462 public function sess_save()
463 {
464 // Save current data to storage
465 }
466
467 public function sess_destroy()
468 {
469 // Destroy the current session and clean up storage
470 }
471
472 public function sess_regenerate()
473 {
474 // Create new session ID
475 }
476
477 public function &get_userdata()
478 {
479 // Return a reference to your userdata array
480 }
Andrey Andreevb57b2ad2014-01-03 12:02:08 +0200481
dchill423169f262012-08-11 20:12:05 -0400482 }
483
Michael5d4131b2013-09-30 12:22:29 +0300484Notice that ``get_userdata()`` returns a reference so the parent library is
dchill423169f262012-08-11 20:12:05 -0400485accessing the same array the driver object is using. This saves memory
486and avoids synchronization issues during usage.
487
488Put your driver in the libraries/Session/drivers folder anywhere in your
489package paths. This includes the application directory, the system directory,
Michael5d4131b2013-09-30 12:22:29 +0300490or any path you add with ``$CI->load->add_package_path()``. Your driver must be
dchill423169f262012-08-11 20:12:05 -0400491named CI_Session_<name>, and your filename must be Session_<name>.php,
492preferably also capitalized, such as::
493
494 CI_Session_foo in libraries/Session/drivers/Session_foo.php
495
496Then specify the driver by setting 'sess_driver' in your config.php file or as a
497parameter when loading the CI_Session object::
498
499 $config['sess_driver'] = 'foo';
500
501OR::
502
503 $CI->load->driver('session', array('sess_driver' => 'foo'));
504
505The driver specified by 'sess_driver' is automatically included as a valid
506driver. However, if you want to make a custom driver available as an option
507without making it the initially loaded driver, set 'sess_valid_drivers' in
508your config.php file to an array including your driver name::
509
510 $config['sess_valid_drivers'] = array('sess_driver');
Michael1c7438f2013-10-06 15:21:21 +0300511
Michael1c7438f2013-10-06 15:21:21 +0300512***************
513Class Reference
514***************
515
516.. class:: CI_Session
517
Michaele0a631c2013-10-20 10:40:51 +0300518 .. method:: load_driver($driver)
519
520 :param string $driver: Driver name
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200521 :returns: object
Michaele0a631c2013-10-20 10:40:51 +0300522
523 Loads a session storage driver
524
525 .. method:: select_driver($driver)
526
527 :param string $driver: Driver name
528 :returns: void
529
530 Selects default session storage driver.
531
532 .. method:: sess_destroy()
533
534 Destroys current session
Michaele0a631c2013-10-20 10:40:51 +0300535
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200536 .. note:: This method should be the last one called, and even flash
537 variables will no longer be available after it is used.
Connor Tumbleson75b3fb22014-01-11 06:58:43 -0600538 If you only want some items destroyed and not all, use
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200539 ``unset_userdata()``.
Michaele0a631c2013-10-20 10:40:51 +0300540
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200541 .. method:: sess_regenerate([$destroy = FALSE])
542
543 :param bool $destroy: Whether to destroy session data
Michaele0a631c2013-10-20 10:40:51 +0300544 :returns: void
545
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200546 Regenerate the current session data.
Michaele0a631c2013-10-20 10:40:51 +0300547
548 .. method:: userdata($item)
549
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200550 :param string $item: Session item name
Michaele0a631c2013-10-20 10:40:51 +0300551 :returns: string
552
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200553 Returns a string containing the value of the passed item or NULL if the item is not found.
554 Example::
Michaele0a631c2013-10-20 10:40:51 +0300555
556 $this->session->userdata('user');
557 //returns example@example.com considering the set_userdata example.
558
559 .. method:: all_userdata()
560
561 :returns: array
562
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200563 Retruns an array with all of the session userdata items.
Michaele0a631c2013-10-20 10:40:51 +0300564
565 .. method:: all_flashdata()
566
567 :returns: array
568
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200569 Retruns an array with all of the session flashdata items.
Michael1c7438f2013-10-06 15:21:21 +0300570
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200571 .. method:: set_userdata($newdata[, $newval = ''])
Michael1c7438f2013-10-06 15:21:21 +0300572
573 :param mixed $newdata: Item name or array of items
574 :param mixed $newval: Item value or empty string (not required if $newdata is array)
575 :returns: void
576
577 Sets items into session example usages::
578
579 $this->session->set_userdata('user', 'example@example.com');
580 // adds item user with value example@example.com to the session
581
582 $this->session->set_userdata(array('user'=>'example@example.com'));
583 // does the same as the above example - adds item user with value example@example.com to the session
584
Michael1c7438f2013-10-06 15:21:21 +0300585 .. method:: unset_userdata($item)
586
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200587 :param mixed $item: Item name or an array containing multiple items
Michael1c7438f2013-10-06 15:21:21 +0300588 :returns: void
589
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200590 Unsets previously set items from the session. Example::
Michael1c7438f2013-10-06 15:21:21 +0300591
592 $this->session->unset_userdata('user');
593 //unsets 'user' from session data.
594
595 $this->session->unset_userdata(array('user', 'useremail'));
Michaele0a631c2013-10-20 10:40:51 +0300596 //unsets both 'user' and 'useremail' from the session data.
Michael1c7438f2013-10-06 15:21:21 +0300597
598 .. method:: has_userdata($item)
599
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200600 :param string $item: Item name
Michael1c7438f2013-10-06 15:21:21 +0300601 :returns: bool
602
603 Checks if an item exists in the session.
604
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200605 .. method:: set_flashdata($newdata[, $newval = ''])
Michael1c7438f2013-10-06 15:21:21 +0300606
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200607 :param mixed $newdata: Item name or an array of items
Michael1c7438f2013-10-06 15:21:21 +0300608 :param mixed $newval: Item value or empty string (not required if $newdata is array)
609 :returns: void
610
611 Sets items into session flashdata example usages::
612
613 $this->session->set_flashdata('message', 'Test message.');
614 // adds item 'message' with value 'Test message.' to the session flashdata
615
616 $this->session->set_flashdata(array('message'=>'Test message.'));
617 // does the same as the above example - adds item 'message' with value 'Test message.'
618 to the session flashdata
619
Michaele0a631c2013-10-20 10:40:51 +0300620 .. method:: keep_flashdata($item)
621
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200622 :param mixed $item: Item name or an array containing multiple flashdata items
Michaele0a631c2013-10-20 10:40:51 +0300623 :returns: void
624
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200625 Keeps items into flashdata for one more request.
Michaele0a631c2013-10-20 10:40:51 +0300626
Michael1c7438f2013-10-06 15:21:21 +0300627 .. method:: flashdata($item)
628
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200629 :param string $item: Flashdata item name
Michael1c7438f2013-10-06 15:21:21 +0300630 :returns: string
631
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200632 Returns a string containing the value of the passed item or NULL if the item is not found.
633 Example::
Michael1c7438f2013-10-06 15:21:21 +0300634
635 $this->session->flashdata('message');
636 //returns 'Test message.' considering the set_flashdata example.
637
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200638 .. method:: set_tempdata($newdata[, $newval = ''[, $expire = 0]])
Michael1c7438f2013-10-06 15:21:21 +0300639
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200640 :param mixed $newdata: Item name or array containing multiple items
Michael1c7438f2013-10-06 15:21:21 +0300641 :param string $newval: Item value or empty string (not required if $newdata is array)
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200642 :param int $expire: Lifetime in seconds (0 for default)
Michael1c7438f2013-10-06 15:21:21 +0300643 :returns: void
644
645 Sets items into session tempdata example::
646
647 $this->session->set_tempdata('message', 'Test message.', '60');
648 // adds item 'message' with value 'Test message.' to the session tempdata for 60 seconds
649
650 $this->session->set_tempdata(array('message'=>'Test message.'));
651 // does the same as the above example - adds item 'message' with value 'Test message.'
652 to the session tempdata for the default value of
653
Michael1c7438f2013-10-06 15:21:21 +0300654 .. method:: unset_tempdata($item)
655
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200656 :param mixed $item: Item name or an array containing multiple items
Michael1c7438f2013-10-06 15:21:21 +0300657 :returns: void
658
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200659 Unsets previously set items from tempdata. Example::
Michael1c7438f2013-10-06 15:21:21 +0300660
661 $this->session->unset_tempdata('user');
662 //unsets 'user' from tempdata.
663
664 $this->session->unset_tempdata(array('user', 'useremail'));
Michaele0a631c2013-10-20 10:40:51 +0300665 //unsets both 'user' and 'useremail' from the tempdata.
Michael1c7438f2013-10-06 15:21:21 +0300666
Michaele0a631c2013-10-20 10:40:51 +0300667 .. method:: tempdata($item)
Michael1c7438f2013-10-06 15:21:21 +0300668
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200669 :param string $item: Tempdata item name
Michaele0a631c2013-10-20 10:40:51 +0300670 :returns: string
Michael1c7438f2013-10-06 15:21:21 +0300671
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200672 Returns a string containing the value of the passed item or NULL if the item is not found.
673 Example::
Michael1c7438f2013-10-06 15:21:21 +0300674
Michaele0a631c2013-10-20 10:40:51 +0300675 $this->session->tempdata('message');
Andrey Andreevb57b2ad2014-01-03 12:02:08 +0200676 //returns 'Test message.' considering the set_tempdata example.