blob: f05f86af121a5e16de53bea461116d7b2bdb78b9 [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
Andrey Andreevde1fe7d2014-01-20 17:06:16 +020019***********************
20Using the Session Class
21***********************
22
Derek Jones8ede1a22011-10-05 13:34:52 -050023Initializing a Session
24======================
25
26Sessions will typically run globally with each page load, so the session
dchill423169f262012-08-11 20:12:05 -040027class must either be :doc:`initialized <../general/drivers>` in your
Derek Jones8ede1a22011-10-05 13:34:52 -050028:doc:`controller <../general/controllers>` constructors, or it can be
29:doc:`auto-loaded <../general/autoloader>` by the system. For the most
30part the session class will run unattended in the background, so simply
31initializing the class will cause it to read, create, and update
32sessions.
33
34To initialize the Session class manually in your controller constructor,
Michael5d4131b2013-09-30 12:22:29 +030035use the ``$this->load->driver`` function::
Derek Jones8ede1a22011-10-05 13:34:52 -050036
dchill423169f262012-08-11 20:12:05 -040037 $this->load->driver('session');
Derek Jones8ede1a22011-10-05 13:34:52 -050038
Michael5d4131b2013-09-30 12:22:29 +030039Once loaded, the Sessions library object will be available using::
40
Andrey Andreevcc042092014-01-03 17:08:27 +020041 $this->session
Derek Jones8ede1a22011-10-05 13:34:52 -050042
43How do Sessions work?
44=====================
45
46When a page is loaded, the session class will check to see if valid
dchill423169f262012-08-11 20:12:05 -040047session data exists in the user's session. If sessions data does **not**
48exist (or if it has expired) a new session will be created and saved.
49If a session does exist, its information will be updated. With each update,
50the session_id will be regenerated.
Derek Jones8ede1a22011-10-05 13:34:52 -050051
52It's important for you to understand that once initialized, the Session
53class runs automatically. There is nothing you need to do to cause the
54above behavior to happen. You can, as you'll see below, work with
55session data or even add your own data to a user's session, but the
56process of reading, writing, and updating a session is automatic.
57
58What is Session Data?
59=====================
60
61A *session*, as far as CodeIgniter is concerned, is simply an array
62containing the following information:
63
Michael1c7438f2013-10-06 15:21:21 +030064- The user's unique Session ID (this is a statistically random string
Derek Jones8ede1a22011-10-05 13:34:52 -050065 with very strong entropy, hashed with MD5 for portability, and
66 regenerated (by default) every five minutes)
Michael1c7438f2013-10-06 15:21:21 +030067- The user's IP Address
68- The user's User Agent data (the first 120 characters of the browser
Derek Jones8ede1a22011-10-05 13:34:52 -050069 data string)
Michael1c7438f2013-10-06 15:21:21 +030070- The "last activity" time stamp.
Derek Jones8ede1a22011-10-05 13:34:52 -050071
72The above data is stored in a cookie as a serialized array with this
73prototype::
74
Derek Jones4b83d912011-10-05 15:42:30 -050075 [array]
76 (
Michael5d4131b2013-09-30 12:22:29 +030077 'session_id' => random hash,
78 'ip_address' => 'string - user IP address',
79 'user_agent' => 'string - user agent data',
80 'last_activity' => timestamp
Derek Jones4b83d912011-10-05 15:42:30 -050081 )
Derek Jones8ede1a22011-10-05 13:34:52 -050082
dchill423169f262012-08-11 20:12:05 -040083.. note:: Sessions are only updated every five minutes by default to
84 reduce processor load. If you repeatedly reload a page you'll notice
85 that the "last activity" time only updates if five minutes or more has
86 passed since the last time the cookie was written. This time is
87 configurable by changing the $config['sess_time_to_update'] line in
88 your system/config/config.php file.
Derek Jones8ede1a22011-10-05 13:34:52 -050089
90Retrieving Session Data
91=======================
92
93Any piece of information from the session array is available using the
94following function::
95
96 $this->session->userdata('item');
97
98Where item is the array index corresponding to the item you wish to
99fetch. For example, to fetch the session ID you will do this::
100
101 $session_id = $this->session->userdata('session_id');
102
dchill423169f262012-08-11 20:12:05 -0400103.. note:: The function returns NULL if the item you are
Derek Jones8ede1a22011-10-05 13:34:52 -0500104 trying to access does not exist.
105
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200106If you want to retrieve all of the existing userdata, you can simply
107omit the item key parameter::
108
109 $this->session->userdata();
110
111 /**
112 * Produces something similar to:
113 *
114 * Array
115 * (
116 * [session_id] => 4a5a5dca22728fb0a84364eeb405b601
117 * [ip_address] => 127.0.0.1
118 * [user_agent] => Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7;
119 * [last_activity] => 1303142623
120 * )
121 */
122
Derek Jones8ede1a22011-10-05 13:34:52 -0500123Adding Custom Session Data
124==========================
125
126A useful aspect of the session array is that you can add your own data
127to it and it will be stored in the user's cookie. Why would you want to
128do this? Here's one example:
129
130Let's say a particular user logs into your site. Once authenticated, you
dchill423169f262012-08-11 20:12:05 -0400131could add their username and email address to the session, making
Derek Jones8ede1a22011-10-05 13:34:52 -0500132that data globally available to you without having to run a database
133query when you need it.
134
135To add your data to the session array involves passing an array
136containing your new data to this function::
137
138 $this->session->set_userdata($array);
139
140Where $array is an associative array containing your new data. Here's an
141example::
142
Derek Jones4b83d912011-10-05 15:42:30 -0500143 $newdata = array(
Michael5d4131b2013-09-30 12:22:29 +0300144 'username' => 'johndoe',
145 'email' => 'johndoe@some-site.com',
146 'logged_in' => TRUE
147 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500148
Derek Jones4b83d912011-10-05 15:42:30 -0500149 $this->session->set_userdata($newdata);
Derek Jones8ede1a22011-10-05 13:34:52 -0500150
Michael5d4131b2013-09-30 12:22:29 +0300151If you want to add userdata one value at a time, ``set_userdata()`` also
Derek Jones8ede1a22011-10-05 13:34:52 -0500152supports this syntax.
153
154::
155
156 $this->session->set_userdata('some_name', 'some_value');
157
Michael5d4131b2013-09-30 12:22:29 +0300158If you want to verify that a userdata value exists, call ``has_userdata()``.
Derek Jones8ede1a22011-10-05 13:34:52 -0500159
dchill423169f262012-08-11 20:12:05 -0400160::
161
162 $this->session->has_userdata('some_name');
Derek Jones8ede1a22011-10-05 13:34:52 -0500163
Derek Jones8ede1a22011-10-05 13:34:52 -0500164Removing Session Data
165=====================
166
167Just as set_userdata() can be used to add information into a session,
168unset_userdata() can be used to remove it, by passing the session key.
169For example, if you wanted to remove 'some_name' from your session
170information::
171
172 $this->session->unset_userdata('some_name');
173
174
175This function can also be passed an associative array of items to unset.
176
177::
178
Derek Jones4b83d912011-10-05 15:42:30 -0500179 $array_items = array('username' => '', 'email' => '');
180
181 $this->session->unset_userdata($array_items);
Derek Jones8ede1a22011-10-05 13:34:52 -0500182
183
184Flashdata
185=========
186
187CodeIgniter supports "flashdata", or session data that will only be
188available for the next server request, and are then automatically
189cleared. These can be very useful, and are typically used for
190informational or status messages (for example: "record 2 deleted").
191
dchill423169f262012-08-11 20:12:05 -0400192.. note:: Flash variables are prefaced with "flash\_" so avoid this prefix
193 in your own session names.
Derek Jones8ede1a22011-10-05 13:34:52 -0500194
195To add flashdata::
196
197 $this->session->set_flashdata('item', 'value');
198
199
Michael5d4131b2013-09-30 12:22:29 +0300200You can also pass an array to ``set_flashdata()``, in the same manner as
201``set_userdata()``.
Derek Jones8ede1a22011-10-05 13:34:52 -0500202
203To read a flashdata variable::
204
205 $this->session->flashdata('item');
Johnathan Croom4beca5c2012-11-23 18:32:46 -0700206
Mike Funk7c26fab2012-02-24 09:45:02 -0500207An array of all flashdata can be retrieved as follows::
208
Andrey Andreevecc260e2014-01-24 14:20:13 +0200209 $this->session->flashdata();
Derek Jones8ede1a22011-10-05 13:34:52 -0500210
211
212If you find that you need to preserve a flashdata variable through an
Michael5d4131b2013-09-30 12:22:29 +0300213additional request, you can do so using the ``keep_flashdata()`` function.
Johnathan Croom4beca5c2012-11-23 18:32:46 -0700214You can either pass a single item or an array of flashdata items to keep.
Derek Jones8ede1a22011-10-05 13:34:52 -0500215
216::
217
218 $this->session->keep_flashdata('item');
Johnathan Croom4beca5c2012-11-23 18:32:46 -0700219 $this->session->keep_flashdata(array('item1', 'item2', 'item3'));
Derek Jones8ede1a22011-10-05 13:34:52 -0500220
Michael5d4131b2013-09-30 12:22:29 +0300221.. note:: The function will return NULL if the item cannot be found.
222
dchill423169f262012-08-11 20:12:05 -0400223Tempdata
224========
225
226CodeIgniter also supports "tempdata", or session data with a specific
227expiration time. After the value expires, or the session expires or is
228deleted, the value is automatically removed.
229
230To add tempdata::
231
232 $expire = 300; // Expire in 5 minutes
233
234 $this->session->set_tempdata('item', 'value', $expire);
235
Michael5d4131b2013-09-30 12:22:29 +0300236You can also pass an array to ``set_tempdata()``::
dchill423169f262012-08-11 20:12:05 -0400237
238 $tempdata = array('newuser' => TRUE, 'message' => 'Thanks for joining!');
239
240 $this->session->set_tempdata($tempdata, '', $expire);
241
242.. note:: If the expiration is omitted or set to 0, the default expiration of
243 5 minutes will be used.
244
245To read a tempdata variable::
246
247 $this->session->tempdata('item');
248
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200249And of course, if you want to retrieve all existing tempdata::
250
251 $this->session->tempdata();
252
dchill423169f262012-08-11 20:12:05 -0400253If you need to remove a tempdata value before it expires,
Michael5d4131b2013-09-30 12:22:29 +0300254use ``unset_tempdata()``::
dchill423169f262012-08-11 20:12:05 -0400255
256 $this->session->unset_tempdata('item');
257
258Destroying a Session
259====================
260
261To clear the current session::
262
263 $this->session->sess_destroy();
264
265.. note:: This function should be the last one called, and even flash
266 variables will no longer be available. If you only want some items
Michael5d4131b2013-09-30 12:22:29 +0300267 destroyed and not all, use ``unset_userdata()``.
dchill423169f262012-08-11 20:12:05 -0400268
269Session Preferences
270===================
271
272You'll find the following Session related preferences in your
Andrey Andreev2d5ebf72013-10-29 13:27:59 +0200273*application/config/config.php* file:
dchill423169f262012-08-11 20:12:05 -0400274
275=========================== =============== =========================== ==========================================================================
276Preference Default Options Description
277=========================== =============== =========================== ==========================================================================
278**sess_driver** cookie cookie/native/*custom* The initial session driver to load.
279**sess_valid_drivers** cookie, native None Additional valid drivers which may be loaded.
280**sess_cookie_name** ci_session None The name you want the session cookie saved as (data for Cookie driver or
281 session ID for Native driver).
282**sess_expiration** 7200 None The number of seconds you would like the session to last. The default
283 value is 2 hours (7200 seconds). If you would like a non-expiring
284 session set the value to zero: 0
285**sess_expire_on_close** FALSE TRUE/FALSE (boolean) Whether to cause the session to expire automatically when the browser
286 window is closed.
287**sess_encrypt_cookie** FALSE TRUE/FALSE (boolean) Whether to encrypt the session data (Cookie driver only).
288**sess_use_database** FALSE TRUE/FALSE (boolean) Whether to save the session data to a database. You must create the
289 table before enabling this option (Cookie driver only).
290**sess_table_name** ci_sessions Any valid SQL table name The name of the session database table (Cookie driver only).
291**sess_time_to_update** 300 Time in seconds This options controls how often the session class will regenerate itself
Andrey Andreev2d5ebf72013-10-29 13:27:59 +0200292 and create a new session ID. Setting it to 0 will disable session
293 ID regeneartion.
dchill423169f262012-08-11 20:12:05 -0400294**sess_match_ip** FALSE TRUE/FALSE (boolean) Whether to match the user's IP address when reading the session data.
295 Note that some ISPs dynamically changes the IP, so if you want a
296 non-expiring session you will likely set this to FALSE.
297**sess_match_useragent** TRUE TRUE/FALSE (boolean) Whether to match the User Agent when reading the session data.
298=========================== =============== =========================== ==========================================================================
299
300In addition to the values above, the cookie and native drivers apply the
301following configuration values shared by the :doc:`Input <input>` and
302:doc:`Security <security>` classes:
303
304=========================== =============== ==========================================================================
305Preference Default Description
306=========================== =============== ==========================================================================
307**cookie_prefix** '' Set a cookie name prefix in order to avoid name collisions
308**cookie_domain** '' The domain for which the session is applicable
309**cookie_path** / The path to which the session is applicable
310=========================== =============== ==========================================================================
311
312Session Drivers
313===============
314
315By default, the `Cookie Driver`_ is loaded when a session is initialized.
316However, any valid driver may be selected with the $config['sess_driver']
317line in your config.php file.
318
319The session driver library comes with the cookie and native drivers
320installed, and `Custom Drivers`_ may also be installed by the user.
321
322Typically, only one driver will be used at a time, but CodeIgniter does
323support loading multiple drivers. If a specific valid driver is called, it
324will be automatically loaded. Or, an additional driver may be explicitly
Michael5d4131b2013-09-30 12:22:29 +0300325loaded by ``calling load_driver()``::
dchill423169f262012-08-11 20:12:05 -0400326
327 $this->session->load_driver('native');
328
329The Session library keeps track of the most recently selected driver to call
330for driver methods. Normally, session class methods are called directly on
331the parent class, as illustrated above. However, any methods called through
332a specific driver will select that driver before invoking the parent method.
333
334So, alternation between multiple drivers can be achieved by specifying which
335driver to use for each call::
336
337 $this->session->native->set_userdata('foo', 'bar');
338
339 $this->session->cookie->userdata('foo');
340
341 $this->session->native->unset_userdata('foo');
342
343Notice in the previous example that the *native* userdata value 'foo'
344would be set to 'bar', which would NOT be returned by the call for
345the *cookie* userdata 'foo', nor would the *cookie* value be unset by
346the call to unset the *native* 'foo' value. The drivers maintain independent
347sets of values, regardless of key names.
348
349A specific driver may also be explicitly selected for use by pursuant
Michael5d4131b2013-09-30 12:22:29 +0300350methods with the ``select_driver()`` call::
dchill423169f262012-08-11 20:12:05 -0400351
352 $this->session->select_driver('native');
353
354 $this->session->userdata('item'); // Uses the native driver
355
356Cookie Driver
357-------------
358
359The Cookie driver stores session information for each user as serialized
360(and optionally encrypted) data in a cookie. It can also store the session
361data in a database table for added security, as this permits the session ID
362in the user's cookie to be matched against the stored session ID. By default
363only the cookie is saved. If you choose to use the database option you'll
364need to create the session table as indicated below.
365
366If you have the encryption option enabled, the serialized array will be
367encrypted before being stored in the cookie, making the data highly
368secure and impervious to being read or altered by someone. More info
369regarding encryption can be :doc:`found here <encryption>`, although
370the Session class will take care of initializing and encrypting the data
371automatically.
372
373.. note:: Even if you are not using encrypted sessions, you must set
374 an :doc:`encryption key <./encryption>` in your config file which is used
375 to aid in preventing session data manipulation.
376
377.. note:: Cookies can only hold 4KB of data, so be careful not to exceed
378 the capacity. The encryption process in particular produces a longer
379 data string than the original so keep careful track of how much data you
380 are storing.
Derek Jones8ede1a22011-10-05 13:34:52 -0500381
382Saving Session Data to a Database
dchill423169f262012-08-11 20:12:05 -0400383^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Derek Jones8ede1a22011-10-05 13:34:52 -0500384
385While the session data array stored in the user's cookie contains a
386Session ID, unless you store session data in a database there is no way
387to validate it. For some applications that require little or no
388security, session ID validation may not be needed, but if your
389application requires security, validation is mandatory. Otherwise, an
390old session could be restored by a user modifying their cookies.
391
392When session data is available in a database, every time a valid session
393is found in the user's cookie, a database query is performed to match
394it. If the session ID does not match, the session is destroyed. Session
395IDs can never be updated, they can only be generated when a new session
396is created.
397
398In order to store sessions, you must first create a database table for
399this purpose. Here is the basic prototype (for MySQL) required by the
Derek Jones4b83d912011-10-05 15:42:30 -0500400session class::
Derek Jones8ede1a22011-10-05 13:34:52 -0500401
Derek Jones4b83d912011-10-05 15:42:30 -0500402 CREATE TABLE IF NOT EXISTS `ci_sessions` (
403 session_id varchar(40) DEFAULT '0' NOT NULL,
Andrey Andreev5a257182012-06-10 06:18:14 +0300404 ip_address varchar(45) DEFAULT '0' NOT NULL,
Derek Jones4b83d912011-10-05 15:42:30 -0500405 user_agent varchar(120) NOT NULL,
406 last_activity int(10) unsigned DEFAULT 0 NOT NULL,
407 user_data text NOT NULL,
Andrey Andreeve2afc882012-11-01 01:35:34 +0200408 PRIMARY KEY (session_id, ip_address, user_agent),
Derek Jones4b83d912011-10-05 15:42:30 -0500409 KEY `last_activity_idx` (`last_activity`)
410 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500411
Andrey Andreev69e1b4f2014-01-07 17:29:25 +0200412Or if you're using PostgreSQL::
413
414 CREATE TABLE ci_sessions (
415 session_id varchar(40) DEFAULT '0' NOT NULL,
416 ip_address varchar(45) DEFAULT '0' NOT NULL,
417 user_agent varchar(120) NOT NULL,
418 last_activity bigint DEFAULT 0 NOT NULL,
419 user_data text NOT NULL,
420 PRIMARY KEY (session_id)
421 );
422
423 CREATE INDEX last_activity_idx ON ci_sessions(last_activity);
424
Derek Jones8ede1a22011-10-05 13:34:52 -0500425.. note:: By default the table is called ci_sessions, but you can name
426 it anything you want as long as you update the
Andrey Andreev69e1b4f2014-01-07 17:29:25 +0200427 *application/config/config.php* file so that it contains the name
428 you have chosen. Once you have created your database table you
429 can enable the database option in your config.php file as follows::
Derek Jones8ede1a22011-10-05 13:34:52 -0500430
431 $config['sess_use_database'] = TRUE;
432
433 Once enabled, the Session class will store session data in the DB.
434
435 Make sure you've specified the table name in your config file as well::
436
437 $config['sess_table_name'] = 'ci_sessions';
438
dchill423169f262012-08-11 20:12:05 -0400439.. note:: The Cookie driver has built-in garbage collection which clears
Derek Jones8ede1a22011-10-05 13:34:52 -0500440 out expired sessions so you do not need to write your own routine to do
441 it.
442
dchill423169f262012-08-11 20:12:05 -0400443Native Driver
444-------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500445
dchill423169f262012-08-11 20:12:05 -0400446The Native driver relies on native PHP sessions to store data in the
447$_SESSION superglobal array. All stored values continue to be available
448through $_SESSION, but flash- and temp- data items carry special prefixes.
Derek Jones8ede1a22011-10-05 13:34:52 -0500449
dchill423169f262012-08-11 20:12:05 -0400450Custom Drivers
451--------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500452
dchill423169f262012-08-11 20:12:05 -0400453You may also :doc:`create your own <../general/creating_drivers>` custom
454session drivers. A session driver basically manages an array of name/value
455pairs with some sort of storage mechanism.
Derek Jones8ede1a22011-10-05 13:34:52 -0500456
Michael5d4131b2013-09-30 12:22:29 +0300457To make a new driver, extend CI_Session_driver. Overload the ``initialize()``
dchill423169f262012-08-11 20:12:05 -0400458method and read or create session data. Then implement a save handler to
459write changed data to storage (sess_save), a destroy handler to remove
dchill42b3816b72012-08-13 09:47:58 -0400460deleted data (sess_destroy), a regenerate handler to make a new session ID
461(sess_regenerate), and an access handler to expose the data (get_userdata).
462Your initial class might look like::
Derek Jones8ede1a22011-10-05 13:34:52 -0500463
dchill423169f262012-08-11 20:12:05 -0400464 class CI_Session_custom extends CI_Session_driver {
Andrey Andreevb57b2ad2014-01-03 12:02:08 +0200465
dchill423169f262012-08-11 20:12:05 -0400466 protected function initialize()
467 {
468 // Read existing session data or create a new one
469 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500470
dchill423169f262012-08-11 20:12:05 -0400471 public function sess_save()
472 {
473 // Save current data to storage
474 }
475
476 public function sess_destroy()
477 {
478 // Destroy the current session and clean up storage
479 }
480
481 public function sess_regenerate()
482 {
483 // Create new session ID
484 }
485
486 public function &get_userdata()
487 {
488 // Return a reference to your userdata array
489 }
Andrey Andreevb57b2ad2014-01-03 12:02:08 +0200490
dchill423169f262012-08-11 20:12:05 -0400491 }
492
Michael5d4131b2013-09-30 12:22:29 +0300493Notice that ``get_userdata()`` returns a reference so the parent library is
dchill423169f262012-08-11 20:12:05 -0400494accessing the same array the driver object is using. This saves memory
495and avoids synchronization issues during usage.
496
497Put your driver in the libraries/Session/drivers folder anywhere in your
498package paths. This includes the application directory, the system directory,
Michael5d4131b2013-09-30 12:22:29 +0300499or any path you add with ``$CI->load->add_package_path()``. Your driver must be
dchill423169f262012-08-11 20:12:05 -0400500named CI_Session_<name>, and your filename must be Session_<name>.php,
501preferably also capitalized, such as::
502
503 CI_Session_foo in libraries/Session/drivers/Session_foo.php
504
505Then specify the driver by setting 'sess_driver' in your config.php file or as a
506parameter when loading the CI_Session object::
507
508 $config['sess_driver'] = 'foo';
509
510OR::
511
512 $CI->load->driver('session', array('sess_driver' => 'foo'));
513
514The driver specified by 'sess_driver' is automatically included as a valid
515driver. However, if you want to make a custom driver available as an option
516without making it the initially loaded driver, set 'sess_valid_drivers' in
517your config.php file to an array including your driver name::
518
519 $config['sess_valid_drivers'] = array('sess_driver');
Michael1c7438f2013-10-06 15:21:21 +0300520
Michael1c7438f2013-10-06 15:21:21 +0300521***************
522Class Reference
523***************
524
525.. class:: CI_Session
526
Michaele0a631c2013-10-20 10:40:51 +0300527 .. method:: load_driver($driver)
528
Andrey Andreev28c2c972014-02-08 04:27:48 +0200529 :param string $driver: Driver name
530 :returns: Instance of currently loaded session driver
531 :rtype: mixed
Michaele0a631c2013-10-20 10:40:51 +0300532
533 Loads a session storage driver
534
535 .. method:: select_driver($driver)
536
Andrey Andreev28c2c972014-02-08 04:27:48 +0200537 :param string $driver: Driver name
538 :rtype: void
Michaele0a631c2013-10-20 10:40:51 +0300539
540 Selects default session storage driver.
541
542 .. method:: sess_destroy()
543
Andrey Andreev28c2c972014-02-08 04:27:48 +0200544 :rtype: void
545
Michaele0a631c2013-10-20 10:40:51 +0300546 Destroys current session
Michaele0a631c2013-10-20 10:40:51 +0300547
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200548 .. note:: This method should be the last one called, and even flash
549 variables will no longer be available after it is used.
Connor Tumbleson75b3fb22014-01-11 06:58:43 -0600550 If you only want some items destroyed and not all, use
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200551 ``unset_userdata()``.
Michaele0a631c2013-10-20 10:40:51 +0300552
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200553 .. method:: sess_regenerate([$destroy = FALSE])
554
Andrey Andreev28c2c972014-02-08 04:27:48 +0200555 :param bool $destroy: Whether to destroy session data
556 :rtype: void
Michaele0a631c2013-10-20 10:40:51 +0300557
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200558 Regenerate the current session data.
Michaele0a631c2013-10-20 10:40:51 +0300559
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200560 .. method:: userdata([$item = NULL])
Michaele0a631c2013-10-20 10:40:51 +0300561
Andrey Andreev28c2c972014-02-08 04:27:48 +0200562 :param string $item: Session item name
563 :returns: Item value if found, NULL if not or an array of all userdata if $item parameter is not used
564 :rtype: mixed
Michaele0a631c2013-10-20 10:40:51 +0300565
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200566 If no parameter is passed, it will return an associative array of all existing userdata.
567
568 Otherwise returns a string containing the value of the passed item or NULL if the item is not found.
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200569 Example::
Michaele0a631c2013-10-20 10:40:51 +0300570
571 $this->session->userdata('user');
572 //returns example@example.com considering the set_userdata example.
573
574 .. method:: all_userdata()
575
Andrey Andreev28c2c972014-02-08 04:27:48 +0200576 :returns: An array of all userdata
577 :rtype: array
Michaele0a631c2013-10-20 10:40:51 +0300578
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200579 Returns an array with all of the session userdata items.
Michaele0a631c2013-10-20 10:40:51 +0300580
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200581 .. note:: This method is DEPRECATED. Use ``userdata()`` with no parameters instead.
582
583 .. method:: &get_userdata()
Michaele0a631c2013-10-20 10:40:51 +0300584
Andrey Andreev28c2c972014-02-08 04:27:48 +0200585 :returns: A reference to the userdata array
586 :rtype: &array
Michaele0a631c2013-10-20 10:40:51 +0300587
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200588 Returns a reference to the userdata array.
Michael1c7438f2013-10-06 15:21:21 +0300589
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200590 .. method:: set_userdata($newdata[, $newval = ''])
Michael1c7438f2013-10-06 15:21:21 +0300591
Andrey Andreev28c2c972014-02-08 04:27:48 +0200592 :param mixed $newdata: Item name or array of items
593 :param mixed $newval: Item value or empty string (not required if $newdata is array)
594 :rtype: void
Michael1c7438f2013-10-06 15:21:21 +0300595
596 Sets items into session example usages::
597
598 $this->session->set_userdata('user', 'example@example.com');
599 // adds item user with value example@example.com to the session
600
601 $this->session->set_userdata(array('user'=>'example@example.com'));
602 // does the same as the above example - adds item user with value example@example.com to the session
603
Michael1c7438f2013-10-06 15:21:21 +0300604 .. method:: unset_userdata($item)
605
Andrey Andreev28c2c972014-02-08 04:27:48 +0200606 :param mixed $item: Item name or an array containing multiple items
607 :rtype: void
Michael1c7438f2013-10-06 15:21:21 +0300608
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200609 Unsets previously set items from the session. Example::
Michael1c7438f2013-10-06 15:21:21 +0300610
611 $this->session->unset_userdata('user');
612 //unsets 'user' from session data.
613
614 $this->session->unset_userdata(array('user', 'useremail'));
Michaele0a631c2013-10-20 10:40:51 +0300615 //unsets both 'user' and 'useremail' from the session data.
Michael1c7438f2013-10-06 15:21:21 +0300616
617 .. method:: has_userdata($item)
618
Andrey Andreev28c2c972014-02-08 04:27:48 +0200619 :param string $item: Item name
620 :returns: TRUE if item exists, FALSE if not
621 :rtype: bool
Michael1c7438f2013-10-06 15:21:21 +0300622
623 Checks if an item exists in the session.
624
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200625 .. method:: flashdata([$item = NULL])
626
Andrey Andreev28c2c972014-02-08 04:27:48 +0200627 :param string $item: Flashdata item name
628 :returns: Item value if found, NULL if not or an array of all flashdata if $item parameter is not used
629 :rtype: mixed
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200630
631 If no parameter is passed, it will return an associative array of all existing flashdata.
632
633 Otherwise returns a string containing the value of the passed item or NULL if the item is not found.
634 Example::
635
636 $this->session->flashdata('message');
637 //returns 'Test message.' considering the set_flashdata example.
638
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200639 .. method:: set_flashdata($newdata[, $newval = ''])
Michael1c7438f2013-10-06 15:21:21 +0300640
Andrey Andreev28c2c972014-02-08 04:27:48 +0200641 :param mixed $newdata: Item name or an array of items
642 :param mixed $newval: Item value or empty string (not required if $newdata is array)
643 :rtype: void
Michael1c7438f2013-10-06 15:21:21 +0300644
645 Sets items into session flashdata example usages::
646
647 $this->session->set_flashdata('message', 'Test message.');
648 // adds item 'message' with value 'Test message.' to the session flashdata
649
650 $this->session->set_flashdata(array('message'=>'Test message.'));
651 // does the same as the above example - adds item 'message' with value 'Test message.'
652 to the session flashdata
653
Michaele0a631c2013-10-20 10:40:51 +0300654 .. method:: keep_flashdata($item)
655
Andrey Andreev28c2c972014-02-08 04:27:48 +0200656 :param mixed $item: Item name or an array containing multiple flashdata items
657 :rtype: void
Michaele0a631c2013-10-20 10:40:51 +0300658
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200659 Keeps items into flashdata for one more request.
Michaele0a631c2013-10-20 10:40:51 +0300660
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200661 .. method:: tempdata([$item = NULL])
Michael1c7438f2013-10-06 15:21:21 +0300662
Andrey Andreev28c2c972014-02-08 04:27:48 +0200663 :param string $item: Tempdata item name
664 :returns: Item value if found, NULL if not or an array of all tempdata if $item parameter is not used
665 :rtype: mixed
Michael1c7438f2013-10-06 15:21:21 +0300666
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200667 If no parameter is passed, it will return an associative array of all existing tempdata.
668
669 Otherwise returns a string containing the value of the passed item or NULL if the item is not found.
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200670 Example::
Michael1c7438f2013-10-06 15:21:21 +0300671
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200672 $this->session->tempdata('message');
673 //returns 'Test message.' considering the set_tempdata example.
Michael1c7438f2013-10-06 15:21:21 +0300674
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200675 .. method:: set_tempdata($newdata[, $newval = ''[, $expire = 0]])
Michael1c7438f2013-10-06 15:21:21 +0300676
Andrey Andreev28c2c972014-02-08 04:27:48 +0200677 :param mixed $newdata: Item name or array containing multiple items
678 :param string $newval: Item value or empty string (not required if $newdata is array)
679 :param int $expire: Lifetime in seconds (0 for default)
680 :rtype: void
Michael1c7438f2013-10-06 15:21:21 +0300681
682 Sets items into session tempdata example::
683
684 $this->session->set_tempdata('message', 'Test message.', '60');
685 // adds item 'message' with value 'Test message.' to the session tempdata for 60 seconds
686
687 $this->session->set_tempdata(array('message'=>'Test message.'));
688 // does the same as the above example - adds item 'message' with value 'Test message.'
689 to the session tempdata for the default value of
690
Michael1c7438f2013-10-06 15:21:21 +0300691 .. method:: unset_tempdata($item)
692
Andrey Andreev28c2c972014-02-08 04:27:48 +0200693 :param mixed $item: Item name or an array containing multiple items
694 :rtype: void
Michael1c7438f2013-10-06 15:21:21 +0300695
Andrey Andreev1ee5a992014-01-06 13:08:47 +0200696 Unsets previously set items from tempdata. Example::
Michael1c7438f2013-10-06 15:21:21 +0300697
698 $this->session->unset_tempdata('user');
699 //unsets 'user' from tempdata.
700
701 $this->session->unset_tempdata(array('user', 'useremail'));
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200702 //unsets both 'user' and 'useremail' from the tempdata.