blob: 2034ed2b0b27a60da32297c655114980ac0e5d49 [file] [log] [blame]
Andrey Andreev54e71b82015-01-19 13:57:05 +02001###############
2Session Library
3###############
Derek Jones8ede1a22011-10-05 13:34:52 -05004
5The Session class permits you maintain a user's "state" and track their
Andrey Andreev973a6542015-01-19 13:25:24 +02006activity while they browse your site.
7
8CodeIgniter comes with a few session storage drivers:
9
10 - files (default; file-system based)
11 - database
12 - redis
13 - memcached
14
15In addition, you may create your own, custom session drivers based on other
16kinds of storage, while still taking advantage of the features of the
17Session class.
Derek Jones8ede1a22011-10-05 13:34:52 -050018
Andrey Andreevcc042092014-01-03 17:08:27 +020019.. contents::
20 :local:
21
22.. raw:: html
23
24 <div class="custom-index container"></div>
25
Andrey Andreevde1fe7d2014-01-20 17:06:16 +020026***********************
27Using the Session Class
28***********************
29
Derek Jones8ede1a22011-10-05 13:34:52 -050030Initializing a Session
31======================
32
Andrey Andreev973a6542015-01-19 13:25:24 +020033Sessions will typically run globally with each page load, so the Session
34class should either be initialized in your :doc:`controller
35<../general/controllers>` constructors, or it can be :doc:`auto-loaded
36<../general/autoloader>` by the system.
37For the most part the session class will run unattended in the background,
38so simply initializing the class will cause it to read, create, and update
39sessions when necessary.
Derek Jones8ede1a22011-10-05 13:34:52 -050040
41To initialize the Session class manually in your controller constructor,
Andrey Andreev973a6542015-01-19 13:25:24 +020042use the ``$this->load->library()`` method::
Derek Jones8ede1a22011-10-05 13:34:52 -050043
Andrey Andreev973a6542015-01-19 13:25:24 +020044 $this->load->library('session');
Derek Jones8ede1a22011-10-05 13:34:52 -050045
Michael5d4131b2013-09-30 12:22:29 +030046Once loaded, the Sessions library object will be available using::
47
Andrey Andreevcc042092014-01-03 17:08:27 +020048 $this->session
Derek Jones8ede1a22011-10-05 13:34:52 -050049
vlakoffa2dee7d2015-01-20 04:22:29 +010050.. important:: Because the :doc:`Loader Class </libraries/loader>` is instantiated
Andrey Andreev973a6542015-01-19 13:25:24 +020051 by CodeIgniter's base controller, make sure to call
52 ``parent::__construct()`` before trying to load a library from
53 inside a controller constructor.
54
Derek Jones8ede1a22011-10-05 13:34:52 -050055How do Sessions work?
56=====================
57
58When a page is loaded, the session class will check to see if valid
Andrey Andreev973a6542015-01-19 13:25:24 +020059session cookie is sent by the user's browser. If a sessions cookie does
60**not** exist (or if it doesn't match one stored on the server or has
61expired) a new session will be created and saved.
62
63If a valid session does exist, its information will be updated. With each
64update, the session ID may be regenerated if configured to do so.
Derek Jones8ede1a22011-10-05 13:34:52 -050065
66It's important for you to understand that once initialized, the Session
67class runs automatically. There is nothing you need to do to cause the
Andrey Andreev973a6542015-01-19 13:25:24 +020068above behavior to happen. You can, as you'll see below, work with session
69data, but the process of reading, writing, and updating a session is
70automatic.
71
72.. note:: Under CLI, the Session library will automatically halt itself,
73 as this is a concept based entirely on the HTTP protocol.
Derek Jones8ede1a22011-10-05 13:34:52 -050074
Andrey Andreeveccac6e2015-02-04 16:04:31 +020075A note about concurrency
76------------------------
77
78Unless you're developing a website with heavy AJAX usage, you can skip this
79section. If you are, however, and if you're experiencing performance
80issues, then this note is exactly what you're looking for.
81
82Sessions in previous versions of CodeIgniter didn't implement locking,
83which meant that two HTTP requests using the same session could run exactly
84at the same time. To use a more appropriate technical term - requests were
85non-blocking.
86
87However, non-blocking requests in the context of sessions also means
88unsafe, because modifications to session data (or session ID regeneration)
89in one request can interfere with the execution of a second, concurrent
90request. This detail was at the root of many issues and the main reason why
91CodeIgniter 3.0 has a completely re-written Session library.
92
93Why are we telling you this? Because it is likely that after trying to
94find the reason for your performance issues, you may conclude that locking
95is the issue and therefore look into how to remove the locks ...
96
97DO NOT DO THAT! Removing locks would be **wrong** and it will cause you
98more problems!
99
100Locking is not the issue, it is a solution. Your issue is that you still
101have the session open, while you've already processed it and therefore no
102longer need it. So, what you need is to close the session for the
103current request after you no longer need it.
104
105Long story short - call ``session_write_close()`` once you no longer need
106anything to do with session variables.
107
Derek Jones8ede1a22011-10-05 13:34:52 -0500108What is Session Data?
109=====================
110
Andrey Andreev973a6542015-01-19 13:25:24 +0200111Session data is simply an array associated with a particular session ID
112(cookie).
Derek Jones8ede1a22011-10-05 13:34:52 -0500113
Andrey Andreev973a6542015-01-19 13:25:24 +0200114If you've used sessions in PHP before, you should be familiar with PHP's
115`$_SESSION superglobal <http://php.net/manual/en/reserved.variables.session.php>`_
116(if not, please read the content on that link).
Derek Jones8ede1a22011-10-05 13:34:52 -0500117
Andrey Andreev973a6542015-01-19 13:25:24 +0200118CodeIgniter gives access to its session data through the same means, as it
119uses the session handlers' mechanism provided by PHP. Using session data is
120as simple as manipulating (read, set and unset values) the ``$_SESSION``
121array.
Derek Jones8ede1a22011-10-05 13:34:52 -0500122
Andrey Andreev973a6542015-01-19 13:25:24 +0200123In addition, CodeIgniter also provides 2 special types of session data
124that are further explained below: flashdata and tempdata.
Derek Jones8ede1a22011-10-05 13:34:52 -0500125
Andrey Andreev973a6542015-01-19 13:25:24 +0200126.. note:: In previous versions, regular session data in CodeIgniter was
127 referred to as 'userdata'. Have this in mind if that term is used
128 elsewhere in the manual. Most of it is written to explain how
129 the custom 'userdata' methods work.
Derek Jones8ede1a22011-10-05 13:34:52 -0500130
131Retrieving Session Data
132=======================
133
Andrey Andreev973a6542015-01-19 13:25:24 +0200134Any piece of information from the session array is available through the
135``$_SESSION`` superglobal::
136
137 $_SESSION['item']
138
139Or through the magic getter::
140
141 $this->session->item
142
143And for backwards compatibility, through the ``userdata()`` method::
Derek Jones8ede1a22011-10-05 13:34:52 -0500144
145 $this->session->userdata('item');
146
Andrey Andreev973a6542015-01-19 13:25:24 +0200147Where item is the array key corresponding to the item you wish to fetch.
148For example, to assign a previously stored 'name' item to the ``$name``
149variable, you will do this::
Derek Jones8ede1a22011-10-05 13:34:52 -0500150
Andrey Andreev973a6542015-01-19 13:25:24 +0200151 $name = $_SESSION['name'];
Derek Jones8ede1a22011-10-05 13:34:52 -0500152
Andrey Andreev973a6542015-01-19 13:25:24 +0200153 // or:
154
155 $name = $this->session->name
156
157 // or:
158
159 $name = $this->session->userdata('name');
160
161.. note:: The ``userdata()`` method returns NULL if the item you are trying
162 to access does not exist.
Derek Jones8ede1a22011-10-05 13:34:52 -0500163
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200164If you want to retrieve all of the existing userdata, you can simply
Andrey Andreev973a6542015-01-19 13:25:24 +0200165omit the item key (magic getter only works for properties)::
166
167 $_SESSION
168
169 // or:
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200170
171 $this->session->userdata();
172
Andrey Andreev973a6542015-01-19 13:25:24 +0200173Adding Session Data
174===================
Derek Jones8ede1a22011-10-05 13:34:52 -0500175
176Let's say a particular user logs into your site. Once authenticated, you
Andrey Andreev973a6542015-01-19 13:25:24 +0200177could add their username and e-mail address to the session, making that
178data globally available to you without having to run a database query when
179you need it.
Derek Jones8ede1a22011-10-05 13:34:52 -0500180
Andrey Andreev973a6542015-01-19 13:25:24 +0200181You can simply assign data to the ``$_SESSION`` array, as with any other
182variable. Or as a property of ``$this->session``.
183
184Alternatively, the old method of assigning it as "userdata" is also
185available. That however passing an array containing your new data to the
186``set_userdata()`` method::
Derek Jones8ede1a22011-10-05 13:34:52 -0500187
188 $this->session->set_userdata($array);
189
Andrey Andreev973a6542015-01-19 13:25:24 +0200190Where ``$array`` is an associative array containing your new data. Here's
191an example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500192
Derek Jones4b83d912011-10-05 15:42:30 -0500193 $newdata = array(
Michael5d4131b2013-09-30 12:22:29 +0300194 'username' => 'johndoe',
195 'email' => 'johndoe@some-site.com',
196 'logged_in' => TRUE
197 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500198
Derek Jones4b83d912011-10-05 15:42:30 -0500199 $this->session->set_userdata($newdata);
Derek Jones8ede1a22011-10-05 13:34:52 -0500200
Michael5d4131b2013-09-30 12:22:29 +0300201If you want to add userdata one value at a time, ``set_userdata()`` also
Andrey Andreev973a6542015-01-19 13:25:24 +0200202supports this syntax::
Derek Jones8ede1a22011-10-05 13:34:52 -0500203
204 $this->session->set_userdata('some_name', 'some_value');
205
Andrey Andreev973a6542015-01-19 13:25:24 +0200206If you want to verify that a session value exists, simply check with
207``isset()``::
Derek Jones8ede1a22011-10-05 13:34:52 -0500208
Andrey Andreev973a6542015-01-19 13:25:24 +0200209 // returns FALSE if the 'some_name' item doesn't exist or is NULL,
210 // TRUE otherwise:
211 isset($_SESSION['some_name'])
212
213Or you can call ``has_userdata()``::
dchill423169f262012-08-11 20:12:05 -0400214
215 $this->session->has_userdata('some_name');
Derek Jones8ede1a22011-10-05 13:34:52 -0500216
Derek Jones8ede1a22011-10-05 13:34:52 -0500217Removing Session Data
218=====================
219
Andrey Andreev973a6542015-01-19 13:25:24 +0200220Just as with any other variable, unsetting a value in ``$_SESSION`` can be
221done through ``unset()``::
222
223 unset($_SESSION['some_name']);
224
225 // or multiple values:
226
227 unset(
228 $_SESSION['some_name'],
229 $_SESSION['another_name']
230 );
231
232Also, just as ``set_userdata()`` can be used to add information to a
233session, ``unset_userdata()`` can be used to remove it, by passing the
234session key. For example, if you wanted to remove 'some_name' from your
235session data array::
Derek Jones8ede1a22011-10-05 13:34:52 -0500236
237 $this->session->unset_userdata('some_name');
238
Andrey Andreevb339df32015-02-02 11:45:11 +0200239This method also accepts an array of item keys to unset::
Derek Jones8ede1a22011-10-05 13:34:52 -0500240
Andrey Andreevb339df32015-02-02 11:45:11 +0200241 $array_items = array('username', 'email');
Derek Jones4b83d912011-10-05 15:42:30 -0500242
243 $this->session->unset_userdata($array_items);
Derek Jones8ede1a22011-10-05 13:34:52 -0500244
Andrey Andreevb339df32015-02-02 11:45:11 +0200245.. note:: In previous versions, the ``unset_userdata()`` method used
246 to accept an associative array of ``key => 'dummy value'``
247 pairs. This is no longer supported.
248
Derek Jones8ede1a22011-10-05 13:34:52 -0500249Flashdata
250=========
251
252CodeIgniter supports "flashdata", or session data that will only be
Andrey Andreev973a6542015-01-19 13:25:24 +0200253available for the next request, and is then automatically cleared.
Derek Jones8ede1a22011-10-05 13:34:52 -0500254
Andrey Andreev973a6542015-01-19 13:25:24 +0200255This can be very useful, especially for one-time informational, error or
256status messages (for example: "Record 2 deleted").
257
258It should be noted that flashdata variables are regular session vars,
259only marked in a specific way under the '__ci_vars' key (please don't touch
260that one, you've been warned).
261
262To mark an existing item as "flashdata"::
263
264 $this->session->mark_as_flash('item');
265
266If you want to mark multiple items as flashdata, simply pass the keys as an
267array::
268
269 $this->session->mark_as_flash(array('item', 'item2'));
Derek Jones8ede1a22011-10-05 13:34:52 -0500270
271To add flashdata::
272
Andrey Andreev973a6542015-01-19 13:25:24 +0200273 $_SESSION['item'] = 'value';
274 $this->session->mark_as_flash('item');
Derek Jones8ede1a22011-10-05 13:34:52 -0500275
Andrey Andreev973a6542015-01-19 13:25:24 +0200276Or alternatively, using the ``set_flashdata()`` method::
277
278 $this->session->set_flashdata('item', 'value');
Derek Jones8ede1a22011-10-05 13:34:52 -0500279
Michael5d4131b2013-09-30 12:22:29 +0300280You can also pass an array to ``set_flashdata()``, in the same manner as
281``set_userdata()``.
Derek Jones8ede1a22011-10-05 13:34:52 -0500282
Andrey Andreev973a6542015-01-19 13:25:24 +0200283Reading flashdata variables is the same as reading regular session data
284through ``$_SESSION``::
285
286 $_SESSION['item']
287
288.. important:: The ``userdata()`` method will NOT return flashdata items.
289
290However, if you want to be sure that you're reading "flashdata" (and not
291any other kind), you can also use the ``flashdata()`` method::
Derek Jones8ede1a22011-10-05 13:34:52 -0500292
293 $this->session->flashdata('item');
Johnathan Croom4beca5c2012-11-23 18:32:46 -0700294
Andrey Andreev973a6542015-01-19 13:25:24 +0200295Or to get an array with all flashdata, simply omit the key parameter::
Mike Funk7c26fab2012-02-24 09:45:02 -0500296
Andrey Andreevecc260e2014-01-24 14:20:13 +0200297 $this->session->flashdata();
Derek Jones8ede1a22011-10-05 13:34:52 -0500298
Andrey Andreev973a6542015-01-19 13:25:24 +0200299.. note:: The ``flashdata()`` method returns NULL if the item cannot be
300 found.
Derek Jones8ede1a22011-10-05 13:34:52 -0500301
302If you find that you need to preserve a flashdata variable through an
Andrey Andreev973a6542015-01-19 13:25:24 +0200303additional request, you can do so using the ``keep_flashdata()`` method.
Johnathan Croom4beca5c2012-11-23 18:32:46 -0700304You can either pass a single item or an array of flashdata items to keep.
Derek Jones8ede1a22011-10-05 13:34:52 -0500305
306::
307
308 $this->session->keep_flashdata('item');
Johnathan Croom4beca5c2012-11-23 18:32:46 -0700309 $this->session->keep_flashdata(array('item1', 'item2', 'item3'));
Derek Jones8ede1a22011-10-05 13:34:52 -0500310
dchill423169f262012-08-11 20:12:05 -0400311Tempdata
312========
313
314CodeIgniter also supports "tempdata", or session data with a specific
315expiration time. After the value expires, or the session expires or is
316deleted, the value is automatically removed.
317
Andrey Andreev973a6542015-01-19 13:25:24 +0200318Similarly to flashdata, tempdata variables are regular session vars that
319are marked in a specific way under the '__ci_vars' key (again, don't touch
320that one).
321
322To mark an existing item as "tempdata", simply pass its key and expiry time
323(in seconds!) to the ``mark_as_temp()`` method::
324
325 // 'item' will be erased after 300 seconds
326 $this->session->mark_as_temp('item', 300);
327
328You can mark multiple items as tempdata in two ways, depending on whether
329you want them all to have the same expiry time or not::
330
331 // Both 'item' and 'item2' will expire after 300 seconds
332 $this->session->mark_as_temp(array('item', 'item2'), 300);
333
334 // 'item' will be erased after 300 seconds, while 'item2'
335 // will do so after only 240 seconds
336 $this->session->mark_as_temp(array(
337 'item' => 300,
338 'item2' => 240
339 ));
340
dchill423169f262012-08-11 20:12:05 -0400341To add tempdata::
342
Andrey Andreev973a6542015-01-19 13:25:24 +0200343 $_SESSION['item'] = 'value';
344 $this->session->mark_as_temp('item', 300); // Expire in 5 minutes
dchill423169f262012-08-11 20:12:05 -0400345
Andrey Andreev973a6542015-01-19 13:25:24 +0200346Or alternatively, using the ``set_tempdata()`` method::
347
348 $this->session->set_tempdata('item', 'value', 300);
dchill423169f262012-08-11 20:12:05 -0400349
Michael5d4131b2013-09-30 12:22:29 +0300350You can also pass an array to ``set_tempdata()``::
dchill423169f262012-08-11 20:12:05 -0400351
352 $tempdata = array('newuser' => TRUE, 'message' => 'Thanks for joining!');
353
Andrey Andreev973a6542015-01-19 13:25:24 +0200354 $this->session->set_tempdata($tempdata, NULL, $expire);
dchill423169f262012-08-11 20:12:05 -0400355
Andrey Andreev973a6542015-01-19 13:25:24 +0200356.. note:: If the expiration is omitted or set to 0, the default
357 time-to-live value of 300 seconds (or 5 minutes) will be used.
dchill423169f262012-08-11 20:12:05 -0400358
Andrey Andreev973a6542015-01-19 13:25:24 +0200359To read a tempdata variable, again you can just access it through the
360``$_SESSION`` superglobal array::
361
362 $_SESSION['item']
363
364.. important:: The ``userdata()`` method will NOT return tempdata items.
365
Master Yoda7762c592015-03-06 16:08:59 -0800366Or if you want to be sure that you're reading "tempdata" (and not any
Andrey Andreev973a6542015-01-19 13:25:24 +0200367other kind), you can also use the ``tempdata()`` method::
dchill423169f262012-08-11 20:12:05 -0400368
369 $this->session->tempdata('item');
370
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200371And of course, if you want to retrieve all existing tempdata::
372
373 $this->session->tempdata();
374
Andrey Andreev973a6542015-01-19 13:25:24 +0200375.. note:: The ``tempdata()`` method returns NULL if the item cannot be
376 found.
377
378If you need to remove a tempdata value before it expires, you can directly
379unset it from the ``$_SESSION`` array::
380
381 unset($_SESSION['item']);
382
383However, this won't remove the marker that makes this specific item to be
384tempdata (it will be invalidated on the next HTTP request), so if you
385intend to reuse that same key in the same request, you'd want to use
386``unset_tempdata()``::
dchill423169f262012-08-11 20:12:05 -0400387
388 $this->session->unset_tempdata('item');
389
390Destroying a Session
391====================
392
Andrey Andreev973a6542015-01-19 13:25:24 +0200393To clear the current session (for example, during a logout), you may
394simply use either PHP's `session_destroy() <http://php.net/session_destroy>`_
395function, or the ``sess_destroy()`` method. Both will work in exactly the
396same way::
397
398 session_destroy();
399
400 // or
dchill423169f262012-08-11 20:12:05 -0400401
402 $this->session->sess_destroy();
403
Andrey Andreev973a6542015-01-19 13:25:24 +0200404.. note:: This must be the last session-related operation that you do
405 during the same request. All session data (including flashdata and
406 tempdata) will be destroyed permanently and functions will be
407 unusable during the same request after you destroy the session.
408
409Accessing session metadata
410==========================
411
412In previous CodeIgniter versions, the session data array included 4 items
413by default: 'session_id', 'ip_address', 'user_agent', 'last_activity'.
414
415This was due to the specifics of how sessions worked, but is now no longer
416necessary with our new implementation. However, it may happen that your
417application relied on these values, so here are alternative methods of
Andrey Andreevd8e25e92015-01-19 22:03:57 +0200418accessing them:
Andrey Andreev973a6542015-01-19 13:25:24 +0200419
420 - session_id: ``session_id()``
421 - ip_address: ``$_SERVER['REMOTE_ADDR']``
422 - user_agent: ``$this->input->user_agent()`` (unused by sessions)
423 - last_activity: Depends on the storage, no straightforward way. Sorry!
dchill423169f262012-08-11 20:12:05 -0400424
425Session Preferences
426===================
427
Andrey Andreev973a6542015-01-19 13:25:24 +0200428CodeIgniter will usually make everything work out of the box. However,
429Sessions are a very sensitive component of any application, so some
430careful configuration must be done. Please take your time to consider
431all of the options and their effects.
dchill423169f262012-08-11 20:12:05 -0400432
Andrey Andreev973a6542015-01-19 13:25:24 +0200433You'll find the following Session related preferences in your
434**application/config/config.php** file:
435
Andrey Andreev789b1fe2015-02-07 19:30:30 +0200436============================ =============== ======================================== ============================================================================================
437Preference Default Options Description
438============================ =============== ======================================== ============================================================================================
439**sess_driver** files files/database/redis/memcached/*custom* The session storage driver to use.
440**sess_cookie_name** ci_session [A-Za-z\_-] characters only The name used for the session cookie.
441**sess_expiration** 7200 (2 hours) Time in seconds (integer) The number of seconds you would like the session to last.
442 If you would like a non-expiring session (until browser is closed) set the value to zero: 0
443**sess_save_path** NULL None Specifies the storage location, depends on the driver being used.
444**sess_match_ip** FALSE TRUE/FALSE (boolean) Whether to validate the user's IP address when reading the session cookie.
445 Note that some ISPs dynamically changes the IP, so if you want a non-expiring session you
446 will likely set this to FALSE.
447**sess_time_to_update** 300 Time in seconds (integer) This option controls how often the session class will regenerate itself and create a new
448 session ID. Setting it to 0 will disable session ID regeneration.
449**sess_regenerate_destroy** FALSE TRUE/FALSE (boolean) Whether to destroy session data associated with the old session ID when auto-regenerating
450 the session ID. When set to FALSE, the data will be later deleted by the garbage collector.
451============================ =============== ======================================== ============================================================================================
Andrey Andreev973a6542015-01-19 13:25:24 +0200452
453.. note:: As a last resort, the Session library will try to fetch PHP's
454 session related INI settings, as well as legacy CI settings such as
455 'sess_expire_on_close' when any of the above is not configured.
456 However, you should never rely on this behavior as it can cause
457 unexpected results or be changed in the future. Please configure
458 everything properly.
dchill423169f262012-08-11 20:12:05 -0400459
460In addition to the values above, the cookie and native drivers apply the
461following configuration values shared by the :doc:`Input <input>` and
462:doc:`Security <security>` classes:
463
Andrey Andreev973a6542015-01-19 13:25:24 +0200464================== =============== ===========================================================================
465Preference Default Description
466================== =============== ===========================================================================
467**cookie_domain** '' The domain for which the session is applicable
468**cookie_path** / The path to which the session is applicable
469**cookie_secure** FALSE Whether to create the session cookie only on encrypted (HTTPS) connections
470================== =============== ===========================================================================
471
472.. note:: The 'cookie_httponly' setting doesn't have an effect on sessions.
473 Instead the HttpOnly parameter is always enabled, for security
474 reasons. Additionaly, the 'cookie_prefix' setting is completely
475 ignored.
dchill423169f262012-08-11 20:12:05 -0400476
477Session Drivers
478===============
479
Andrey Andreev973a6542015-01-19 13:25:24 +0200480As already mentioned, the Session library comes with 4 drivers, or storage
481engines, that you can use:
dchill423169f262012-08-11 20:12:05 -0400482
Andrey Andreev973a6542015-01-19 13:25:24 +0200483 - files
484 - database
485 - redis
486 - memcached
dchill423169f262012-08-11 20:12:05 -0400487
Andrey Andreev973a6542015-01-19 13:25:24 +0200488By default, the `Files Driver`_ will be used when a session is initialized,
489because it is the most safe choice and is expected to work everywhere
490(virtually every environment has a file system).
dchill423169f262012-08-11 20:12:05 -0400491
Andrey Andreev973a6542015-01-19 13:25:24 +0200492However, any other driver may be selected via the ``$config['sess_driver']``
493line in your **application/config/config.php** file, if you chose to do so.
494Have it in mind though, every driver has different caveats, so be sure to
495get yourself familiar with them (below) before you make that choice.
dchill423169f262012-08-11 20:12:05 -0400496
Andrey Andreev973a6542015-01-19 13:25:24 +0200497In addition, you may also create and use `Custom Drivers`_, if the ones
498provided by default don't satisfy your use case.
dchill423169f262012-08-11 20:12:05 -0400499
Andrey Andreev973a6542015-01-19 13:25:24 +0200500.. note:: In previous CodeIgniter versions, a different, "cookie driver"
501 was the only option and we have received negative feedback on not
502 providing that option. While we do listen to feedback from the
503 community, we want to warn you that it was dropped because it is
504 **unsafe** and we advise you NOT to try to replicate it via a
505 custom driver.
dchill423169f262012-08-11 20:12:05 -0400506
Andrey Andreev973a6542015-01-19 13:25:24 +0200507Files Driver
508------------
dchill423169f262012-08-11 20:12:05 -0400509
Andrey Andreev973a6542015-01-19 13:25:24 +0200510The 'files' driver uses your file system for storing session data.
dchill423169f262012-08-11 20:12:05 -0400511
Andrey Andreev973a6542015-01-19 13:25:24 +0200512It can safely be said that it works exactly like PHP's own default session
513implementation, but in case this is an important detail for you, have it
514mind that it is in fact not the same code and it has some limitations
515(and advantages).
dchill423169f262012-08-11 20:12:05 -0400516
Andrey Andreev973a6542015-01-19 13:25:24 +0200517To be more specific, it doesn't support PHP's `directory level and mode
518formats used in session.save_path
519<http://php.net/manual/en/session.configuration.php#ini.session.save-path>`_,
520and it has most of the options hard-coded for safety. Instead, only
521absolute paths are supported for ``$config['sess_save_path']``.
dchill423169f262012-08-11 20:12:05 -0400522
Andrey Andreev973a6542015-01-19 13:25:24 +0200523Another important thing that you should know, is to make sure that you
524don't use a publicly-readable or shared directory for storing your session
525files. Make sure that *only you* have access to see the contents of your
526chosen *sess_save_path* directory. Otherwise, anybody who can do that, can
527also steal any of the current sessions (also known as "session fixation"
528attack).
dchill423169f262012-08-11 20:12:05 -0400529
Andrey Andreev973a6542015-01-19 13:25:24 +0200530On UNIX-like operating systems, this is usually achieved by setting the
Andrey Andreev6e8a2022015-02-03 10:53:05 +02005310700 mode permissions on that directory via the `chmod` command, which
Andrey Andreev973a6542015-01-19 13:25:24 +0200532allows only the directory's owner to perform read and write operations on
533it. But be careful because the system user *running* the script is usually
534not your own, but something like 'www-data' instead, so only setting those
535permissions will probable break your application.
dchill423169f262012-08-11 20:12:05 -0400536
Andrey Andreev973a6542015-01-19 13:25:24 +0200537Instead, you should do something like this, depending on your environment
538::
dchill423169f262012-08-11 20:12:05 -0400539
Andrey Andreev973a6542015-01-19 13:25:24 +0200540 mkdir /<path to your application directory>/sessions/
Andrey Andreev6e8a2022015-02-03 10:53:05 +0200541 chmod 0700 /<path to your application directory>/sessions/
Andrey Andreev973a6542015-01-19 13:25:24 +0200542 chown www-data /<path to your application directory>/sessions/
dchill423169f262012-08-11 20:12:05 -0400543
Andrey Andreev973a6542015-01-19 13:25:24 +0200544Bonus Tip
545^^^^^^^^^
dchill423169f262012-08-11 20:12:05 -0400546
Andrey Andreev973a6542015-01-19 13:25:24 +0200547Some of you will probably opt to choose another session driver because
548file storage is usually slower. This is only half true.
dchill423169f262012-08-11 20:12:05 -0400549
Andrey Andreev973a6542015-01-19 13:25:24 +0200550A very basic test will probably trick you into believing that an SQL
551database is faster, but in 99% of the cases, this is only true while you
552only have a few current sessions. As the sessions count and server loads
553increase - which is the time when it matters - the file system will
554consistently outperform almost all relational database setups.
dchill423169f262012-08-11 20:12:05 -0400555
Andrey Andreev973a6542015-01-19 13:25:24 +0200556In addition, if performance is your only concern, you may want to look
557into using `tmpfs <http://eddmann.com/posts/storing-php-sessions-file-caches-in-memory-using-tmpfs/>`_,
558(warning: external resource), which can make your sessions blazing fast.
Derek Jones8ede1a22011-10-05 13:34:52 -0500559
Andrey Andreev973a6542015-01-19 13:25:24 +0200560Database Driver
561---------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500562
Andrey Andreev973a6542015-01-19 13:25:24 +0200563The 'database' driver uses a relational database such as MySQL or
564PostgreSQL to store sessions. This is a popular choice among many users,
565because it allows the developer easy access to the session data within
566an application - it is just another table in your database.
Derek Jones8ede1a22011-10-05 13:34:52 -0500567
Andrey Andreev973a6542015-01-19 13:25:24 +0200568However, there are some conditions that must be met:
Derek Jones8ede1a22011-10-05 13:34:52 -0500569
Andrey Andreev973a6542015-01-19 13:25:24 +0200570 - Only your **default** database connection (or the one that you access
571 as ``$this->db`` from your controllers) can be used.
Andrey Andreev973a6542015-01-19 13:25:24 +0200572 - You must have the :doc:`Query Builder </database/query_builder>`
573 enabled.
Andrey Andreev737a5662015-03-21 12:41:38 +0200574 - You can NOT use a persistent connection.
575 - You can NOT use a connection with the *cache_on* setting enabled.
Derek Jones8ede1a22011-10-05 13:34:52 -0500576
Andrey Andreev973a6542015-01-19 13:25:24 +0200577In order to use the 'database' session driver, you must also create this
578table that we already mentioned and then set it as your
579``$config['sess_save_path']`` value.
580For example, if you would like to use 'ci_sessions' as your table name,
Andrey Andreeveccac6e2015-02-04 16:04:31 +0200581you would do this::
Andrey Andreev973a6542015-01-19 13:25:24 +0200582
583 $config['sess_driver'] = 'database';
584 $config['sess_save_path'] = 'ci_sessions';
585
586.. note:: If you've upgraded from a previous version of CodeIgniter and
587 you don't have 'sess_save_path' configured, then the Session
588 library will look for the old 'sess_table_name' setting and use
589 it instead. Please don't rely on this behavior as it will get
590 removed in the future.
591
592And then of course, create the database table ...
593
594For MySQL::
595
596 CREATE TABLE IF NOT EXISTS `ci_sessions` (
Andrey Andreev3053b812015-01-20 17:31:29 +0200597 `id` varchar(40) NOT NULL,
Andrey Andreev973a6542015-01-19 13:25:24 +0200598 `ip_address` varchar(45) NOT NULL,
599 `timestamp` int(10) unsigned DEFAULT 0 NOT NULL,
Andrey Andreevff7563e2015-02-18 21:38:01 +0200600 `data` blob NOT NULL,
Andrey Andreev9f4eda72015-01-26 11:58:07 +0200601 PRIMARY KEY (id),
Andrey Andreev973a6542015-01-19 13:25:24 +0200602 KEY `ci_sessions_timestamp` (`timestamp`)
Derek Jones4b83d912011-10-05 15:42:30 -0500603 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500604
Andrey Andreev973a6542015-01-19 13:25:24 +0200605For PostgreSQL::
Andrey Andreev69e1b4f2014-01-07 17:29:25 +0200606
Andrey Andreev973a6542015-01-19 13:25:24 +0200607 CREATE TABLE "ci_sessions" (
Andrey Andreev3053b812015-01-20 17:31:29 +0200608 "id" varchar(40) NOT NULL,
Andrey Andreev973a6542015-01-19 13:25:24 +0200609 "ip_address" varchar(45) NOT NULL,
610 "timestamp" bigint DEFAULT 0 NOT NULL,
611 "data" text DEFAULT '' NOT NULL,
Master Yoda08a92bc2015-01-26 00:46:57 -0800612 PRIMARY KEY ("id")
Andrey Andreev69e1b4f2014-01-07 17:29:25 +0200613 );
614
Andrey Andreev973a6542015-01-19 13:25:24 +0200615 CREATE INDEX "ci_sessions_timestamp" ON "ci_sessions" ("timestamp");
Andrey Andreev69e1b4f2014-01-07 17:29:25 +0200616
Andrey Andreev973a6542015-01-19 13:25:24 +0200617However, if you want to turn on the *sess_match_ip* setting, you should
618also do the following, after creating the table::
Derek Jones8ede1a22011-10-05 13:34:52 -0500619
Andrey Andreev973a6542015-01-19 13:25:24 +0200620 // Works both on MySQL and PostgreSQL
Andrey Andreev3053b812015-01-20 17:31:29 +0200621 ALTER TABLE ci_sessions ADD CONSTRAINT ci_sessions_id_ip UNIQUE (id, ip_address);
Derek Jones8ede1a22011-10-05 13:34:52 -0500622
Andrey Andreev973a6542015-01-19 13:25:24 +0200623.. important:: Only MySQL and PostgreSQL databases are officially
Andrey Andreev052c02f2015-01-19 13:30:30 +0200624 supported, due to lack of advisory locking mechanisms on other
625 platforms. Using sessions without locks can cause all sorts of
626 problems, especially with heavy usage of AJAX, and we will not
627 support such cases. Use ``session_write_close()`` after you've
628 done processing session data if you're having performance
629 issues.
Derek Jones8ede1a22011-10-05 13:34:52 -0500630
Andrey Andreev973a6542015-01-19 13:25:24 +0200631Redis Driver
632------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500633
Andrey Andreevb7cea9c2015-02-14 21:16:48 +0200634.. note:: Since Redis doesn't have a locking mechanism exposed, locks for
635 this driver are emulated by a separate value that is kept for up
Andrey Andreeve1a5bb32015-03-04 13:33:39 +0200636 to 300 seconds.
Andrey Andreevb7cea9c2015-02-14 21:16:48 +0200637
Andrey Andreev973a6542015-01-19 13:25:24 +0200638Redis is a storage engine typically used for caching and popular because
639of its high performance, which is also probably your reason to use the
640'redis' session driver.
Derek Jones8ede1a22011-10-05 13:34:52 -0500641
Andrey Andreev973a6542015-01-19 13:25:24 +0200642The downside is that it is not as ubiquitous as relational databases and
Master Yodabd2a7e42015-03-25 02:36:31 -0700643requires the `phpredis <https://github.com/phpredis/phpredis>`_ PHP
Andrey Andreev973a6542015-01-19 13:25:24 +0200644extension to be installed on your system, and that one doesn't come
645bundled with PHP.
646Chances are, you're only be using the 'redis' driver only if you're already
647both familiar with Redis and using it for other purposes.
Derek Jones8ede1a22011-10-05 13:34:52 -0500648
Andrey Andreev973a6542015-01-19 13:25:24 +0200649Just as with the 'files' and 'database' drivers, you must also configure
650the storage location for your sessions via the
651``$config['sess_save_path']`` setting.
652The format here is a bit different and complicated at the same time. It is
653best explained by the *phpredis* extension's README file, so we'll simply
654link you to it:
Derek Jones8ede1a22011-10-05 13:34:52 -0500655
Andrey Andreev973a6542015-01-19 13:25:24 +0200656 https://github.com/phpredis/phpredis#php-session-handler
657
658.. warning:: CodeIgniter's Session library does NOT use the actual 'redis'
659 ``session.save_handler``. Take note **only** of the path format in
660 the link above.
661
662For the most common case however, a simple ``host:port`` pair should be
663sufficient::
664
665 $config['sess_driver'] = 'redis';
666 $config['sess_save_path'] = 'tcp://localhost:6379';
667
668Memcached Driver
669----------------
670
Andrey Andreevb7cea9c2015-02-14 21:16:48 +0200671.. note:: Since Memcache doesn't have a locking mechanism exposed, locks
672 for this driver are emulated by a separate value that is kept for
Andrey Andreeve1a5bb32015-03-04 13:33:39 +0200673 up to 300 seconds.
Andrey Andreevb7cea9c2015-02-14 21:16:48 +0200674
Andrey Andreev973a6542015-01-19 13:25:24 +0200675The 'memcached' driver is very similar to the 'redis' one in all of its
676properties, except perhaps for availability, because PHP's `Memcached
677<http://php.net/memcached>`_ extension is distributed via PECL and some
678Linux distrubutions make it available as an easy to install package.
679
680Other than that, and without any intentional bias towards Redis, there's
681not much different to be said about Memcached - it is also a popular
682product that is usually used for caching and famed for its speed.
683
684However, it is worth noting that the only guarantee given by Memcached
685is that setting value X to expire after Y seconds will result in it being
686deleted after Y seconds have passed (but not necessarily that it won't
687expire earlier than that time). This happens very rarely, but should be
688considered as it may result in loss of sessions.
689
690The ``$config['sess_save_path']`` format is fairly straightforward here,
691being just a ``host:port`` pair::
692
693 $config['sess_driver'] = 'memcached';
694 $config['sess_save_path'] = 'localhost:11211';
695
696Bonus Tip
697^^^^^^^^^
698
699Multi-server configuration with an optional *weight* parameter as the
700third colon-separated (``:weight``) value is also supported, but we have
701to note that we haven't tested if that is reliable.
702
703If you want to experiment with this feature (on your own risk), simply
704separate the multiple server paths with commas::
705
706 // localhost will be given higher priority (5) here,
707 // compared to 192.0.2.1 with a weight of 1.
708 $config['sess_save_path'] = 'localhost:11211:5,192.0.2.1:11211:1';
Derek Jones8ede1a22011-10-05 13:34:52 -0500709
dchill423169f262012-08-11 20:12:05 -0400710Custom Drivers
711--------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500712
Andrey Andreev973a6542015-01-19 13:25:24 +0200713You may also create your own, custom session drivers. However, have it in
714mind that this is typically not an easy task, as it takes a lot of
715knowledge to do it properly.
Derek Jones8ede1a22011-10-05 13:34:52 -0500716
Andrey Andreev973a6542015-01-19 13:25:24 +0200717You need to know not only how sessions work in general, but also how they
718work specifically in PHP, how the underlying storage mechanism works, how
719to handle concurrency, avoid deadlocks (but NOT through lack of locks) and
720last but not least - how to handle the potential security issues, which
721is far from trivial.
Derek Jones8ede1a22011-10-05 13:34:52 -0500722
Andrey Andreev973a6542015-01-19 13:25:24 +0200723Long story short - if you don't know how to do that already in raw PHP,
724you shouldn't be trying to do it within CodeIgniter either. You've been
725warned.
Andrey Andreevb57b2ad2014-01-03 12:02:08 +0200726
Andrey Andreev973a6542015-01-19 13:25:24 +0200727If you only want to add some extra functionality to your sessions, just
728extend the base Session class, which is a lot more easier. Read the
729:doc:`Creating Libraries <../general/creating_libraries>` article to
730learn how to do that.
731
732Now, to the point - there are three general rules that you must follow
733when creating a session driver for CodeIgniter:
734
735 - Put your driver's file under **application/libraries/Session/drivers/**
736 and follow the naming conventions used by the Session class.
737
738 For example, if you were to create a 'dummy' driver, you would have
739 a ``Session_dummy_driver`` class name, that is declared in
740 *application/libraries/Session/drivers/Session_dummy_driver.php*.
741
742 - Extend the ``CI_Session_driver`` class.
743
744 This is just a basic class with a few internal helper methods. It is
745 also extendable like any other library, if you really need to do that,
746 but we are not going to explain how ... if you're familiar with how
747 class extensions/overrides work in CI, then you already know how to do
748 it. If not, well, you shouldn't be doing it in the first place.
749
750
751 - Implement the `SessionHandlerInterface
752 <http://php.net/sessionhandlerinterface>`_ interface.
753
754 .. note:: You may notice that ``SessionHandlerInterface`` is provided
755 by PHP since version 5.4.0. CodeIgniter will automatically declare
756 the same interface if you're running an older PHP version.
757
758 The link will explain why and how.
759
760So, based on our 'dummy' driver example above, you'd end up with something
761like this::
762
763 // application/libraries/Session/drivers/Session_dummy_driver.php:
764
765 class CI_Session_dummy_driver extends CI_Session_driver implements SessionHandlerInterface
766 {
767
768 public function __construct(&$params)
dchill423169f262012-08-11 20:12:05 -0400769 {
Andrey Andreev973a6542015-01-19 13:25:24 +0200770 // DO NOT forget this
771 parent::__construct($params);
772
773 // Configuration & other initializations
dchill423169f262012-08-11 20:12:05 -0400774 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500775
Andrey Andreev973a6542015-01-19 13:25:24 +0200776 public function open($save_path, $name)
dchill423169f262012-08-11 20:12:05 -0400777 {
Andrey Andreev973a6542015-01-19 13:25:24 +0200778 // Initialize storage mechanism (connection)
dchill423169f262012-08-11 20:12:05 -0400779 }
780
Andrey Andreev973a6542015-01-19 13:25:24 +0200781 public function read($session_id)
dchill423169f262012-08-11 20:12:05 -0400782 {
Andrey Andreev973a6542015-01-19 13:25:24 +0200783 // Read session data (if exists), acquire locks
dchill423169f262012-08-11 20:12:05 -0400784 }
785
Andrey Andreev973a6542015-01-19 13:25:24 +0200786 public function write($session_id, $session_data)
dchill423169f262012-08-11 20:12:05 -0400787 {
Andrey Andreev973a6542015-01-19 13:25:24 +0200788 // Create / update session data (it might not exist!)
dchill423169f262012-08-11 20:12:05 -0400789 }
790
Andrey Andreev973a6542015-01-19 13:25:24 +0200791 public function close()
dchill423169f262012-08-11 20:12:05 -0400792 {
Andrey Andreev973a6542015-01-19 13:25:24 +0200793 // Free locks, close connections / streams / etc.
794 }
795
796 public function destroy($session_id)
797 {
798 // Call close() method & destroy data for current session (order may differ)
799 }
800
801 public function gc($maxlifetime)
802 {
803 // Erase data for expired sessions
dchill423169f262012-08-11 20:12:05 -0400804 }
Andrey Andreevb57b2ad2014-01-03 12:02:08 +0200805
dchill423169f262012-08-11 20:12:05 -0400806 }
807
Andrey Andreev973a6542015-01-19 13:25:24 +0200808If you've done everything properly, you can now set your *sess_driver*
809configuration value to 'dummy' and use your own driver. Congratulations!
Michael1c7438f2013-10-06 15:21:21 +0300810
Michael1c7438f2013-10-06 15:21:21 +0300811***************
812Class Reference
813***************
814
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200815.. php:class:: CI_Session
Michael1c7438f2013-10-06 15:21:21 +0300816
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200817 .. php:method:: userdata([$key = NULL])
Michaele0a631c2013-10-20 10:40:51 +0300818
Andrey Andreev973a6542015-01-19 13:25:24 +0200819 :param mixed $key: Session item key or NULL
820 :returns: Value of the specified item key, or an array of all userdata
Andrey Andreev28c2c972014-02-08 04:27:48 +0200821 :rtype: mixed
Michaele0a631c2013-10-20 10:40:51 +0300822
Andrey Andreev973a6542015-01-19 13:25:24 +0200823 Gets the value for a specific ``$_SESSION`` item, or an
824 array of all "userdata" items if not key was specified.
825
826 .. note:: This is a legacy method kept only for backwards
827 compatibility with older applications. You should
828 directly access ``$_SESSION`` instead.
Michaele0a631c2013-10-20 10:40:51 +0300829
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200830 .. php:method:: all_userdata()
Michaele0a631c2013-10-20 10:40:51 +0300831
Andrey Andreev28c2c972014-02-08 04:27:48 +0200832 :returns: An array of all userdata
833 :rtype: array
Michaele0a631c2013-10-20 10:40:51 +0300834
Andrey Andreev973a6542015-01-19 13:25:24 +0200835 Returns an array containing all "userdata" items.
Michaele0a631c2013-10-20 10:40:51 +0300836
Andrey Andreev973a6542015-01-19 13:25:24 +0200837 .. note:: This method is DEPRECATED. Use ``userdata()``
838 with no parameters instead.
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200839
Andrey Andreevf4cb8f92015-03-19 11:54:47 +0200840 .. php:method:: &get_userdata()
Michaele0a631c2013-10-20 10:40:51 +0300841
Andrey Andreev973a6542015-01-19 13:25:24 +0200842 :returns: A reference to ``$_SESSION``
843 :rtype: array
Michaele0a631c2013-10-20 10:40:51 +0300844
Andrey Andreev973a6542015-01-19 13:25:24 +0200845 Returns a reference to the ``$_SESSION`` array.
Michael1c7438f2013-10-06 15:21:21 +0300846
Andrey Andreev973a6542015-01-19 13:25:24 +0200847 .. note:: This is a legacy method kept only for backwards
848 compatibility with older applications.
Michael1c7438f2013-10-06 15:21:21 +0300849
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200850 .. php:method:: has_userdata($key)
Michael1c7438f2013-10-06 15:21:21 +0300851
Andrey Andreev973a6542015-01-19 13:25:24 +0200852 :param string $key: Session item key
853 :returns: TRUE if the specified key exists, FALSE if not
Andrey Andreev28c2c972014-02-08 04:27:48 +0200854 :rtype: bool
Michael1c7438f2013-10-06 15:21:21 +0300855
Andrey Andreev973a6542015-01-19 13:25:24 +0200856 Checks if an item exists in ``$_SESSION``.
Michael1c7438f2013-10-06 15:21:21 +0300857
Andrey Andreev973a6542015-01-19 13:25:24 +0200858 .. note:: This is a legacy method kept only for backwards
859 compatibility with older applications. It is just
860 an alias for ``isset($_SESSION[$key])`` - please
861 use that instead.
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200862
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200863 .. php:method:: set_userdata($data[, $value = NULL])
Andrey Andreev973a6542015-01-19 13:25:24 +0200864
865 :param mixed $data: An array of key/value pairs to set as session data, or the key for a single item
866 :param mixed $value: The value to set for a specific session item, if $data is a key
867 :rtype: void
868
869 Assigns data to the ``$_SESSION`` superglobal.
870
871 .. note:: This is a legacy method kept only for backwards
872 compatibility with older applications.
873
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200874 .. php:method:: unset_userdata($key)
Andrey Andreev973a6542015-01-19 13:25:24 +0200875
876 :param mixed $key: Key for the session data item to unset, or an array of multiple keys
877 :rtype: void
878
879 Unsets the specified key(s) from the ``$_SESSION``
880 superglobal.
881
882 .. note:: This is a legacy method kept only for backwards
883 compatibility with older applications. It is just
884 an alias for ``unset($_SESSION[$key])`` - please
885 use that instead.
886
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200887 .. php:method:: mark_as_flash($key)
Andrey Andreev973a6542015-01-19 13:25:24 +0200888
889 :param mixed $key: Key to mark as flashdata, or an array of multiple keys
890 :returns: TRUE on success, FALSE on failure
891 :rtype: bool
892
893 Marks a ``$_SESSION`` item key (or multiple ones) as
894 "flashdata".
895
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200896 .. php:method:: get_flash_keys()
Andrey Andreev973a6542015-01-19 13:25:24 +0200897
898 :returns: Array containing the keys of all "flashdata" items.
899 :rtype: array
900
901 Gets a list of all ``$_SESSION`` that have been marked as
902 "flashdata".
903
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200904 .. php:method:: umark_flash($key)
Andrey Andreev973a6542015-01-19 13:25:24 +0200905
906 :param mixed $key: Key to be un-marked as flashdata, or an array of multiple keys
907 :rtype: void
908
909 Unmarks a ``$_SESSION`` item key (or multiple ones) as
910 "flashdata".
911
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200912 .. php:method:: flashdata([$key = NULL])
Andrey Andreev973a6542015-01-19 13:25:24 +0200913
914 :param mixed $key: Flashdata item key or NULL
915 :returns: Value of the specified item key, or an array of all flashdata
Andrey Andreev28c2c972014-02-08 04:27:48 +0200916 :rtype: mixed
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200917
Andrey Andreev973a6542015-01-19 13:25:24 +0200918 Gets the value for a specific ``$_SESSION`` item that has
919 been marked as "flashdata", or an array of all "flashdata"
920 items if no key was specified.
921
922 .. note:: This is a legacy method kept only for backwards
923 compatibility with older applications. You should
924 directly access ``$_SESSION`` instead.
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200925
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200926 .. php:method:: keep_flashdata($key)
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200927
Andrey Andreev973a6542015-01-19 13:25:24 +0200928 :param mixed $key: Flashdata key to keep, or an array of multiple keys
vlakoffa2dee7d2015-01-20 04:22:29 +0100929 :returns: TRUE on success, FALSE on failure
Andrey Andreev973a6542015-01-19 13:25:24 +0200930 :rtype: bool
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200931
Andrey Andreev973a6542015-01-19 13:25:24 +0200932 Retains the specified session data key(s) as "flashdata"
933 through the next request.
Michael1c7438f2013-10-06 15:21:21 +0300934
Andrey Andreev973a6542015-01-19 13:25:24 +0200935 .. note:: This is a legacy method kept only for backwards
936 compatibility with older applications. It is just
937 an alias for the ``mark_as_flash()`` method.
Michael1c7438f2013-10-06 15:21:21 +0300938
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200939 .. php:method:: set_flashdata($data[, $value = NULL])
Michael1c7438f2013-10-06 15:21:21 +0300940
Andrey Andreev973a6542015-01-19 13:25:24 +0200941 :param mixed $data: An array of key/value pairs to set as flashdata, or the key for a single item
942 :param mixed $value: The value to set for a specific session item, if $data is a key
Andrey Andreev28c2c972014-02-08 04:27:48 +0200943 :rtype: void
Michaele0a631c2013-10-20 10:40:51 +0300944
Andrey Andreev973a6542015-01-19 13:25:24 +0200945 Assigns data to the ``$_SESSION`` superglobal and marks it
946 as "flashdata".
Michaele0a631c2013-10-20 10:40:51 +0300947
Andrey Andreev973a6542015-01-19 13:25:24 +0200948 .. note:: This is a legacy method kept only for backwards
949 compatibility with older applications.
Michael1c7438f2013-10-06 15:21:21 +0300950
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200951 .. php:method:: mark_as_temp($key[, $ttl = 300])
Andrey Andreev973a6542015-01-19 13:25:24 +0200952
953 :param mixed $key: Key to mark as tempdata, or an array of multiple keys
954 :param int $ttl: Time-to-live value for the tempdata, in seconds
955 :returns: TRUE on success, FALSE on failure
956 :rtype: bool
957
958 Marks a ``$_SESSION`` item key (or multiple ones) as
959 "tempdata".
960
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200961 .. php:method:: get_temp_keys()
Andrey Andreev973a6542015-01-19 13:25:24 +0200962
963 :returns: Array containing the keys of all "tempdata" items.
964 :rtype: array
965
966 Gets a list of all ``$_SESSION`` that have been marked as
967 "tempdata".
968
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200969 .. php:method:: umark_temp($key)
Andrey Andreev973a6542015-01-19 13:25:24 +0200970
971 :param mixed $key: Key to be un-marked as tempdata, or an array of multiple keys
972 :rtype: void
973
974 Unmarks a ``$_SESSION`` item key (or multiple ones) as
975 "tempdata".
976
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200977 .. php:method:: tempdata([$key = NULL])
Andrey Andreev973a6542015-01-19 13:25:24 +0200978
979 :param mixed $key: Tempdata item key or NULL
980 :returns: Value of the specified item key, or an array of all tempdata
Andrey Andreev28c2c972014-02-08 04:27:48 +0200981 :rtype: mixed
Michael1c7438f2013-10-06 15:21:21 +0300982
Andrey Andreev973a6542015-01-19 13:25:24 +0200983 Gets the value for a specific ``$_SESSION`` item that has
984 been marked as "tempdata", or an array of all "tempdata"
985 items if no key was specified.
986
987 .. note:: This is a legacy method kept only for backwards
988 compatibility with older applications. You should
989 directly access ``$_SESSION`` instead.
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200990
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200991 .. php:method:: set_tempdata($data[, $value = NULL])
Michael1c7438f2013-10-06 15:21:21 +0300992
Andrey Andreev973a6542015-01-19 13:25:24 +0200993 :param mixed $data: An array of key/value pairs to set as tempdata, or the key for a single item
994 :param mixed $value: The value to set for a specific session item, if $data is a key
995 :param int $ttl: Time-to-live value for the tempdata item(s), in seconds
Andrey Andreev28c2c972014-02-08 04:27:48 +0200996 :rtype: void
Michael1c7438f2013-10-06 15:21:21 +0300997
Andrey Andreev973a6542015-01-19 13:25:24 +0200998 Assigns data to the ``$_SESSION`` superglobal and marks it
999 as "tempdata".
Michael1c7438f2013-10-06 15:21:21 +03001000
Andrey Andreev973a6542015-01-19 13:25:24 +02001001 .. note:: This is a legacy method kept only for backwards
1002 compatibility with older applications.
Michael1c7438f2013-10-06 15:21:21 +03001003
Andrey Andreevcd3d9db2015-02-02 13:41:01 +02001004 .. php:method:: sess_regenerate([$destroy = FALSE])
Michael1c7438f2013-10-06 15:21:21 +03001005
Andrey Andreev973a6542015-01-19 13:25:24 +02001006 :param bool $destroy: Whether to destroy session data
Andrey Andreev28c2c972014-02-08 04:27:48 +02001007 :rtype: void
Michael1c7438f2013-10-06 15:21:21 +03001008
Andrey Andreev973a6542015-01-19 13:25:24 +02001009 Regenerate session ID, optionally destroying the current
1010 session's data.
Michael1c7438f2013-10-06 15:21:21 +03001011
Andrey Andreev973a6542015-01-19 13:25:24 +02001012 .. note:: This method is just an alias for PHP's native
1013 `session_regenerate_id()
1014 <http://php.net/session_regenerate_id>`_ function.
Michael1c7438f2013-10-06 15:21:21 +03001015
Andrey Andreevcd3d9db2015-02-02 13:41:01 +02001016 .. php:method:: sess_destroy()
Andrey Andreev973a6542015-01-19 13:25:24 +02001017
1018 :rtype: void
1019
1020 Destroys the current session.
1021
1022 .. note:: This must be the *last* session-related function
1023 that you call. All session data will be lost after
1024 you do that.
1025
1026 .. note:: This method is just an alias for PHP's native
1027 `session_destroy()
1028 <http://php.net/session_destroy>`_ function.
1029
Andrey Andreevcd3d9db2015-02-02 13:41:01 +02001030 .. php:method:: __get($key)
Andrey Andreev973a6542015-01-19 13:25:24 +02001031
1032 :param string $key: Session item key
1033 :returns: The requested session data item, or NULL if it doesn't exist
1034 :rtype: mixed
1035
1036 A magic method that allows you to use
1037 ``$this->session->item`` instead of ``$_SESSION['item']``,
1038 if that's what you prefer.
1039
1040 It will also return the session ID by calling
1041 ``session_id()`` if you try to access
1042 ``$this->session->session_id``.
1043
Andrey Andreevcd3d9db2015-02-02 13:41:01 +02001044 .. php:method:: __set($key, $value)
Andrey Andreev973a6542015-01-19 13:25:24 +02001045
1046 :param string $key: Session item key
1047 :param mixed $value: Value to assign to the session item key
1048 :returns: void
1049
1050 A magic method that allows you to assign items to
1051 ``$_SESSION`` by accessing them as ``$this->session``
1052 properties::
1053
1054 $this->session->foo = 'bar';
1055
1056 // Results in:
Andrey Andreevf4cb8f92015-03-19 11:54:47 +02001057 // $_SESSION['foo'] = 'bar';