blob: e2780683fd2622c17956019e167c5208a34c1ce7 [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
366Or if you want to be sure that you're reading "flashdata" (and not any
367other 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
436======================== =============== ======================================== ============================================================================================
437Preference Default Options Description
438======================== =============== ======================================== ============================================================================================
439**sess_driver** files files/database/redis/memcached/*custom* The session storage driver to use.
vlakoffa2dee7d2015-01-20 04:22:29 +0100440**sess_cookie_name** ci_session [A-Za-z\_-] characters only The name used for the session cookie.
Andrey Andreev973a6542015-01-19 13:25:24 +0200441**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.
Andrey Andreev9a0e6602015-01-19 14:54:08 +0200444**sess_time_to_update** 300 Time in seconds (integer) This option controls how often the session class will regenerate itself and create a new
445 session ID. Setting it to 0 will disable session ID regeneration.
Andrey Andreev973a6542015-01-19 13:25:24 +0200446**sess_match_ip** FALSE TRUE/FALSE (boolean) Whether to validate the user's IP address when reading the session cookie.
447 Note that some ISPs dynamically changes the IP, so if you want a non-expiring session you
448 will likely set this to FALSE.
449======================== =============== ======================================== ============================================================================================
450
451.. note:: As a last resort, the Session library will try to fetch PHP's
452 session related INI settings, as well as legacy CI settings such as
453 'sess_expire_on_close' when any of the above is not configured.
454 However, you should never rely on this behavior as it can cause
455 unexpected results or be changed in the future. Please configure
456 everything properly.
dchill423169f262012-08-11 20:12:05 -0400457
458In addition to the values above, the cookie and native drivers apply the
459following configuration values shared by the :doc:`Input <input>` and
460:doc:`Security <security>` classes:
461
Andrey Andreev973a6542015-01-19 13:25:24 +0200462================== =============== ===========================================================================
463Preference Default Description
464================== =============== ===========================================================================
465**cookie_domain** '' The domain for which the session is applicable
466**cookie_path** / The path to which the session is applicable
467**cookie_secure** FALSE Whether to create the session cookie only on encrypted (HTTPS) connections
468================== =============== ===========================================================================
469
470.. note:: The 'cookie_httponly' setting doesn't have an effect on sessions.
471 Instead the HttpOnly parameter is always enabled, for security
472 reasons. Additionaly, the 'cookie_prefix' setting is completely
473 ignored.
dchill423169f262012-08-11 20:12:05 -0400474
475Session Drivers
476===============
477
Andrey Andreev973a6542015-01-19 13:25:24 +0200478As already mentioned, the Session library comes with 4 drivers, or storage
479engines, that you can use:
dchill423169f262012-08-11 20:12:05 -0400480
Andrey Andreev973a6542015-01-19 13:25:24 +0200481 - files
482 - database
483 - redis
484 - memcached
dchill423169f262012-08-11 20:12:05 -0400485
Andrey Andreev973a6542015-01-19 13:25:24 +0200486By default, the `Files Driver`_ will be used when a session is initialized,
487because it is the most safe choice and is expected to work everywhere
488(virtually every environment has a file system).
dchill423169f262012-08-11 20:12:05 -0400489
Andrey Andreev973a6542015-01-19 13:25:24 +0200490However, any other driver may be selected via the ``$config['sess_driver']``
491line in your **application/config/config.php** file, if you chose to do so.
492Have it in mind though, every driver has different caveats, so be sure to
493get yourself familiar with them (below) before you make that choice.
dchill423169f262012-08-11 20:12:05 -0400494
Andrey Andreev973a6542015-01-19 13:25:24 +0200495In addition, you may also create and use `Custom Drivers`_, if the ones
496provided by default don't satisfy your use case.
dchill423169f262012-08-11 20:12:05 -0400497
Andrey Andreev973a6542015-01-19 13:25:24 +0200498.. note:: In previous CodeIgniter versions, a different, "cookie driver"
499 was the only option and we have received negative feedback on not
500 providing that option. While we do listen to feedback from the
501 community, we want to warn you that it was dropped because it is
502 **unsafe** and we advise you NOT to try to replicate it via a
503 custom driver.
dchill423169f262012-08-11 20:12:05 -0400504
Andrey Andreev973a6542015-01-19 13:25:24 +0200505Files Driver
506------------
dchill423169f262012-08-11 20:12:05 -0400507
Andrey Andreev973a6542015-01-19 13:25:24 +0200508The 'files' driver uses your file system for storing session data.
dchill423169f262012-08-11 20:12:05 -0400509
Andrey Andreev973a6542015-01-19 13:25:24 +0200510It can safely be said that it works exactly like PHP's own default session
511implementation, but in case this is an important detail for you, have it
512mind that it is in fact not the same code and it has some limitations
513(and advantages).
dchill423169f262012-08-11 20:12:05 -0400514
Andrey Andreev973a6542015-01-19 13:25:24 +0200515To be more specific, it doesn't support PHP's `directory level and mode
516formats used in session.save_path
517<http://php.net/manual/en/session.configuration.php#ini.session.save-path>`_,
518and it has most of the options hard-coded for safety. Instead, only
519absolute paths are supported for ``$config['sess_save_path']``.
dchill423169f262012-08-11 20:12:05 -0400520
Andrey Andreev973a6542015-01-19 13:25:24 +0200521Another important thing that you should know, is to make sure that you
522don't use a publicly-readable or shared directory for storing your session
523files. Make sure that *only you* have access to see the contents of your
524chosen *sess_save_path* directory. Otherwise, anybody who can do that, can
525also steal any of the current sessions (also known as "session fixation"
526attack).
dchill423169f262012-08-11 20:12:05 -0400527
Andrey Andreev973a6542015-01-19 13:25:24 +0200528On UNIX-like operating systems, this is usually achieved by setting the
Andrey Andreev6e8a2022015-02-03 10:53:05 +02005290700 mode permissions on that directory via the `chmod` command, which
Andrey Andreev973a6542015-01-19 13:25:24 +0200530allows only the directory's owner to perform read and write operations on
531it. But be careful because the system user *running* the script is usually
532not your own, but something like 'www-data' instead, so only setting those
533permissions will probable break your application.
dchill423169f262012-08-11 20:12:05 -0400534
Andrey Andreev973a6542015-01-19 13:25:24 +0200535Instead, you should do something like this, depending on your environment
536::
dchill423169f262012-08-11 20:12:05 -0400537
Andrey Andreev973a6542015-01-19 13:25:24 +0200538 mkdir /<path to your application directory>/sessions/
Andrey Andreev6e8a2022015-02-03 10:53:05 +0200539 chmod 0700 /<path to your application directory>/sessions/
Andrey Andreev973a6542015-01-19 13:25:24 +0200540 chown www-data /<path to your application directory>/sessions/
dchill423169f262012-08-11 20:12:05 -0400541
Andrey Andreev973a6542015-01-19 13:25:24 +0200542Bonus Tip
543^^^^^^^^^
dchill423169f262012-08-11 20:12:05 -0400544
Andrey Andreev973a6542015-01-19 13:25:24 +0200545Some of you will probably opt to choose another session driver because
546file storage is usually slower. This is only half true.
dchill423169f262012-08-11 20:12:05 -0400547
Andrey Andreev973a6542015-01-19 13:25:24 +0200548A very basic test will probably trick you into believing that an SQL
549database is faster, but in 99% of the cases, this is only true while you
550only have a few current sessions. As the sessions count and server loads
551increase - which is the time when it matters - the file system will
552consistently outperform almost all relational database setups.
dchill423169f262012-08-11 20:12:05 -0400553
Andrey Andreev973a6542015-01-19 13:25:24 +0200554In addition, if performance is your only concern, you may want to look
555into using `tmpfs <http://eddmann.com/posts/storing-php-sessions-file-caches-in-memory-using-tmpfs/>`_,
556(warning: external resource), which can make your sessions blazing fast.
Derek Jones8ede1a22011-10-05 13:34:52 -0500557
Andrey Andreev973a6542015-01-19 13:25:24 +0200558Database Driver
559---------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500560
Andrey Andreev973a6542015-01-19 13:25:24 +0200561The 'database' driver uses a relational database such as MySQL or
562PostgreSQL to store sessions. This is a popular choice among many users,
563because it allows the developer easy access to the session data within
564an application - it is just another table in your database.
Derek Jones8ede1a22011-10-05 13:34:52 -0500565
Andrey Andreev973a6542015-01-19 13:25:24 +0200566However, there are some conditions that must be met:
Derek Jones8ede1a22011-10-05 13:34:52 -0500567
Andrey Andreev973a6542015-01-19 13:25:24 +0200568 - Only your **default** database connection (or the one that you access
569 as ``$this->db`` from your controllers) can be used.
570 - You can NOT use a persistent connection.
571 - You must have the :doc:`Query Builder </database/query_builder>`
572 enabled.
Derek Jones8ede1a22011-10-05 13:34:52 -0500573
Andrey Andreev973a6542015-01-19 13:25:24 +0200574In order to use the 'database' session driver, you must also create this
575table that we already mentioned and then set it as your
576``$config['sess_save_path']`` value.
577For example, if you would like to use 'ci_sessions' as your table name,
Andrey Andreeveccac6e2015-02-04 16:04:31 +0200578you would do this::
Andrey Andreev973a6542015-01-19 13:25:24 +0200579
580 $config['sess_driver'] = 'database';
581 $config['sess_save_path'] = 'ci_sessions';
582
583.. note:: If you've upgraded from a previous version of CodeIgniter and
584 you don't have 'sess_save_path' configured, then the Session
585 library will look for the old 'sess_table_name' setting and use
586 it instead. Please don't rely on this behavior as it will get
587 removed in the future.
588
589And then of course, create the database table ...
590
591For MySQL::
592
593 CREATE TABLE IF NOT EXISTS `ci_sessions` (
Andrey Andreev3053b812015-01-20 17:31:29 +0200594 `id` varchar(40) NOT NULL,
Andrey Andreev973a6542015-01-19 13:25:24 +0200595 `ip_address` varchar(45) NOT NULL,
596 `timestamp` int(10) unsigned DEFAULT 0 NOT NULL,
597 `data` blob DEFAULT '' NOT NULL,
Andrey Andreev9f4eda72015-01-26 11:58:07 +0200598 PRIMARY KEY (id),
Andrey Andreev973a6542015-01-19 13:25:24 +0200599 KEY `ci_sessions_timestamp` (`timestamp`)
Derek Jones4b83d912011-10-05 15:42:30 -0500600 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500601
Andrey Andreev973a6542015-01-19 13:25:24 +0200602For PostgreSQL::
Andrey Andreev69e1b4f2014-01-07 17:29:25 +0200603
Andrey Andreev973a6542015-01-19 13:25:24 +0200604 CREATE TABLE "ci_sessions" (
Andrey Andreev3053b812015-01-20 17:31:29 +0200605 "id" varchar(40) NOT NULL,
Andrey Andreev973a6542015-01-19 13:25:24 +0200606 "ip_address" varchar(45) NOT NULL,
607 "timestamp" bigint DEFAULT 0 NOT NULL,
608 "data" text DEFAULT '' NOT NULL,
Master Yoda08a92bc2015-01-26 00:46:57 -0800609 PRIMARY KEY ("id")
Andrey Andreev69e1b4f2014-01-07 17:29:25 +0200610 );
611
Andrey Andreev973a6542015-01-19 13:25:24 +0200612 CREATE INDEX "ci_sessions_timestamp" ON "ci_sessions" ("timestamp");
Andrey Andreev69e1b4f2014-01-07 17:29:25 +0200613
Andrey Andreev973a6542015-01-19 13:25:24 +0200614However, if you want to turn on the *sess_match_ip* setting, you should
615also do the following, after creating the table::
Derek Jones8ede1a22011-10-05 13:34:52 -0500616
Andrey Andreev973a6542015-01-19 13:25:24 +0200617 // Works both on MySQL and PostgreSQL
Andrey Andreev3053b812015-01-20 17:31:29 +0200618 ALTER TABLE ci_sessions ADD CONSTRAINT ci_sessions_id_ip UNIQUE (id, ip_address);
Derek Jones8ede1a22011-10-05 13:34:52 -0500619
Andrey Andreev973a6542015-01-19 13:25:24 +0200620.. important:: Only MySQL and PostgreSQL databases are officially
Andrey Andreev052c02f2015-01-19 13:30:30 +0200621 supported, due to lack of advisory locking mechanisms on other
622 platforms. Using sessions without locks can cause all sorts of
623 problems, especially with heavy usage of AJAX, and we will not
624 support such cases. Use ``session_write_close()`` after you've
625 done processing session data if you're having performance
626 issues.
Derek Jones8ede1a22011-10-05 13:34:52 -0500627
Andrey Andreev973a6542015-01-19 13:25:24 +0200628Redis Driver
629------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500630
Andrey Andreev973a6542015-01-19 13:25:24 +0200631Redis is a storage engine typically used for caching and popular because
632of its high performance, which is also probably your reason to use the
633'redis' session driver.
Derek Jones8ede1a22011-10-05 13:34:52 -0500634
Andrey Andreev973a6542015-01-19 13:25:24 +0200635The downside is that it is not as ubiquitous as relational databases and
636requires the `phpredis <https://github.com/nicolasff/phpredis>`_ PHP
637extension to be installed on your system, and that one doesn't come
638bundled with PHP.
639Chances are, you're only be using the 'redis' driver only if you're already
640both familiar with Redis and using it for other purposes.
Derek Jones8ede1a22011-10-05 13:34:52 -0500641
Andrey Andreev973a6542015-01-19 13:25:24 +0200642Just as with the 'files' and 'database' drivers, you must also configure
643the storage location for your sessions via the
644``$config['sess_save_path']`` setting.
645The format here is a bit different and complicated at the same time. It is
646best explained by the *phpredis* extension's README file, so we'll simply
647link you to it:
Derek Jones8ede1a22011-10-05 13:34:52 -0500648
Andrey Andreev973a6542015-01-19 13:25:24 +0200649 https://github.com/phpredis/phpredis#php-session-handler
650
651.. warning:: CodeIgniter's Session library does NOT use the actual 'redis'
652 ``session.save_handler``. Take note **only** of the path format in
653 the link above.
654
655For the most common case however, a simple ``host:port`` pair should be
656sufficient::
657
658 $config['sess_driver'] = 'redis';
659 $config['sess_save_path'] = 'tcp://localhost:6379';
660
661Memcached Driver
662----------------
663
664The 'memcached' driver is very similar to the 'redis' one in all of its
665properties, except perhaps for availability, because PHP's `Memcached
666<http://php.net/memcached>`_ extension is distributed via PECL and some
667Linux distrubutions make it available as an easy to install package.
668
669Other than that, and without any intentional bias towards Redis, there's
670not much different to be said about Memcached - it is also a popular
671product that is usually used for caching and famed for its speed.
672
673However, it is worth noting that the only guarantee given by Memcached
674is that setting value X to expire after Y seconds will result in it being
675deleted after Y seconds have passed (but not necessarily that it won't
676expire earlier than that time). This happens very rarely, but should be
677considered as it may result in loss of sessions.
678
679The ``$config['sess_save_path']`` format is fairly straightforward here,
680being just a ``host:port`` pair::
681
682 $config['sess_driver'] = 'memcached';
683 $config['sess_save_path'] = 'localhost:11211';
684
685Bonus Tip
686^^^^^^^^^
687
688Multi-server configuration with an optional *weight* parameter as the
689third colon-separated (``:weight``) value is also supported, but we have
690to note that we haven't tested if that is reliable.
691
692If you want to experiment with this feature (on your own risk), simply
693separate the multiple server paths with commas::
694
695 // localhost will be given higher priority (5) here,
696 // compared to 192.0.2.1 with a weight of 1.
697 $config['sess_save_path'] = 'localhost:11211:5,192.0.2.1:11211:1';
Derek Jones8ede1a22011-10-05 13:34:52 -0500698
dchill423169f262012-08-11 20:12:05 -0400699Custom Drivers
700--------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500701
Andrey Andreev973a6542015-01-19 13:25:24 +0200702You may also create your own, custom session drivers. However, have it in
703mind that this is typically not an easy task, as it takes a lot of
704knowledge to do it properly.
Derek Jones8ede1a22011-10-05 13:34:52 -0500705
Andrey Andreev973a6542015-01-19 13:25:24 +0200706You need to know not only how sessions work in general, but also how they
707work specifically in PHP, how the underlying storage mechanism works, how
708to handle concurrency, avoid deadlocks (but NOT through lack of locks) and
709last but not least - how to handle the potential security issues, which
710is far from trivial.
Derek Jones8ede1a22011-10-05 13:34:52 -0500711
Andrey Andreev973a6542015-01-19 13:25:24 +0200712Long story short - if you don't know how to do that already in raw PHP,
713you shouldn't be trying to do it within CodeIgniter either. You've been
714warned.
Andrey Andreevb57b2ad2014-01-03 12:02:08 +0200715
Andrey Andreev973a6542015-01-19 13:25:24 +0200716If you only want to add some extra functionality to your sessions, just
717extend the base Session class, which is a lot more easier. Read the
718:doc:`Creating Libraries <../general/creating_libraries>` article to
719learn how to do that.
720
721Now, to the point - there are three general rules that you must follow
722when creating a session driver for CodeIgniter:
723
724 - Put your driver's file under **application/libraries/Session/drivers/**
725 and follow the naming conventions used by the Session class.
726
727 For example, if you were to create a 'dummy' driver, you would have
728 a ``Session_dummy_driver`` class name, that is declared in
729 *application/libraries/Session/drivers/Session_dummy_driver.php*.
730
731 - Extend the ``CI_Session_driver`` class.
732
733 This is just a basic class with a few internal helper methods. It is
734 also extendable like any other library, if you really need to do that,
735 but we are not going to explain how ... if you're familiar with how
736 class extensions/overrides work in CI, then you already know how to do
737 it. If not, well, you shouldn't be doing it in the first place.
738
739
740 - Implement the `SessionHandlerInterface
741 <http://php.net/sessionhandlerinterface>`_ interface.
742
743 .. note:: You may notice that ``SessionHandlerInterface`` is provided
744 by PHP since version 5.4.0. CodeIgniter will automatically declare
745 the same interface if you're running an older PHP version.
746
747 The link will explain why and how.
748
749So, based on our 'dummy' driver example above, you'd end up with something
750like this::
751
752 // application/libraries/Session/drivers/Session_dummy_driver.php:
753
754 class CI_Session_dummy_driver extends CI_Session_driver implements SessionHandlerInterface
755 {
756
757 public function __construct(&$params)
dchill423169f262012-08-11 20:12:05 -0400758 {
Andrey Andreev973a6542015-01-19 13:25:24 +0200759 // DO NOT forget this
760 parent::__construct($params);
761
762 // Configuration & other initializations
dchill423169f262012-08-11 20:12:05 -0400763 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500764
Andrey Andreev973a6542015-01-19 13:25:24 +0200765 public function open($save_path, $name)
dchill423169f262012-08-11 20:12:05 -0400766 {
Andrey Andreev973a6542015-01-19 13:25:24 +0200767 // Initialize storage mechanism (connection)
dchill423169f262012-08-11 20:12:05 -0400768 }
769
Andrey Andreev973a6542015-01-19 13:25:24 +0200770 public function read($session_id)
dchill423169f262012-08-11 20:12:05 -0400771 {
Andrey Andreev973a6542015-01-19 13:25:24 +0200772 // Read session data (if exists), acquire locks
dchill423169f262012-08-11 20:12:05 -0400773 }
774
Andrey Andreev973a6542015-01-19 13:25:24 +0200775 public function write($session_id, $session_data)
dchill423169f262012-08-11 20:12:05 -0400776 {
Andrey Andreev973a6542015-01-19 13:25:24 +0200777 // Create / update session data (it might not exist!)
dchill423169f262012-08-11 20:12:05 -0400778 }
779
Andrey Andreev973a6542015-01-19 13:25:24 +0200780 public function close()
dchill423169f262012-08-11 20:12:05 -0400781 {
Andrey Andreev973a6542015-01-19 13:25:24 +0200782 // Free locks, close connections / streams / etc.
783 }
784
785 public function destroy($session_id)
786 {
787 // Call close() method & destroy data for current session (order may differ)
788 }
789
790 public function gc($maxlifetime)
791 {
792 // Erase data for expired sessions
dchill423169f262012-08-11 20:12:05 -0400793 }
Andrey Andreevb57b2ad2014-01-03 12:02:08 +0200794
dchill423169f262012-08-11 20:12:05 -0400795 }
796
Andrey Andreev973a6542015-01-19 13:25:24 +0200797If you've done everything properly, you can now set your *sess_driver*
798configuration value to 'dummy' and use your own driver. Congratulations!
Michael1c7438f2013-10-06 15:21:21 +0300799
Michael1c7438f2013-10-06 15:21:21 +0300800***************
801Class Reference
802***************
803
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200804.. php:class:: CI_Session
Michael1c7438f2013-10-06 15:21:21 +0300805
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200806 .. php:method:: userdata([$key = NULL])
Michaele0a631c2013-10-20 10:40:51 +0300807
Andrey Andreev973a6542015-01-19 13:25:24 +0200808 :param mixed $key: Session item key or NULL
809 :returns: Value of the specified item key, or an array of all userdata
Andrey Andreev28c2c972014-02-08 04:27:48 +0200810 :rtype: mixed
Michaele0a631c2013-10-20 10:40:51 +0300811
Andrey Andreev973a6542015-01-19 13:25:24 +0200812 Gets the value for a specific ``$_SESSION`` item, or an
813 array of all "userdata" items if not key was specified.
814
815 .. note:: This is a legacy method kept only for backwards
816 compatibility with older applications. You should
817 directly access ``$_SESSION`` instead.
Michaele0a631c2013-10-20 10:40:51 +0300818
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200819 .. php:method:: all_userdata()
Michaele0a631c2013-10-20 10:40:51 +0300820
Andrey Andreev28c2c972014-02-08 04:27:48 +0200821 :returns: An array of all userdata
822 :rtype: array
Michaele0a631c2013-10-20 10:40:51 +0300823
Andrey Andreev973a6542015-01-19 13:25:24 +0200824 Returns an array containing all "userdata" items.
Michaele0a631c2013-10-20 10:40:51 +0300825
Andrey Andreev973a6542015-01-19 13:25:24 +0200826 .. note:: This method is DEPRECATED. Use ``userdata()``
827 with no parameters instead.
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200828
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200829 .. php:method:: &get_usedata()
Michaele0a631c2013-10-20 10:40:51 +0300830
Andrey Andreev973a6542015-01-19 13:25:24 +0200831 :returns: A reference to ``$_SESSION``
832 :rtype: array
Michaele0a631c2013-10-20 10:40:51 +0300833
Andrey Andreev973a6542015-01-19 13:25:24 +0200834 Returns a reference to the ``$_SESSION`` array.
Michael1c7438f2013-10-06 15:21:21 +0300835
Andrey Andreev973a6542015-01-19 13:25:24 +0200836 .. note:: This is a legacy method kept only for backwards
837 compatibility with older applications.
Michael1c7438f2013-10-06 15:21:21 +0300838
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200839 .. php:method:: has_userdata($key)
Michael1c7438f2013-10-06 15:21:21 +0300840
Andrey Andreev973a6542015-01-19 13:25:24 +0200841 :param string $key: Session item key
842 :returns: TRUE if the specified key exists, FALSE if not
Andrey Andreev28c2c972014-02-08 04:27:48 +0200843 :rtype: bool
Michael1c7438f2013-10-06 15:21:21 +0300844
Andrey Andreev973a6542015-01-19 13:25:24 +0200845 Checks if an item exists in ``$_SESSION``.
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. It is just
849 an alias for ``isset($_SESSION[$key])`` - please
850 use that instead.
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200851
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200852 .. php:method:: set_userdata($data[, $value = NULL])
Andrey Andreev973a6542015-01-19 13:25:24 +0200853
854 :param mixed $data: An array of key/value pairs to set as session data, or the key for a single item
855 :param mixed $value: The value to set for a specific session item, if $data is a key
856 :rtype: void
857
858 Assigns data to the ``$_SESSION`` superglobal.
859
860 .. note:: This is a legacy method kept only for backwards
861 compatibility with older applications.
862
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200863 .. php:method:: unset_userdata($key)
Andrey Andreev973a6542015-01-19 13:25:24 +0200864
865 :param mixed $key: Key for the session data item to unset, or an array of multiple keys
866 :rtype: void
867
868 Unsets the specified key(s) from the ``$_SESSION``
869 superglobal.
870
871 .. note:: This is a legacy method kept only for backwards
872 compatibility with older applications. It is just
873 an alias for ``unset($_SESSION[$key])`` - please
874 use that instead.
875
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200876 .. php:method:: mark_as_flash($key)
Andrey Andreev973a6542015-01-19 13:25:24 +0200877
878 :param mixed $key: Key to mark as flashdata, or an array of multiple keys
879 :returns: TRUE on success, FALSE on failure
880 :rtype: bool
881
882 Marks a ``$_SESSION`` item key (or multiple ones) as
883 "flashdata".
884
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200885 .. php:method:: get_flash_keys()
Andrey Andreev973a6542015-01-19 13:25:24 +0200886
887 :returns: Array containing the keys of all "flashdata" items.
888 :rtype: array
889
890 Gets a list of all ``$_SESSION`` that have been marked as
891 "flashdata".
892
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200893 .. php:method:: umark_flash($key)
Andrey Andreev973a6542015-01-19 13:25:24 +0200894
895 :param mixed $key: Key to be un-marked as flashdata, or an array of multiple keys
896 :rtype: void
897
898 Unmarks a ``$_SESSION`` item key (or multiple ones) as
899 "flashdata".
900
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200901 .. php:method:: flashdata([$key = NULL])
Andrey Andreev973a6542015-01-19 13:25:24 +0200902
903 :param mixed $key: Flashdata item key or NULL
904 :returns: Value of the specified item key, or an array of all flashdata
Andrey Andreev28c2c972014-02-08 04:27:48 +0200905 :rtype: mixed
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200906
Andrey Andreev973a6542015-01-19 13:25:24 +0200907 Gets the value for a specific ``$_SESSION`` item that has
908 been marked as "flashdata", or an array of all "flashdata"
909 items if no key was specified.
910
911 .. note:: This is a legacy method kept only for backwards
912 compatibility with older applications. You should
913 directly access ``$_SESSION`` instead.
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200914
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200915 .. php:method:: keep_flashdata($key)
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200916
Andrey Andreev973a6542015-01-19 13:25:24 +0200917 :param mixed $key: Flashdata key to keep, or an array of multiple keys
vlakoffa2dee7d2015-01-20 04:22:29 +0100918 :returns: TRUE on success, FALSE on failure
Andrey Andreev973a6542015-01-19 13:25:24 +0200919 :rtype: bool
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200920
Andrey Andreev973a6542015-01-19 13:25:24 +0200921 Retains the specified session data key(s) as "flashdata"
922 through the next request.
Michael1c7438f2013-10-06 15:21:21 +0300923
Andrey Andreev973a6542015-01-19 13:25:24 +0200924 .. note:: This is a legacy method kept only for backwards
925 compatibility with older applications. It is just
926 an alias for the ``mark_as_flash()`` method.
Michael1c7438f2013-10-06 15:21:21 +0300927
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200928 .. php:method:: set_flashdata($data[, $value = NULL])
Michael1c7438f2013-10-06 15:21:21 +0300929
Andrey Andreev973a6542015-01-19 13:25:24 +0200930 :param mixed $data: An array of key/value pairs to set as flashdata, or the key for a single item
931 :param mixed $value: The value to set for a specific session item, if $data is a key
Andrey Andreev28c2c972014-02-08 04:27:48 +0200932 :rtype: void
Michaele0a631c2013-10-20 10:40:51 +0300933
Andrey Andreev973a6542015-01-19 13:25:24 +0200934 Assigns data to the ``$_SESSION`` superglobal and marks it
935 as "flashdata".
Michaele0a631c2013-10-20 10:40:51 +0300936
Andrey Andreev973a6542015-01-19 13:25:24 +0200937 .. note:: This is a legacy method kept only for backwards
938 compatibility with older applications.
Michael1c7438f2013-10-06 15:21:21 +0300939
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200940 .. php:method:: mark_as_temp($key[, $ttl = 300])
Andrey Andreev973a6542015-01-19 13:25:24 +0200941
942 :param mixed $key: Key to mark as tempdata, or an array of multiple keys
943 :param int $ttl: Time-to-live value for the tempdata, in seconds
944 :returns: TRUE on success, FALSE on failure
945 :rtype: bool
946
947 Marks a ``$_SESSION`` item key (or multiple ones) as
948 "tempdata".
949
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200950 .. php:method:: get_temp_keys()
Andrey Andreev973a6542015-01-19 13:25:24 +0200951
952 :returns: Array containing the keys of all "tempdata" items.
953 :rtype: array
954
955 Gets a list of all ``$_SESSION`` that have been marked as
956 "tempdata".
957
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200958 .. php:method:: umark_temp($key)
Andrey Andreev973a6542015-01-19 13:25:24 +0200959
960 :param mixed $key: Key to be un-marked as tempdata, or an array of multiple keys
961 :rtype: void
962
963 Unmarks a ``$_SESSION`` item key (or multiple ones) as
964 "tempdata".
965
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200966 .. php:method:: tempdata([$key = NULL])
Andrey Andreev973a6542015-01-19 13:25:24 +0200967
968 :param mixed $key: Tempdata item key or NULL
969 :returns: Value of the specified item key, or an array of all tempdata
Andrey Andreev28c2c972014-02-08 04:27:48 +0200970 :rtype: mixed
Michael1c7438f2013-10-06 15:21:21 +0300971
Andrey Andreev973a6542015-01-19 13:25:24 +0200972 Gets the value for a specific ``$_SESSION`` item that has
973 been marked as "tempdata", or an array of all "tempdata"
974 items if no key was specified.
975
976 .. note:: This is a legacy method kept only for backwards
977 compatibility with older applications. You should
978 directly access ``$_SESSION`` instead.
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200979
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200980 .. php:method:: set_tempdata($data[, $value = NULL])
Michael1c7438f2013-10-06 15:21:21 +0300981
Andrey Andreev973a6542015-01-19 13:25:24 +0200982 :param mixed $data: An array of key/value pairs to set as tempdata, or the key for a single item
983 :param mixed $value: The value to set for a specific session item, if $data is a key
984 :param int $ttl: Time-to-live value for the tempdata item(s), in seconds
Andrey Andreev28c2c972014-02-08 04:27:48 +0200985 :rtype: void
Michael1c7438f2013-10-06 15:21:21 +0300986
Andrey Andreev973a6542015-01-19 13:25:24 +0200987 Assigns data to the ``$_SESSION`` superglobal and marks it
988 as "tempdata".
Michael1c7438f2013-10-06 15:21:21 +0300989
Andrey Andreev973a6542015-01-19 13:25:24 +0200990 .. note:: This is a legacy method kept only for backwards
991 compatibility with older applications.
Michael1c7438f2013-10-06 15:21:21 +0300992
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200993 .. php:method:: sess_regenerate([$destroy = FALSE])
Michael1c7438f2013-10-06 15:21:21 +0300994
Andrey Andreev973a6542015-01-19 13:25:24 +0200995 :param bool $destroy: Whether to destroy session data
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 Regenerate session ID, optionally destroying the current
999 session's data.
Michael1c7438f2013-10-06 15:21:21 +03001000
Andrey Andreev973a6542015-01-19 13:25:24 +02001001 .. note:: This method is just an alias for PHP's native
1002 `session_regenerate_id()
1003 <http://php.net/session_regenerate_id>`_ function.
Michael1c7438f2013-10-06 15:21:21 +03001004
Andrey Andreevcd3d9db2015-02-02 13:41:01 +02001005 .. php:method:: sess_destroy()
Andrey Andreev973a6542015-01-19 13:25:24 +02001006
1007 :rtype: void
1008
1009 Destroys the current session.
1010
1011 .. note:: This must be the *last* session-related function
1012 that you call. All session data will be lost after
1013 you do that.
1014
1015 .. note:: This method is just an alias for PHP's native
1016 `session_destroy()
1017 <http://php.net/session_destroy>`_ function.
1018
Andrey Andreevcd3d9db2015-02-02 13:41:01 +02001019 .. php:method:: __get($key)
Andrey Andreev973a6542015-01-19 13:25:24 +02001020
1021 :param string $key: Session item key
1022 :returns: The requested session data item, or NULL if it doesn't exist
1023 :rtype: mixed
1024
1025 A magic method that allows you to use
1026 ``$this->session->item`` instead of ``$_SESSION['item']``,
1027 if that's what you prefer.
1028
1029 It will also return the session ID by calling
1030 ``session_id()`` if you try to access
1031 ``$this->session->session_id``.
1032
Andrey Andreevcd3d9db2015-02-02 13:41:01 +02001033 .. php:method:: __set($key, $value)
Andrey Andreev973a6542015-01-19 13:25:24 +02001034
1035 :param string $key: Session item key
1036 :param mixed $value: Value to assign to the session item key
1037 :returns: void
1038
1039 A magic method that allows you to assign items to
1040 ``$_SESSION`` by accessing them as ``$this->session``
1041 properties::
1042
1043 $this->session->foo = 'bar';
1044
1045 // Results in:
1046 // $_SESSION['foo'] = 'bar';