blob: 994dc2e080f6cd6855a7288ca75e06860e705bcb [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
Andrey Andreev71d8f722017-01-17 12:01:00 +0200474 reasons. Additionally, the 'cookie_prefix' setting is completely
Andrey Andreev973a6542015-01-19 13:25:24 +0200475 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 Andreev6c6ee1a2016-10-22 16:33:06 +0300597 `id` varchar(128) 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 Andreev973a6542015-01-19 13:25:24 +0200601 KEY `ci_sessions_timestamp` (`timestamp`)
Derek Jones4b83d912011-10-05 15:42:30 -0500602 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500603
Andrey Andreev973a6542015-01-19 13:25:24 +0200604For PostgreSQL::
Andrey Andreev69e1b4f2014-01-07 17:29:25 +0200605
Andrey Andreev973a6542015-01-19 13:25:24 +0200606 CREATE TABLE "ci_sessions" (
Andrey Andreev6c6ee1a2016-10-22 16:33:06 +0300607 "id" varchar(128) NOT NULL,
Andrey Andreev973a6542015-01-19 13:25:24 +0200608 "ip_address" varchar(45) NOT NULL,
609 "timestamp" bigint DEFAULT 0 NOT NULL,
Andrey Andreev20573bd2015-09-01 12:46:06 +0300610 "data" text DEFAULT '' NOT NULL
Andrey Andreev69e1b4f2014-01-07 17:29:25 +0200611 );
612
Andrey Andreev973a6542015-01-19 13:25:24 +0200613 CREATE INDEX "ci_sessions_timestamp" ON "ci_sessions" ("timestamp");
Andrey Andreev69e1b4f2014-01-07 17:29:25 +0200614
Andrey Andreev20573bd2015-09-01 12:46:06 +0300615You will also need to add a PRIMARY KEY **depending on your 'sess_match_ip'
616setting**. The examples below work both on MySQL and PostgreSQL::
Derek Jones8ede1a22011-10-05 13:34:52 -0500617
Andrey Andreev20573bd2015-09-01 12:46:06 +0300618 // When sess_match_ip = TRUE
619 ALTER TABLE ci_sessions ADD PRIMARY KEY (id, ip_address);
620
621 // When sess_match_ip = FALSE
622 ALTER TABLE ci_sessions ADD PRIMARY KEY (id);
623
624 // To drop a previously created primary key (use when changing the setting)
625 ALTER TABLE ci_sessions DROP PRIMARY KEY;
626
Derek Jones8ede1a22011-10-05 13:34:52 -0500627
Andrey Andreev973a6542015-01-19 13:25:24 +0200628.. important:: Only MySQL and PostgreSQL databases are officially
Andrey Andreev052c02f2015-01-19 13:30:30 +0200629 supported, due to lack of advisory locking mechanisms on other
630 platforms. Using sessions without locks can cause all sorts of
631 problems, especially with heavy usage of AJAX, and we will not
632 support such cases. Use ``session_write_close()`` after you've
633 done processing session data if you're having performance
634 issues.
Derek Jones8ede1a22011-10-05 13:34:52 -0500635
Andrey Andreev973a6542015-01-19 13:25:24 +0200636Redis Driver
637------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500638
Andrey Andreevb7cea9c2015-02-14 21:16:48 +0200639.. note:: Since Redis doesn't have a locking mechanism exposed, locks for
640 this driver are emulated by a separate value that is kept for up
Andrey Andreeve1a5bb32015-03-04 13:33:39 +0200641 to 300 seconds.
Andrey Andreevb7cea9c2015-02-14 21:16:48 +0200642
Andrey Andreev973a6542015-01-19 13:25:24 +0200643Redis is a storage engine typically used for caching and popular because
644of its high performance, which is also probably your reason to use the
645'redis' session driver.
Derek Jones8ede1a22011-10-05 13:34:52 -0500646
Andrey Andreev973a6542015-01-19 13:25:24 +0200647The downside is that it is not as ubiquitous as relational databases and
Master Yodabd2a7e42015-03-25 02:36:31 -0700648requires the `phpredis <https://github.com/phpredis/phpredis>`_ PHP
Andrey Andreev973a6542015-01-19 13:25:24 +0200649extension to be installed on your system, and that one doesn't come
650bundled with PHP.
651Chances are, you're only be using the 'redis' driver only if you're already
652both familiar with Redis and using it for other purposes.
Derek Jones8ede1a22011-10-05 13:34:52 -0500653
Andrey Andreev973a6542015-01-19 13:25:24 +0200654Just as with the 'files' and 'database' drivers, you must also configure
655the storage location for your sessions via the
656``$config['sess_save_path']`` setting.
657The format here is a bit different and complicated at the same time. It is
658best explained by the *phpredis* extension's README file, so we'll simply
659link you to it:
Derek Jones8ede1a22011-10-05 13:34:52 -0500660
Andrey Andreev973a6542015-01-19 13:25:24 +0200661 https://github.com/phpredis/phpredis#php-session-handler
662
663.. warning:: CodeIgniter's Session library does NOT use the actual 'redis'
664 ``session.save_handler``. Take note **only** of the path format in
665 the link above.
666
667For the most common case however, a simple ``host:port`` pair should be
668sufficient::
669
670 $config['sess_driver'] = 'redis';
671 $config['sess_save_path'] = 'tcp://localhost:6379';
672
673Memcached Driver
674----------------
675
Andrey Andreevb7cea9c2015-02-14 21:16:48 +0200676.. note:: Since Memcache doesn't have a locking mechanism exposed, locks
677 for this driver are emulated by a separate value that is kept for
Andrey Andreeve1a5bb32015-03-04 13:33:39 +0200678 up to 300 seconds.
Andrey Andreevb7cea9c2015-02-14 21:16:48 +0200679
Andrey Andreev973a6542015-01-19 13:25:24 +0200680The 'memcached' driver is very similar to the 'redis' one in all of its
681properties, except perhaps for availability, because PHP's `Memcached
682<http://php.net/memcached>`_ extension is distributed via PECL and some
683Linux distrubutions make it available as an easy to install package.
684
685Other than that, and without any intentional bias towards Redis, there's
686not much different to be said about Memcached - it is also a popular
687product that is usually used for caching and famed for its speed.
688
689However, it is worth noting that the only guarantee given by Memcached
690is that setting value X to expire after Y seconds will result in it being
691deleted after Y seconds have passed (but not necessarily that it won't
692expire earlier than that time). This happens very rarely, but should be
693considered as it may result in loss of sessions.
694
695The ``$config['sess_save_path']`` format is fairly straightforward here,
696being just a ``host:port`` pair::
697
698 $config['sess_driver'] = 'memcached';
699 $config['sess_save_path'] = 'localhost:11211';
700
701Bonus Tip
702^^^^^^^^^
703
704Multi-server configuration with an optional *weight* parameter as the
705third colon-separated (``:weight``) value is also supported, but we have
706to note that we haven't tested if that is reliable.
707
708If you want to experiment with this feature (on your own risk), simply
709separate the multiple server paths with commas::
710
711 // localhost will be given higher priority (5) here,
712 // compared to 192.0.2.1 with a weight of 1.
713 $config['sess_save_path'] = 'localhost:11211:5,192.0.2.1:11211:1';
Derek Jones8ede1a22011-10-05 13:34:52 -0500714
dchill423169f262012-08-11 20:12:05 -0400715Custom Drivers
716--------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500717
Andrey Andreev973a6542015-01-19 13:25:24 +0200718You may also create your own, custom session drivers. However, have it in
719mind that this is typically not an easy task, as it takes a lot of
720knowledge to do it properly.
Derek Jones8ede1a22011-10-05 13:34:52 -0500721
Andrey Andreev973a6542015-01-19 13:25:24 +0200722You need to know not only how sessions work in general, but also how they
723work specifically in PHP, how the underlying storage mechanism works, how
724to handle concurrency, avoid deadlocks (but NOT through lack of locks) and
725last but not least - how to handle the potential security issues, which
726is far from trivial.
Derek Jones8ede1a22011-10-05 13:34:52 -0500727
Andrey Andreev973a6542015-01-19 13:25:24 +0200728Long story short - if you don't know how to do that already in raw PHP,
729you shouldn't be trying to do it within CodeIgniter either. You've been
730warned.
Andrey Andreevb57b2ad2014-01-03 12:02:08 +0200731
Andrey Andreev973a6542015-01-19 13:25:24 +0200732If you only want to add some extra functionality to your sessions, just
733extend the base Session class, which is a lot more easier. Read the
734:doc:`Creating Libraries <../general/creating_libraries>` article to
735learn how to do that.
736
737Now, to the point - there are three general rules that you must follow
738when creating a session driver for CodeIgniter:
739
740 - Put your driver's file under **application/libraries/Session/drivers/**
741 and follow the naming conventions used by the Session class.
742
743 For example, if you were to create a 'dummy' driver, you would have
744 a ``Session_dummy_driver`` class name, that is declared in
745 *application/libraries/Session/drivers/Session_dummy_driver.php*.
746
747 - Extend the ``CI_Session_driver`` class.
748
749 This is just a basic class with a few internal helper methods. It is
750 also extendable like any other library, if you really need to do that,
751 but we are not going to explain how ... if you're familiar with how
752 class extensions/overrides work in CI, then you already know how to do
753 it. If not, well, you shouldn't be doing it in the first place.
754
755
756 - Implement the `SessionHandlerInterface
757 <http://php.net/sessionhandlerinterface>`_ interface.
758
759 .. note:: You may notice that ``SessionHandlerInterface`` is provided
Andrey Andreev4a567782017-11-20 09:58:03 +0200760 by PHP since version 5.4.0. CodeIgniter will automatically declare
761 the same interface if you're running an older PHP version.
Andrey Andreev973a6542015-01-19 13:25:24 +0200762
763 The link will explain why and how.
764
765So, based on our 'dummy' driver example above, you'd end up with something
766like this::
767
768 // application/libraries/Session/drivers/Session_dummy_driver.php:
769
770 class CI_Session_dummy_driver extends CI_Session_driver implements SessionHandlerInterface
771 {
772
773 public function __construct(&$params)
dchill423169f262012-08-11 20:12:05 -0400774 {
Andrey Andreev973a6542015-01-19 13:25:24 +0200775 // DO NOT forget this
776 parent::__construct($params);
777
778 // Configuration & other initializations
dchill423169f262012-08-11 20:12:05 -0400779 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500780
Andrey Andreev973a6542015-01-19 13:25:24 +0200781 public function open($save_path, $name)
dchill423169f262012-08-11 20:12:05 -0400782 {
Andrey Andreev973a6542015-01-19 13:25:24 +0200783 // Initialize storage mechanism (connection)
dchill423169f262012-08-11 20:12:05 -0400784 }
785
Andrey Andreev973a6542015-01-19 13:25:24 +0200786 public function read($session_id)
dchill423169f262012-08-11 20:12:05 -0400787 {
Andrey Andreev973a6542015-01-19 13:25:24 +0200788 // Read session data (if exists), acquire locks
dchill423169f262012-08-11 20:12:05 -0400789 }
790
Andrey Andreev973a6542015-01-19 13:25:24 +0200791 public function write($session_id, $session_data)
dchill423169f262012-08-11 20:12:05 -0400792 {
Andrey Andreev973a6542015-01-19 13:25:24 +0200793 // Create / update session data (it might not exist!)
dchill423169f262012-08-11 20:12:05 -0400794 }
795
Andrey Andreev973a6542015-01-19 13:25:24 +0200796 public function close()
dchill423169f262012-08-11 20:12:05 -0400797 {
Andrey Andreev973a6542015-01-19 13:25:24 +0200798 // Free locks, close connections / streams / etc.
799 }
800
801 public function destroy($session_id)
802 {
803 // Call close() method & destroy data for current session (order may differ)
804 }
805
806 public function gc($maxlifetime)
807 {
808 // Erase data for expired sessions
dchill423169f262012-08-11 20:12:05 -0400809 }
Andrey Andreevb57b2ad2014-01-03 12:02:08 +0200810
dchill423169f262012-08-11 20:12:05 -0400811 }
812
Andrey Andreev973a6542015-01-19 13:25:24 +0200813If you've done everything properly, you can now set your *sess_driver*
814configuration value to 'dummy' and use your own driver. Congratulations!
Michael1c7438f2013-10-06 15:21:21 +0300815
Michael1c7438f2013-10-06 15:21:21 +0300816***************
817Class Reference
818***************
819
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200820.. php:class:: CI_Session
Michael1c7438f2013-10-06 15:21:21 +0300821
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200822 .. php:method:: userdata([$key = NULL])
Michaele0a631c2013-10-20 10:40:51 +0300823
Andrey Andreev973a6542015-01-19 13:25:24 +0200824 :param mixed $key: Session item key or NULL
825 :returns: Value of the specified item key, or an array of all userdata
Andrey Andreev28c2c972014-02-08 04:27:48 +0200826 :rtype: mixed
Michaele0a631c2013-10-20 10:40:51 +0300827
Andrey Andreev973a6542015-01-19 13:25:24 +0200828 Gets the value for a specific ``$_SESSION`` item, or an
829 array of all "userdata" items if not key was specified.
830
831 .. note:: This is a legacy method kept only for backwards
832 compatibility with older applications. You should
833 directly access ``$_SESSION`` instead.
Michaele0a631c2013-10-20 10:40:51 +0300834
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200835 .. php:method:: all_userdata()
Michaele0a631c2013-10-20 10:40:51 +0300836
Andrey Andreev28c2c972014-02-08 04:27:48 +0200837 :returns: An array of all userdata
838 :rtype: array
Michaele0a631c2013-10-20 10:40:51 +0300839
Andrey Andreev973a6542015-01-19 13:25:24 +0200840 Returns an array containing all "userdata" items.
Michaele0a631c2013-10-20 10:40:51 +0300841
Andrey Andreev973a6542015-01-19 13:25:24 +0200842 .. note:: This method is DEPRECATED. Use ``userdata()``
843 with no parameters instead.
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200844
Andrey Andreevf4cb8f92015-03-19 11:54:47 +0200845 .. php:method:: &get_userdata()
Michaele0a631c2013-10-20 10:40:51 +0300846
Andrey Andreev973a6542015-01-19 13:25:24 +0200847 :returns: A reference to ``$_SESSION``
848 :rtype: array
Michaele0a631c2013-10-20 10:40:51 +0300849
Andrey Andreev973a6542015-01-19 13:25:24 +0200850 Returns a reference to the ``$_SESSION`` array.
Michael1c7438f2013-10-06 15:21:21 +0300851
Andrey Andreev973a6542015-01-19 13:25:24 +0200852 .. note:: This is a legacy method kept only for backwards
853 compatibility with older applications.
Michael1c7438f2013-10-06 15:21:21 +0300854
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200855 .. php:method:: has_userdata($key)
Michael1c7438f2013-10-06 15:21:21 +0300856
Andrey Andreev973a6542015-01-19 13:25:24 +0200857 :param string $key: Session item key
858 :returns: TRUE if the specified key exists, FALSE if not
Andrey Andreev28c2c972014-02-08 04:27:48 +0200859 :rtype: bool
Michael1c7438f2013-10-06 15:21:21 +0300860
Andrey Andreev973a6542015-01-19 13:25:24 +0200861 Checks if an item exists in ``$_SESSION``.
Michael1c7438f2013-10-06 15:21:21 +0300862
Andrey Andreev973a6542015-01-19 13:25:24 +0200863 .. note:: This is a legacy method kept only for backwards
864 compatibility with older applications. It is just
865 an alias for ``isset($_SESSION[$key])`` - please
866 use that instead.
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200867
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200868 .. php:method:: set_userdata($data[, $value = NULL])
Andrey Andreev973a6542015-01-19 13:25:24 +0200869
870 :param mixed $data: An array of key/value pairs to set as session data, or the key for a single item
871 :param mixed $value: The value to set for a specific session item, if $data is a key
872 :rtype: void
873
874 Assigns data to the ``$_SESSION`` superglobal.
875
876 .. note:: This is a legacy method kept only for backwards
877 compatibility with older applications.
878
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200879 .. php:method:: unset_userdata($key)
Andrey Andreev973a6542015-01-19 13:25:24 +0200880
881 :param mixed $key: Key for the session data item to unset, or an array of multiple keys
882 :rtype: void
883
884 Unsets the specified key(s) from the ``$_SESSION``
885 superglobal.
886
887 .. note:: This is a legacy method kept only for backwards
888 compatibility with older applications. It is just
889 an alias for ``unset($_SESSION[$key])`` - please
890 use that instead.
891
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200892 .. php:method:: mark_as_flash($key)
Andrey Andreev973a6542015-01-19 13:25:24 +0200893
894 :param mixed $key: Key to mark as flashdata, or an array of multiple keys
895 :returns: TRUE on success, FALSE on failure
896 :rtype: bool
897
898 Marks a ``$_SESSION`` item key (or multiple ones) as
899 "flashdata".
900
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200901 .. php:method:: get_flash_keys()
Andrey Andreev973a6542015-01-19 13:25:24 +0200902
903 :returns: Array containing the keys of all "flashdata" items.
904 :rtype: array
905
906 Gets a list of all ``$_SESSION`` that have been marked as
907 "flashdata".
908
Andrey Andreev71789ce2016-07-21 12:45:58 +0300909 .. php:method:: unmark_flash($key)
Andrey Andreev973a6542015-01-19 13:25:24 +0200910
911 :param mixed $key: Key to be un-marked as flashdata, or an array of multiple keys
912 :rtype: void
913
914 Unmarks a ``$_SESSION`` item key (or multiple ones) as
915 "flashdata".
916
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200917 .. php:method:: flashdata([$key = NULL])
Andrey Andreev973a6542015-01-19 13:25:24 +0200918
919 :param mixed $key: Flashdata item key or NULL
920 :returns: Value of the specified item key, or an array of all flashdata
Andrey Andreev28c2c972014-02-08 04:27:48 +0200921 :rtype: mixed
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200922
Andrey Andreev973a6542015-01-19 13:25:24 +0200923 Gets the value for a specific ``$_SESSION`` item that has
924 been marked as "flashdata", or an array of all "flashdata"
925 items if no key was specified.
926
927 .. note:: This is a legacy method kept only for backwards
928 compatibility with older applications. You should
929 directly access ``$_SESSION`` instead.
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200930
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200931 .. php:method:: keep_flashdata($key)
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200932
Andrey Andreev973a6542015-01-19 13:25:24 +0200933 :param mixed $key: Flashdata key to keep, or an array of multiple keys
vlakoffa2dee7d2015-01-20 04:22:29 +0100934 :returns: TRUE on success, FALSE on failure
Andrey Andreev973a6542015-01-19 13:25:24 +0200935 :rtype: bool
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200936
Andrey Andreev973a6542015-01-19 13:25:24 +0200937 Retains the specified session data key(s) as "flashdata"
938 through the next request.
Michael1c7438f2013-10-06 15:21:21 +0300939
Andrey Andreev973a6542015-01-19 13:25:24 +0200940 .. note:: This is a legacy method kept only for backwards
941 compatibility with older applications. It is just
942 an alias for the ``mark_as_flash()`` method.
Michael1c7438f2013-10-06 15:21:21 +0300943
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200944 .. php:method:: set_flashdata($data[, $value = NULL])
Michael1c7438f2013-10-06 15:21:21 +0300945
Andrey Andreev973a6542015-01-19 13:25:24 +0200946 :param mixed $data: An array of key/value pairs to set as flashdata, or the key for a single item
947 :param mixed $value: The value to set for a specific session item, if $data is a key
Andrey Andreev28c2c972014-02-08 04:27:48 +0200948 :rtype: void
Michaele0a631c2013-10-20 10:40:51 +0300949
Andrey Andreev973a6542015-01-19 13:25:24 +0200950 Assigns data to the ``$_SESSION`` superglobal and marks it
951 as "flashdata".
Michaele0a631c2013-10-20 10:40:51 +0300952
Andrey Andreev973a6542015-01-19 13:25:24 +0200953 .. note:: This is a legacy method kept only for backwards
954 compatibility with older applications.
Michael1c7438f2013-10-06 15:21:21 +0300955
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200956 .. php:method:: mark_as_temp($key[, $ttl = 300])
Andrey Andreev973a6542015-01-19 13:25:24 +0200957
958 :param mixed $key: Key to mark as tempdata, or an array of multiple keys
959 :param int $ttl: Time-to-live value for the tempdata, in seconds
960 :returns: TRUE on success, FALSE on failure
961 :rtype: bool
962
963 Marks a ``$_SESSION`` item key (or multiple ones) as
964 "tempdata".
965
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200966 .. php:method:: get_temp_keys()
Andrey Andreev973a6542015-01-19 13:25:24 +0200967
968 :returns: Array containing the keys of all "tempdata" items.
969 :rtype: array
970
971 Gets a list of all ``$_SESSION`` that have been marked as
972 "tempdata".
973
Andrey Andreev71789ce2016-07-21 12:45:58 +0300974 .. php:method:: unmark_temp($key)
Andrey Andreev973a6542015-01-19 13:25:24 +0200975
976 :param mixed $key: Key to be un-marked as tempdata, or an array of multiple keys
977 :rtype: void
978
979 Unmarks a ``$_SESSION`` item key (or multiple ones) as
980 "tempdata".
981
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200982 .. php:method:: tempdata([$key = NULL])
Andrey Andreev973a6542015-01-19 13:25:24 +0200983
984 :param mixed $key: Tempdata item key or NULL
985 :returns: Value of the specified item key, or an array of all tempdata
Andrey Andreev28c2c972014-02-08 04:27:48 +0200986 :rtype: mixed
Michael1c7438f2013-10-06 15:21:21 +0300987
Andrey Andreev973a6542015-01-19 13:25:24 +0200988 Gets the value for a specific ``$_SESSION`` item that has
989 been marked as "tempdata", or an array of all "tempdata"
990 items if no key was specified.
991
992 .. note:: This is a legacy method kept only for backwards
993 compatibility with older applications. You should
994 directly access ``$_SESSION`` instead.
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200995
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200996 .. php:method:: set_tempdata($data[, $value = NULL])
Michael1c7438f2013-10-06 15:21:21 +0300997
Andrey Andreev973a6542015-01-19 13:25:24 +0200998 :param mixed $data: An array of key/value pairs to set as tempdata, or the key for a single item
999 :param mixed $value: The value to set for a specific session item, if $data is a key
1000 :param int $ttl: Time-to-live value for the tempdata item(s), in seconds
Andrey Andreev28c2c972014-02-08 04:27:48 +02001001 :rtype: void
Michael1c7438f2013-10-06 15:21:21 +03001002
Andrey Andreev973a6542015-01-19 13:25:24 +02001003 Assigns data to the ``$_SESSION`` superglobal and marks it
1004 as "tempdata".
Michael1c7438f2013-10-06 15:21:21 +03001005
Andrey Andreev973a6542015-01-19 13:25:24 +02001006 .. note:: This is a legacy method kept only for backwards
1007 compatibility with older applications.
Michael1c7438f2013-10-06 15:21:21 +03001008
Andrey Andreevcd3d9db2015-02-02 13:41:01 +02001009 .. php:method:: sess_regenerate([$destroy = FALSE])
Michael1c7438f2013-10-06 15:21:21 +03001010
Andrey Andreev973a6542015-01-19 13:25:24 +02001011 :param bool $destroy: Whether to destroy session data
Andrey Andreev28c2c972014-02-08 04:27:48 +02001012 :rtype: void
Michael1c7438f2013-10-06 15:21:21 +03001013
Andrey Andreev973a6542015-01-19 13:25:24 +02001014 Regenerate session ID, optionally destroying the current
1015 session's data.
Michael1c7438f2013-10-06 15:21:21 +03001016
Andrey Andreev973a6542015-01-19 13:25:24 +02001017 .. note:: This method is just an alias for PHP's native
1018 `session_regenerate_id()
1019 <http://php.net/session_regenerate_id>`_ function.
Michael1c7438f2013-10-06 15:21:21 +03001020
Andrey Andreevcd3d9db2015-02-02 13:41:01 +02001021 .. php:method:: sess_destroy()
Andrey Andreev973a6542015-01-19 13:25:24 +02001022
1023 :rtype: void
1024
1025 Destroys the current session.
1026
1027 .. note:: This must be the *last* session-related function
1028 that you call. All session data will be lost after
1029 you do that.
1030
1031 .. note:: This method is just an alias for PHP's native
1032 `session_destroy()
1033 <http://php.net/session_destroy>`_ function.
1034
Andrey Andreevcd3d9db2015-02-02 13:41:01 +02001035 .. php:method:: __get($key)
Andrey Andreev973a6542015-01-19 13:25:24 +02001036
1037 :param string $key: Session item key
1038 :returns: The requested session data item, or NULL if it doesn't exist
1039 :rtype: mixed
1040
1041 A magic method that allows you to use
1042 ``$this->session->item`` instead of ``$_SESSION['item']``,
1043 if that's what you prefer.
1044
1045 It will also return the session ID by calling
1046 ``session_id()`` if you try to access
1047 ``$this->session->session_id``.
1048
Andrey Andreevcd3d9db2015-02-02 13:41:01 +02001049 .. php:method:: __set($key, $value)
Andrey Andreev973a6542015-01-19 13:25:24 +02001050
1051 :param string $key: Session item key
1052 :param mixed $value: Value to assign to the session item key
1053 :returns: void
1054
1055 A magic method that allows you to assign items to
1056 ``$_SESSION`` by accessing them as ``$this->session``
1057 properties::
1058
1059 $this->session->foo = 'bar';
1060
1061 // Results in:
Andrey Andreevf4cb8f92015-03-19 11:54:47 +02001062 // $_SESSION['foo'] = 'bar';