blob: ac0ca27f5b3337feec00aa47283da3096347d001 [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
75What is Session Data?
76=====================
77
Andrey Andreev973a6542015-01-19 13:25:24 +020078Session data is simply an array associated with a particular session ID
79(cookie).
Derek Jones8ede1a22011-10-05 13:34:52 -050080
Andrey Andreev973a6542015-01-19 13:25:24 +020081If you've used sessions in PHP before, you should be familiar with PHP's
82`$_SESSION superglobal <http://php.net/manual/en/reserved.variables.session.php>`_
83(if not, please read the content on that link).
Derek Jones8ede1a22011-10-05 13:34:52 -050084
Andrey Andreev973a6542015-01-19 13:25:24 +020085CodeIgniter gives access to its session data through the same means, as it
86uses the session handlers' mechanism provided by PHP. Using session data is
87as simple as manipulating (read, set and unset values) the ``$_SESSION``
88array.
Derek Jones8ede1a22011-10-05 13:34:52 -050089
Andrey Andreev973a6542015-01-19 13:25:24 +020090In addition, CodeIgniter also provides 2 special types of session data
91that are further explained below: flashdata and tempdata.
Derek Jones8ede1a22011-10-05 13:34:52 -050092
Andrey Andreev973a6542015-01-19 13:25:24 +020093.. note:: In previous versions, regular session data in CodeIgniter was
94 referred to as 'userdata'. Have this in mind if that term is used
95 elsewhere in the manual. Most of it is written to explain how
96 the custom 'userdata' methods work.
Derek Jones8ede1a22011-10-05 13:34:52 -050097
98Retrieving Session Data
99=======================
100
Andrey Andreev973a6542015-01-19 13:25:24 +0200101Any piece of information from the session array is available through the
102``$_SESSION`` superglobal::
103
104 $_SESSION['item']
105
106Or through the magic getter::
107
108 $this->session->item
109
110And for backwards compatibility, through the ``userdata()`` method::
Derek Jones8ede1a22011-10-05 13:34:52 -0500111
112 $this->session->userdata('item');
113
Andrey Andreev973a6542015-01-19 13:25:24 +0200114Where item is the array key corresponding to the item you wish to fetch.
115For example, to assign a previously stored 'name' item to the ``$name``
116variable, you will do this::
Derek Jones8ede1a22011-10-05 13:34:52 -0500117
Andrey Andreev973a6542015-01-19 13:25:24 +0200118 $name = $_SESSION['name'];
Derek Jones8ede1a22011-10-05 13:34:52 -0500119
Andrey Andreev973a6542015-01-19 13:25:24 +0200120 // or:
121
122 $name = $this->session->name
123
124 // or:
125
126 $name = $this->session->userdata('name');
127
128.. note:: The ``userdata()`` method returns NULL if the item you are trying
129 to access does not exist.
Derek Jones8ede1a22011-10-05 13:34:52 -0500130
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200131If you want to retrieve all of the existing userdata, you can simply
Andrey Andreev973a6542015-01-19 13:25:24 +0200132omit the item key (magic getter only works for properties)::
133
134 $_SESSION
135
136 // or:
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200137
138 $this->session->userdata();
139
Andrey Andreev973a6542015-01-19 13:25:24 +0200140Adding Session Data
141===================
Derek Jones8ede1a22011-10-05 13:34:52 -0500142
143Let's say a particular user logs into your site. Once authenticated, you
Andrey Andreev973a6542015-01-19 13:25:24 +0200144could add their username and e-mail address to the session, making that
145data globally available to you without having to run a database query when
146you need it.
Derek Jones8ede1a22011-10-05 13:34:52 -0500147
Andrey Andreev973a6542015-01-19 13:25:24 +0200148You can simply assign data to the ``$_SESSION`` array, as with any other
149variable. Or as a property of ``$this->session``.
150
151Alternatively, the old method of assigning it as "userdata" is also
152available. That however passing an array containing your new data to the
153``set_userdata()`` method::
Derek Jones8ede1a22011-10-05 13:34:52 -0500154
155 $this->session->set_userdata($array);
156
Andrey Andreev973a6542015-01-19 13:25:24 +0200157Where ``$array`` is an associative array containing your new data. Here's
158an example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500159
Derek Jones4b83d912011-10-05 15:42:30 -0500160 $newdata = array(
Michael5d4131b2013-09-30 12:22:29 +0300161 'username' => 'johndoe',
162 'email' => 'johndoe@some-site.com',
163 'logged_in' => TRUE
164 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500165
Derek Jones4b83d912011-10-05 15:42:30 -0500166 $this->session->set_userdata($newdata);
Derek Jones8ede1a22011-10-05 13:34:52 -0500167
Michael5d4131b2013-09-30 12:22:29 +0300168If you want to add userdata one value at a time, ``set_userdata()`` also
Andrey Andreev973a6542015-01-19 13:25:24 +0200169supports this syntax::
Derek Jones8ede1a22011-10-05 13:34:52 -0500170
171 $this->session->set_userdata('some_name', 'some_value');
172
Andrey Andreev973a6542015-01-19 13:25:24 +0200173If you want to verify that a session value exists, simply check with
174``isset()``::
Derek Jones8ede1a22011-10-05 13:34:52 -0500175
Andrey Andreev973a6542015-01-19 13:25:24 +0200176 // returns FALSE if the 'some_name' item doesn't exist or is NULL,
177 // TRUE otherwise:
178 isset($_SESSION['some_name'])
179
180Or you can call ``has_userdata()``::
dchill423169f262012-08-11 20:12:05 -0400181
182 $this->session->has_userdata('some_name');
Derek Jones8ede1a22011-10-05 13:34:52 -0500183
Derek Jones8ede1a22011-10-05 13:34:52 -0500184Removing Session Data
185=====================
186
Andrey Andreev973a6542015-01-19 13:25:24 +0200187Just as with any other variable, unsetting a value in ``$_SESSION`` can be
188done through ``unset()``::
189
190 unset($_SESSION['some_name']);
191
192 // or multiple values:
193
194 unset(
195 $_SESSION['some_name'],
196 $_SESSION['another_name']
197 );
198
199Also, just as ``set_userdata()`` can be used to add information to a
200session, ``unset_userdata()`` can be used to remove it, by passing the
201session key. For example, if you wanted to remove 'some_name' from your
202session data array::
Derek Jones8ede1a22011-10-05 13:34:52 -0500203
204 $this->session->unset_userdata('some_name');
205
Andrey Andreevb339df32015-02-02 11:45:11 +0200206This method also accepts an array of item keys to unset::
Derek Jones8ede1a22011-10-05 13:34:52 -0500207
Andrey Andreevb339df32015-02-02 11:45:11 +0200208 $array_items = array('username', 'email');
Derek Jones4b83d912011-10-05 15:42:30 -0500209
210 $this->session->unset_userdata($array_items);
Derek Jones8ede1a22011-10-05 13:34:52 -0500211
Andrey Andreevb339df32015-02-02 11:45:11 +0200212.. note:: In previous versions, the ``unset_userdata()`` method used
213 to accept an associative array of ``key => 'dummy value'``
214 pairs. This is no longer supported.
215
Derek Jones8ede1a22011-10-05 13:34:52 -0500216Flashdata
217=========
218
219CodeIgniter supports "flashdata", or session data that will only be
Andrey Andreev973a6542015-01-19 13:25:24 +0200220available for the next request, and is then automatically cleared.
Derek Jones8ede1a22011-10-05 13:34:52 -0500221
Andrey Andreev973a6542015-01-19 13:25:24 +0200222This can be very useful, especially for one-time informational, error or
223status messages (for example: "Record 2 deleted").
224
225It should be noted that flashdata variables are regular session vars,
226only marked in a specific way under the '__ci_vars' key (please don't touch
227that one, you've been warned).
228
229To mark an existing item as "flashdata"::
230
231 $this->session->mark_as_flash('item');
232
233If you want to mark multiple items as flashdata, simply pass the keys as an
234array::
235
236 $this->session->mark_as_flash(array('item', 'item2'));
Derek Jones8ede1a22011-10-05 13:34:52 -0500237
238To add flashdata::
239
Andrey Andreev973a6542015-01-19 13:25:24 +0200240 $_SESSION['item'] = 'value';
241 $this->session->mark_as_flash('item');
Derek Jones8ede1a22011-10-05 13:34:52 -0500242
Andrey Andreev973a6542015-01-19 13:25:24 +0200243Or alternatively, using the ``set_flashdata()`` method::
244
245 $this->session->set_flashdata('item', 'value');
Derek Jones8ede1a22011-10-05 13:34:52 -0500246
Michael5d4131b2013-09-30 12:22:29 +0300247You can also pass an array to ``set_flashdata()``, in the same manner as
248``set_userdata()``.
Derek Jones8ede1a22011-10-05 13:34:52 -0500249
Andrey Andreev973a6542015-01-19 13:25:24 +0200250Reading flashdata variables is the same as reading regular session data
251through ``$_SESSION``::
252
253 $_SESSION['item']
254
255.. important:: The ``userdata()`` method will NOT return flashdata items.
256
257However, if you want to be sure that you're reading "flashdata" (and not
258any other kind), you can also use the ``flashdata()`` method::
Derek Jones8ede1a22011-10-05 13:34:52 -0500259
260 $this->session->flashdata('item');
Johnathan Croom4beca5c2012-11-23 18:32:46 -0700261
Andrey Andreev973a6542015-01-19 13:25:24 +0200262Or to get an array with all flashdata, simply omit the key parameter::
Mike Funk7c26fab2012-02-24 09:45:02 -0500263
Andrey Andreevecc260e2014-01-24 14:20:13 +0200264 $this->session->flashdata();
Derek Jones8ede1a22011-10-05 13:34:52 -0500265
Andrey Andreev973a6542015-01-19 13:25:24 +0200266.. note:: The ``flashdata()`` method returns NULL if the item cannot be
267 found.
Derek Jones8ede1a22011-10-05 13:34:52 -0500268
269If you find that you need to preserve a flashdata variable through an
Andrey Andreev973a6542015-01-19 13:25:24 +0200270additional request, you can do so using the ``keep_flashdata()`` method.
Johnathan Croom4beca5c2012-11-23 18:32:46 -0700271You can either pass a single item or an array of flashdata items to keep.
Derek Jones8ede1a22011-10-05 13:34:52 -0500272
273::
274
275 $this->session->keep_flashdata('item');
Johnathan Croom4beca5c2012-11-23 18:32:46 -0700276 $this->session->keep_flashdata(array('item1', 'item2', 'item3'));
Derek Jones8ede1a22011-10-05 13:34:52 -0500277
dchill423169f262012-08-11 20:12:05 -0400278Tempdata
279========
280
281CodeIgniter also supports "tempdata", or session data with a specific
282expiration time. After the value expires, or the session expires or is
283deleted, the value is automatically removed.
284
Andrey Andreev973a6542015-01-19 13:25:24 +0200285Similarly to flashdata, tempdata variables are regular session vars that
286are marked in a specific way under the '__ci_vars' key (again, don't touch
287that one).
288
289To mark an existing item as "tempdata", simply pass its key and expiry time
290(in seconds!) to the ``mark_as_temp()`` method::
291
292 // 'item' will be erased after 300 seconds
293 $this->session->mark_as_temp('item', 300);
294
295You can mark multiple items as tempdata in two ways, depending on whether
296you want them all to have the same expiry time or not::
297
298 // Both 'item' and 'item2' will expire after 300 seconds
299 $this->session->mark_as_temp(array('item', 'item2'), 300);
300
301 // 'item' will be erased after 300 seconds, while 'item2'
302 // will do so after only 240 seconds
303 $this->session->mark_as_temp(array(
304 'item' => 300,
305 'item2' => 240
306 ));
307
dchill423169f262012-08-11 20:12:05 -0400308To add tempdata::
309
Andrey Andreev973a6542015-01-19 13:25:24 +0200310 $_SESSION['item'] = 'value';
311 $this->session->mark_as_temp('item', 300); // Expire in 5 minutes
dchill423169f262012-08-11 20:12:05 -0400312
Andrey Andreev973a6542015-01-19 13:25:24 +0200313Or alternatively, using the ``set_tempdata()`` method::
314
315 $this->session->set_tempdata('item', 'value', 300);
dchill423169f262012-08-11 20:12:05 -0400316
Michael5d4131b2013-09-30 12:22:29 +0300317You can also pass an array to ``set_tempdata()``::
dchill423169f262012-08-11 20:12:05 -0400318
319 $tempdata = array('newuser' => TRUE, 'message' => 'Thanks for joining!');
320
Andrey Andreev973a6542015-01-19 13:25:24 +0200321 $this->session->set_tempdata($tempdata, NULL, $expire);
dchill423169f262012-08-11 20:12:05 -0400322
Andrey Andreev973a6542015-01-19 13:25:24 +0200323.. note:: If the expiration is omitted or set to 0, the default
324 time-to-live value of 300 seconds (or 5 minutes) will be used.
dchill423169f262012-08-11 20:12:05 -0400325
Andrey Andreev973a6542015-01-19 13:25:24 +0200326To read a tempdata variable, again you can just access it through the
327``$_SESSION`` superglobal array::
328
329 $_SESSION['item']
330
331.. important:: The ``userdata()`` method will NOT return tempdata items.
332
333Or if you want to be sure that you're reading "flashdata" (and not any
334other kind), you can also use the ``tempdata()`` method::
dchill423169f262012-08-11 20:12:05 -0400335
336 $this->session->tempdata('item');
337
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200338And of course, if you want to retrieve all existing tempdata::
339
340 $this->session->tempdata();
341
Andrey Andreev973a6542015-01-19 13:25:24 +0200342.. note:: The ``tempdata()`` method returns NULL if the item cannot be
343 found.
344
345If you need to remove a tempdata value before it expires, you can directly
346unset it from the ``$_SESSION`` array::
347
348 unset($_SESSION['item']);
349
350However, this won't remove the marker that makes this specific item to be
351tempdata (it will be invalidated on the next HTTP request), so if you
352intend to reuse that same key in the same request, you'd want to use
353``unset_tempdata()``::
dchill423169f262012-08-11 20:12:05 -0400354
355 $this->session->unset_tempdata('item');
356
357Destroying a Session
358====================
359
Andrey Andreev973a6542015-01-19 13:25:24 +0200360To clear the current session (for example, during a logout), you may
361simply use either PHP's `session_destroy() <http://php.net/session_destroy>`_
362function, or the ``sess_destroy()`` method. Both will work in exactly the
363same way::
364
365 session_destroy();
366
367 // or
dchill423169f262012-08-11 20:12:05 -0400368
369 $this->session->sess_destroy();
370
Andrey Andreev973a6542015-01-19 13:25:24 +0200371.. note:: This must be the last session-related operation that you do
372 during the same request. All session data (including flashdata and
373 tempdata) will be destroyed permanently and functions will be
374 unusable during the same request after you destroy the session.
375
376Accessing session metadata
377==========================
378
379In previous CodeIgniter versions, the session data array included 4 items
380by default: 'session_id', 'ip_address', 'user_agent', 'last_activity'.
381
382This was due to the specifics of how sessions worked, but is now no longer
383necessary with our new implementation. However, it may happen that your
384application relied on these values, so here are alternative methods of
Andrey Andreevd8e25e92015-01-19 22:03:57 +0200385accessing them:
Andrey Andreev973a6542015-01-19 13:25:24 +0200386
387 - session_id: ``session_id()``
388 - ip_address: ``$_SERVER['REMOTE_ADDR']``
389 - user_agent: ``$this->input->user_agent()`` (unused by sessions)
390 - last_activity: Depends on the storage, no straightforward way. Sorry!
dchill423169f262012-08-11 20:12:05 -0400391
392Session Preferences
393===================
394
Andrey Andreev973a6542015-01-19 13:25:24 +0200395CodeIgniter will usually make everything work out of the box. However,
396Sessions are a very sensitive component of any application, so some
397careful configuration must be done. Please take your time to consider
398all of the options and their effects.
dchill423169f262012-08-11 20:12:05 -0400399
Andrey Andreev973a6542015-01-19 13:25:24 +0200400You'll find the following Session related preferences in your
401**application/config/config.php** file:
402
403======================== =============== ======================================== ============================================================================================
404Preference Default Options Description
405======================== =============== ======================================== ============================================================================================
406**sess_driver** files files/database/redis/memcached/*custom* The session storage driver to use.
vlakoffa2dee7d2015-01-20 04:22:29 +0100407**sess_cookie_name** ci_session [A-Za-z\_-] characters only The name used for the session cookie.
Andrey Andreev973a6542015-01-19 13:25:24 +0200408**sess_expiration** 7200 (2 hours) Time in seconds (integer) The number of seconds you would like the session to last.
409 If you would like a non-expiring session (until browser is closed) set the value to zero: 0
410**sess_save_path** NULL None Specifies the storage location, depends on the driver being used.
Andrey Andreev9a0e6602015-01-19 14:54:08 +0200411**sess_time_to_update** 300 Time in seconds (integer) This option controls how often the session class will regenerate itself and create a new
412 session ID. Setting it to 0 will disable session ID regeneration.
Andrey Andreev973a6542015-01-19 13:25:24 +0200413**sess_match_ip** FALSE TRUE/FALSE (boolean) Whether to validate the user's IP address when reading the session cookie.
414 Note that some ISPs dynamically changes the IP, so if you want a non-expiring session you
415 will likely set this to FALSE.
416======================== =============== ======================================== ============================================================================================
417
418.. note:: As a last resort, the Session library will try to fetch PHP's
419 session related INI settings, as well as legacy CI settings such as
420 'sess_expire_on_close' when any of the above is not configured.
421 However, you should never rely on this behavior as it can cause
422 unexpected results or be changed in the future. Please configure
423 everything properly.
dchill423169f262012-08-11 20:12:05 -0400424
425In addition to the values above, the cookie and native drivers apply the
426following configuration values shared by the :doc:`Input <input>` and
427:doc:`Security <security>` classes:
428
Andrey Andreev973a6542015-01-19 13:25:24 +0200429================== =============== ===========================================================================
430Preference Default Description
431================== =============== ===========================================================================
432**cookie_domain** '' The domain for which the session is applicable
433**cookie_path** / The path to which the session is applicable
434**cookie_secure** FALSE Whether to create the session cookie only on encrypted (HTTPS) connections
435================== =============== ===========================================================================
436
437.. note:: The 'cookie_httponly' setting doesn't have an effect on sessions.
438 Instead the HttpOnly parameter is always enabled, for security
439 reasons. Additionaly, the 'cookie_prefix' setting is completely
440 ignored.
dchill423169f262012-08-11 20:12:05 -0400441
442Session Drivers
443===============
444
Andrey Andreev973a6542015-01-19 13:25:24 +0200445As already mentioned, the Session library comes with 4 drivers, or storage
446engines, that you can use:
dchill423169f262012-08-11 20:12:05 -0400447
Andrey Andreev973a6542015-01-19 13:25:24 +0200448 - files
449 - database
450 - redis
451 - memcached
dchill423169f262012-08-11 20:12:05 -0400452
Andrey Andreev973a6542015-01-19 13:25:24 +0200453By default, the `Files Driver`_ will be used when a session is initialized,
454because it is the most safe choice and is expected to work everywhere
455(virtually every environment has a file system).
dchill423169f262012-08-11 20:12:05 -0400456
Andrey Andreev973a6542015-01-19 13:25:24 +0200457However, any other driver may be selected via the ``$config['sess_driver']``
458line in your **application/config/config.php** file, if you chose to do so.
459Have it in mind though, every driver has different caveats, so be sure to
460get yourself familiar with them (below) before you make that choice.
dchill423169f262012-08-11 20:12:05 -0400461
Andrey Andreev973a6542015-01-19 13:25:24 +0200462In addition, you may also create and use `Custom Drivers`_, if the ones
463provided by default don't satisfy your use case.
dchill423169f262012-08-11 20:12:05 -0400464
Andrey Andreev973a6542015-01-19 13:25:24 +0200465.. note:: In previous CodeIgniter versions, a different, "cookie driver"
466 was the only option and we have received negative feedback on not
467 providing that option. While we do listen to feedback from the
468 community, we want to warn you that it was dropped because it is
469 **unsafe** and we advise you NOT to try to replicate it via a
470 custom driver.
dchill423169f262012-08-11 20:12:05 -0400471
Andrey Andreev973a6542015-01-19 13:25:24 +0200472Files Driver
473------------
dchill423169f262012-08-11 20:12:05 -0400474
Andrey Andreev973a6542015-01-19 13:25:24 +0200475The 'files' driver uses your file system for storing session data.
dchill423169f262012-08-11 20:12:05 -0400476
Andrey Andreev973a6542015-01-19 13:25:24 +0200477It can safely be said that it works exactly like PHP's own default session
478implementation, but in case this is an important detail for you, have it
479mind that it is in fact not the same code and it has some limitations
480(and advantages).
dchill423169f262012-08-11 20:12:05 -0400481
Andrey Andreev973a6542015-01-19 13:25:24 +0200482To be more specific, it doesn't support PHP's `directory level and mode
483formats used in session.save_path
484<http://php.net/manual/en/session.configuration.php#ini.session.save-path>`_,
485and it has most of the options hard-coded for safety. Instead, only
486absolute paths are supported for ``$config['sess_save_path']``.
dchill423169f262012-08-11 20:12:05 -0400487
Andrey Andreev973a6542015-01-19 13:25:24 +0200488Another important thing that you should know, is to make sure that you
489don't use a publicly-readable or shared directory for storing your session
490files. Make sure that *only you* have access to see the contents of your
491chosen *sess_save_path* directory. Otherwise, anybody who can do that, can
492also steal any of the current sessions (also known as "session fixation"
493attack).
dchill423169f262012-08-11 20:12:05 -0400494
Andrey Andreev973a6542015-01-19 13:25:24 +0200495On UNIX-like operating systems, this is usually achieved by setting the
Andrey Andreev6e8a2022015-02-03 10:53:05 +02004960700 mode permissions on that directory via the `chmod` command, which
Andrey Andreev973a6542015-01-19 13:25:24 +0200497allows only the directory's owner to perform read and write operations on
498it. But be careful because the system user *running* the script is usually
499not your own, but something like 'www-data' instead, so only setting those
500permissions will probable break your application.
dchill423169f262012-08-11 20:12:05 -0400501
Andrey Andreev973a6542015-01-19 13:25:24 +0200502Instead, you should do something like this, depending on your environment
503::
dchill423169f262012-08-11 20:12:05 -0400504
Andrey Andreev973a6542015-01-19 13:25:24 +0200505 mkdir /<path to your application directory>/sessions/
Andrey Andreev6e8a2022015-02-03 10:53:05 +0200506 chmod 0700 /<path to your application directory>/sessions/
Andrey Andreev973a6542015-01-19 13:25:24 +0200507 chown www-data /<path to your application directory>/sessions/
dchill423169f262012-08-11 20:12:05 -0400508
Andrey Andreev973a6542015-01-19 13:25:24 +0200509Bonus Tip
510^^^^^^^^^
dchill423169f262012-08-11 20:12:05 -0400511
Andrey Andreev973a6542015-01-19 13:25:24 +0200512Some of you will probably opt to choose another session driver because
513file storage is usually slower. This is only half true.
dchill423169f262012-08-11 20:12:05 -0400514
Andrey Andreev973a6542015-01-19 13:25:24 +0200515A very basic test will probably trick you into believing that an SQL
516database is faster, but in 99% of the cases, this is only true while you
517only have a few current sessions. As the sessions count and server loads
518increase - which is the time when it matters - the file system will
519consistently outperform almost all relational database setups.
dchill423169f262012-08-11 20:12:05 -0400520
Andrey Andreev973a6542015-01-19 13:25:24 +0200521In addition, if performance is your only concern, you may want to look
522into using `tmpfs <http://eddmann.com/posts/storing-php-sessions-file-caches-in-memory-using-tmpfs/>`_,
523(warning: external resource), which can make your sessions blazing fast.
Derek Jones8ede1a22011-10-05 13:34:52 -0500524
Andrey Andreev973a6542015-01-19 13:25:24 +0200525Database Driver
526---------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500527
Andrey Andreev973a6542015-01-19 13:25:24 +0200528The 'database' driver uses a relational database such as MySQL or
529PostgreSQL to store sessions. This is a popular choice among many users,
530because it allows the developer easy access to the session data within
531an application - it is just another table in your database.
Derek Jones8ede1a22011-10-05 13:34:52 -0500532
Andrey Andreev973a6542015-01-19 13:25:24 +0200533However, there are some conditions that must be met:
Derek Jones8ede1a22011-10-05 13:34:52 -0500534
Andrey Andreev973a6542015-01-19 13:25:24 +0200535 - Only your **default** database connection (or the one that you access
536 as ``$this->db`` from your controllers) can be used.
537 - You can NOT use a persistent connection.
538 - You must have the :doc:`Query Builder </database/query_builder>`
539 enabled.
Derek Jones8ede1a22011-10-05 13:34:52 -0500540
Andrey Andreev973a6542015-01-19 13:25:24 +0200541In order to use the 'database' session driver, you must also create this
542table that we already mentioned and then set it as your
543``$config['sess_save_path']`` value.
544For example, if you would like to use 'ci_sessions' as your table name,
545you would do this:
546
547 $config['sess_driver'] = 'database';
548 $config['sess_save_path'] = 'ci_sessions';
549
550.. note:: If you've upgraded from a previous version of CodeIgniter and
551 you don't have 'sess_save_path' configured, then the Session
552 library will look for the old 'sess_table_name' setting and use
553 it instead. Please don't rely on this behavior as it will get
554 removed in the future.
555
556And then of course, create the database table ...
557
558For MySQL::
559
560 CREATE TABLE IF NOT EXISTS `ci_sessions` (
Andrey Andreev3053b812015-01-20 17:31:29 +0200561 `id` varchar(40) NOT NULL,
Andrey Andreev973a6542015-01-19 13:25:24 +0200562 `ip_address` varchar(45) NOT NULL,
563 `timestamp` int(10) unsigned DEFAULT 0 NOT NULL,
564 `data` blob DEFAULT '' NOT NULL,
Andrey Andreev9f4eda72015-01-26 11:58:07 +0200565 PRIMARY KEY (id),
Andrey Andreev973a6542015-01-19 13:25:24 +0200566 KEY `ci_sessions_timestamp` (`timestamp`)
Derek Jones4b83d912011-10-05 15:42:30 -0500567 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500568
Andrey Andreev973a6542015-01-19 13:25:24 +0200569For PostgreSQL::
Andrey Andreev69e1b4f2014-01-07 17:29:25 +0200570
Andrey Andreev973a6542015-01-19 13:25:24 +0200571 CREATE TABLE "ci_sessions" (
Andrey Andreev3053b812015-01-20 17:31:29 +0200572 "id" varchar(40) NOT NULL,
Andrey Andreev973a6542015-01-19 13:25:24 +0200573 "ip_address" varchar(45) NOT NULL,
574 "timestamp" bigint DEFAULT 0 NOT NULL,
575 "data" text DEFAULT '' NOT NULL,
Master Yoda08a92bc2015-01-26 00:46:57 -0800576 PRIMARY KEY ("id")
Andrey Andreev69e1b4f2014-01-07 17:29:25 +0200577 );
578
Andrey Andreev973a6542015-01-19 13:25:24 +0200579 CREATE INDEX "ci_sessions_timestamp" ON "ci_sessions" ("timestamp");
Andrey Andreev69e1b4f2014-01-07 17:29:25 +0200580
Andrey Andreev973a6542015-01-19 13:25:24 +0200581However, if you want to turn on the *sess_match_ip* setting, you should
582also do the following, after creating the table::
Derek Jones8ede1a22011-10-05 13:34:52 -0500583
Andrey Andreev973a6542015-01-19 13:25:24 +0200584 // Works both on MySQL and PostgreSQL
Andrey Andreev3053b812015-01-20 17:31:29 +0200585 ALTER TABLE ci_sessions ADD CONSTRAINT ci_sessions_id_ip UNIQUE (id, ip_address);
Derek Jones8ede1a22011-10-05 13:34:52 -0500586
Andrey Andreev973a6542015-01-19 13:25:24 +0200587.. important:: Only MySQL and PostgreSQL databases are officially
Andrey Andreev052c02f2015-01-19 13:30:30 +0200588 supported, due to lack of advisory locking mechanisms on other
589 platforms. Using sessions without locks can cause all sorts of
590 problems, especially with heavy usage of AJAX, and we will not
591 support such cases. Use ``session_write_close()`` after you've
592 done processing session data if you're having performance
593 issues.
Derek Jones8ede1a22011-10-05 13:34:52 -0500594
Andrey Andreev973a6542015-01-19 13:25:24 +0200595Redis Driver
596------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500597
Andrey Andreev973a6542015-01-19 13:25:24 +0200598Redis is a storage engine typically used for caching and popular because
599of its high performance, which is also probably your reason to use the
600'redis' session driver.
Derek Jones8ede1a22011-10-05 13:34:52 -0500601
Andrey Andreev973a6542015-01-19 13:25:24 +0200602The downside is that it is not as ubiquitous as relational databases and
603requires the `phpredis <https://github.com/nicolasff/phpredis>`_ PHP
604extension to be installed on your system, and that one doesn't come
605bundled with PHP.
606Chances are, you're only be using the 'redis' driver only if you're already
607both familiar with Redis and using it for other purposes.
Derek Jones8ede1a22011-10-05 13:34:52 -0500608
Andrey Andreev973a6542015-01-19 13:25:24 +0200609Just as with the 'files' and 'database' drivers, you must also configure
610the storage location for your sessions via the
611``$config['sess_save_path']`` setting.
612The format here is a bit different and complicated at the same time. It is
613best explained by the *phpredis* extension's README file, so we'll simply
614link you to it:
Derek Jones8ede1a22011-10-05 13:34:52 -0500615
Andrey Andreev973a6542015-01-19 13:25:24 +0200616 https://github.com/phpredis/phpredis#php-session-handler
617
618.. warning:: CodeIgniter's Session library does NOT use the actual 'redis'
619 ``session.save_handler``. Take note **only** of the path format in
620 the link above.
621
622For the most common case however, a simple ``host:port`` pair should be
623sufficient::
624
625 $config['sess_driver'] = 'redis';
626 $config['sess_save_path'] = 'tcp://localhost:6379';
627
628Memcached Driver
629----------------
630
631The 'memcached' driver is very similar to the 'redis' one in all of its
632properties, except perhaps for availability, because PHP's `Memcached
633<http://php.net/memcached>`_ extension is distributed via PECL and some
634Linux distrubutions make it available as an easy to install package.
635
636Other than that, and without any intentional bias towards Redis, there's
637not much different to be said about Memcached - it is also a popular
638product that is usually used for caching and famed for its speed.
639
640However, it is worth noting that the only guarantee given by Memcached
641is that setting value X to expire after Y seconds will result in it being
642deleted after Y seconds have passed (but not necessarily that it won't
643expire earlier than that time). This happens very rarely, but should be
644considered as it may result in loss of sessions.
645
646The ``$config['sess_save_path']`` format is fairly straightforward here,
647being just a ``host:port`` pair::
648
649 $config['sess_driver'] = 'memcached';
650 $config['sess_save_path'] = 'localhost:11211';
651
652Bonus Tip
653^^^^^^^^^
654
655Multi-server configuration with an optional *weight* parameter as the
656third colon-separated (``:weight``) value is also supported, but we have
657to note that we haven't tested if that is reliable.
658
659If you want to experiment with this feature (on your own risk), simply
660separate the multiple server paths with commas::
661
662 // localhost will be given higher priority (5) here,
663 // compared to 192.0.2.1 with a weight of 1.
664 $config['sess_save_path'] = 'localhost:11211:5,192.0.2.1:11211:1';
Derek Jones8ede1a22011-10-05 13:34:52 -0500665
dchill423169f262012-08-11 20:12:05 -0400666Custom Drivers
667--------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500668
Andrey Andreev973a6542015-01-19 13:25:24 +0200669You may also create your own, custom session drivers. However, have it in
670mind that this is typically not an easy task, as it takes a lot of
671knowledge to do it properly.
Derek Jones8ede1a22011-10-05 13:34:52 -0500672
Andrey Andreev973a6542015-01-19 13:25:24 +0200673You need to know not only how sessions work in general, but also how they
674work specifically in PHP, how the underlying storage mechanism works, how
675to handle concurrency, avoid deadlocks (but NOT through lack of locks) and
676last but not least - how to handle the potential security issues, which
677is far from trivial.
Derek Jones8ede1a22011-10-05 13:34:52 -0500678
Andrey Andreev973a6542015-01-19 13:25:24 +0200679Long story short - if you don't know how to do that already in raw PHP,
680you shouldn't be trying to do it within CodeIgniter either. You've been
681warned.
Andrey Andreevb57b2ad2014-01-03 12:02:08 +0200682
Andrey Andreev973a6542015-01-19 13:25:24 +0200683If you only want to add some extra functionality to your sessions, just
684extend the base Session class, which is a lot more easier. Read the
685:doc:`Creating Libraries <../general/creating_libraries>` article to
686learn how to do that.
687
688Now, to the point - there are three general rules that you must follow
689when creating a session driver for CodeIgniter:
690
691 - Put your driver's file under **application/libraries/Session/drivers/**
692 and follow the naming conventions used by the Session class.
693
694 For example, if you were to create a 'dummy' driver, you would have
695 a ``Session_dummy_driver`` class name, that is declared in
696 *application/libraries/Session/drivers/Session_dummy_driver.php*.
697
698 - Extend the ``CI_Session_driver`` class.
699
700 This is just a basic class with a few internal helper methods. It is
701 also extendable like any other library, if you really need to do that,
702 but we are not going to explain how ... if you're familiar with how
703 class extensions/overrides work in CI, then you already know how to do
704 it. If not, well, you shouldn't be doing it in the first place.
705
706
707 - Implement the `SessionHandlerInterface
708 <http://php.net/sessionhandlerinterface>`_ interface.
709
710 .. note:: You may notice that ``SessionHandlerInterface`` is provided
711 by PHP since version 5.4.0. CodeIgniter will automatically declare
712 the same interface if you're running an older PHP version.
713
714 The link will explain why and how.
715
716So, based on our 'dummy' driver example above, you'd end up with something
717like this::
718
719 // application/libraries/Session/drivers/Session_dummy_driver.php:
720
721 class CI_Session_dummy_driver extends CI_Session_driver implements SessionHandlerInterface
722 {
723
724 public function __construct(&$params)
dchill423169f262012-08-11 20:12:05 -0400725 {
Andrey Andreev973a6542015-01-19 13:25:24 +0200726 // DO NOT forget this
727 parent::__construct($params);
728
729 // Configuration & other initializations
dchill423169f262012-08-11 20:12:05 -0400730 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500731
Andrey Andreev973a6542015-01-19 13:25:24 +0200732 public function open($save_path, $name)
dchill423169f262012-08-11 20:12:05 -0400733 {
Andrey Andreev973a6542015-01-19 13:25:24 +0200734 // Initialize storage mechanism (connection)
dchill423169f262012-08-11 20:12:05 -0400735 }
736
Andrey Andreev973a6542015-01-19 13:25:24 +0200737 public function read($session_id)
dchill423169f262012-08-11 20:12:05 -0400738 {
Andrey Andreev973a6542015-01-19 13:25:24 +0200739 // Read session data (if exists), acquire locks
dchill423169f262012-08-11 20:12:05 -0400740 }
741
Andrey Andreev973a6542015-01-19 13:25:24 +0200742 public function write($session_id, $session_data)
dchill423169f262012-08-11 20:12:05 -0400743 {
Andrey Andreev973a6542015-01-19 13:25:24 +0200744 // Create / update session data (it might not exist!)
dchill423169f262012-08-11 20:12:05 -0400745 }
746
Andrey Andreev973a6542015-01-19 13:25:24 +0200747 public function close()
dchill423169f262012-08-11 20:12:05 -0400748 {
Andrey Andreev973a6542015-01-19 13:25:24 +0200749 // Free locks, close connections / streams / etc.
750 }
751
752 public function destroy($session_id)
753 {
754 // Call close() method & destroy data for current session (order may differ)
755 }
756
757 public function gc($maxlifetime)
758 {
759 // Erase data for expired sessions
dchill423169f262012-08-11 20:12:05 -0400760 }
Andrey Andreevb57b2ad2014-01-03 12:02:08 +0200761
dchill423169f262012-08-11 20:12:05 -0400762 }
763
Andrey Andreev973a6542015-01-19 13:25:24 +0200764If you've done everything properly, you can now set your *sess_driver*
765configuration value to 'dummy' and use your own driver. Congratulations!
Michael1c7438f2013-10-06 15:21:21 +0300766
Michael1c7438f2013-10-06 15:21:21 +0300767***************
768Class Reference
769***************
770
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200771.. php:class:: CI_Session
Michael1c7438f2013-10-06 15:21:21 +0300772
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200773 .. php:method:: userdata([$key = NULL])
Michaele0a631c2013-10-20 10:40:51 +0300774
Andrey Andreev973a6542015-01-19 13:25:24 +0200775 :param mixed $key: Session item key or NULL
776 :returns: Value of the specified item key, or an array of all userdata
Andrey Andreev28c2c972014-02-08 04:27:48 +0200777 :rtype: mixed
Michaele0a631c2013-10-20 10:40:51 +0300778
Andrey Andreev973a6542015-01-19 13:25:24 +0200779 Gets the value for a specific ``$_SESSION`` item, or an
780 array of all "userdata" items if not key was specified.
781
782 .. note:: This is a legacy method kept only for backwards
783 compatibility with older applications. You should
784 directly access ``$_SESSION`` instead.
Michaele0a631c2013-10-20 10:40:51 +0300785
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200786 .. php:method:: all_userdata()
Michaele0a631c2013-10-20 10:40:51 +0300787
Andrey Andreev28c2c972014-02-08 04:27:48 +0200788 :returns: An array of all userdata
789 :rtype: array
Michaele0a631c2013-10-20 10:40:51 +0300790
Andrey Andreev973a6542015-01-19 13:25:24 +0200791 Returns an array containing all "userdata" items.
Michaele0a631c2013-10-20 10:40:51 +0300792
Andrey Andreev973a6542015-01-19 13:25:24 +0200793 .. note:: This method is DEPRECATED. Use ``userdata()``
794 with no parameters instead.
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200795
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200796 .. php:method:: &get_usedata()
Michaele0a631c2013-10-20 10:40:51 +0300797
Andrey Andreev973a6542015-01-19 13:25:24 +0200798 :returns: A reference to ``$_SESSION``
799 :rtype: array
Michaele0a631c2013-10-20 10:40:51 +0300800
Andrey Andreev973a6542015-01-19 13:25:24 +0200801 Returns a reference to the ``$_SESSION`` array.
Michael1c7438f2013-10-06 15:21:21 +0300802
Andrey Andreev973a6542015-01-19 13:25:24 +0200803 .. note:: This is a legacy method kept only for backwards
804 compatibility with older applications.
Michael1c7438f2013-10-06 15:21:21 +0300805
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200806 .. php:method:: has_userdata($key)
Michael1c7438f2013-10-06 15:21:21 +0300807
Andrey Andreev973a6542015-01-19 13:25:24 +0200808 :param string $key: Session item key
809 :returns: TRUE if the specified key exists, FALSE if not
Andrey Andreev28c2c972014-02-08 04:27:48 +0200810 :rtype: bool
Michael1c7438f2013-10-06 15:21:21 +0300811
Andrey Andreev973a6542015-01-19 13:25:24 +0200812 Checks if an item exists in ``$_SESSION``.
Michael1c7438f2013-10-06 15:21:21 +0300813
Andrey Andreev973a6542015-01-19 13:25:24 +0200814 .. note:: This is a legacy method kept only for backwards
815 compatibility with older applications. It is just
816 an alias for ``isset($_SESSION[$key])`` - please
817 use that instead.
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200818
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200819 .. php:method:: set_userdata($data[, $value = NULL])
Andrey Andreev973a6542015-01-19 13:25:24 +0200820
821 :param mixed $data: An array of key/value pairs to set as session data, or the key for a single item
822 :param mixed $value: The value to set for a specific session item, if $data is a key
823 :rtype: void
824
825 Assigns data to the ``$_SESSION`` superglobal.
826
827 .. note:: This is a legacy method kept only for backwards
828 compatibility with older applications.
829
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200830 .. php:method:: unset_userdata($key)
Andrey Andreev973a6542015-01-19 13:25:24 +0200831
832 :param mixed $key: Key for the session data item to unset, or an array of multiple keys
833 :rtype: void
834
835 Unsets the specified key(s) from the ``$_SESSION``
836 superglobal.
837
838 .. note:: This is a legacy method kept only for backwards
839 compatibility with older applications. It is just
840 an alias for ``unset($_SESSION[$key])`` - please
841 use that instead.
842
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200843 .. php:method:: mark_as_flash($key)
Andrey Andreev973a6542015-01-19 13:25:24 +0200844
845 :param mixed $key: Key to mark as flashdata, or an array of multiple keys
846 :returns: TRUE on success, FALSE on failure
847 :rtype: bool
848
849 Marks a ``$_SESSION`` item key (or multiple ones) as
850 "flashdata".
851
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200852 .. php:method:: get_flash_keys()
Andrey Andreev973a6542015-01-19 13:25:24 +0200853
854 :returns: Array containing the keys of all "flashdata" items.
855 :rtype: array
856
857 Gets a list of all ``$_SESSION`` that have been marked as
858 "flashdata".
859
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200860 .. php:method:: umark_flash($key)
Andrey Andreev973a6542015-01-19 13:25:24 +0200861
862 :param mixed $key: Key to be un-marked as flashdata, or an array of multiple keys
863 :rtype: void
864
865 Unmarks a ``$_SESSION`` item key (or multiple ones) as
866 "flashdata".
867
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200868 .. php:method:: flashdata([$key = NULL])
Andrey Andreev973a6542015-01-19 13:25:24 +0200869
870 :param mixed $key: Flashdata item key or NULL
871 :returns: Value of the specified item key, or an array of all flashdata
Andrey Andreev28c2c972014-02-08 04:27:48 +0200872 :rtype: mixed
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200873
Andrey Andreev973a6542015-01-19 13:25:24 +0200874 Gets the value for a specific ``$_SESSION`` item that has
875 been marked as "flashdata", or an array of all "flashdata"
876 items if no key was specified.
877
878 .. note:: This is a legacy method kept only for backwards
879 compatibility with older applications. You should
880 directly access ``$_SESSION`` instead.
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200881
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200882 .. php:method:: keep_flashdata($key)
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200883
Andrey Andreev973a6542015-01-19 13:25:24 +0200884 :param mixed $key: Flashdata key to keep, or an array of multiple keys
vlakoffa2dee7d2015-01-20 04:22:29 +0100885 :returns: TRUE on success, FALSE on failure
Andrey Andreev973a6542015-01-19 13:25:24 +0200886 :rtype: bool
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200887
Andrey Andreev973a6542015-01-19 13:25:24 +0200888 Retains the specified session data key(s) as "flashdata"
889 through the next request.
Michael1c7438f2013-10-06 15:21:21 +0300890
Andrey Andreev973a6542015-01-19 13:25:24 +0200891 .. note:: This is a legacy method kept only for backwards
892 compatibility with older applications. It is just
893 an alias for the ``mark_as_flash()`` method.
Michael1c7438f2013-10-06 15:21:21 +0300894
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200895 .. php:method:: set_flashdata($data[, $value = NULL])
Michael1c7438f2013-10-06 15:21:21 +0300896
Andrey Andreev973a6542015-01-19 13:25:24 +0200897 :param mixed $data: An array of key/value pairs to set as flashdata, or the key for a single item
898 :param mixed $value: The value to set for a specific session item, if $data is a key
Andrey Andreev28c2c972014-02-08 04:27:48 +0200899 :rtype: void
Michaele0a631c2013-10-20 10:40:51 +0300900
Andrey Andreev973a6542015-01-19 13:25:24 +0200901 Assigns data to the ``$_SESSION`` superglobal and marks it
902 as "flashdata".
Michaele0a631c2013-10-20 10:40:51 +0300903
Andrey Andreev973a6542015-01-19 13:25:24 +0200904 .. note:: This is a legacy method kept only for backwards
905 compatibility with older applications.
Michael1c7438f2013-10-06 15:21:21 +0300906
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200907 .. php:method:: mark_as_temp($key[, $ttl = 300])
Andrey Andreev973a6542015-01-19 13:25:24 +0200908
909 :param mixed $key: Key to mark as tempdata, or an array of multiple keys
910 :param int $ttl: Time-to-live value for the tempdata, in seconds
911 :returns: TRUE on success, FALSE on failure
912 :rtype: bool
913
914 Marks a ``$_SESSION`` item key (or multiple ones) as
915 "tempdata".
916
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200917 .. php:method:: get_temp_keys()
Andrey Andreev973a6542015-01-19 13:25:24 +0200918
919 :returns: Array containing the keys of all "tempdata" items.
920 :rtype: array
921
922 Gets a list of all ``$_SESSION`` that have been marked as
923 "tempdata".
924
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200925 .. php:method:: umark_temp($key)
Andrey Andreev973a6542015-01-19 13:25:24 +0200926
927 :param mixed $key: Key to be un-marked as tempdata, or an array of multiple keys
928 :rtype: void
929
930 Unmarks a ``$_SESSION`` item key (or multiple ones) as
931 "tempdata".
932
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200933 .. php:method:: tempdata([$key = NULL])
Andrey Andreev973a6542015-01-19 13:25:24 +0200934
935 :param mixed $key: Tempdata item key or NULL
936 :returns: Value of the specified item key, or an array of all tempdata
Andrey Andreev28c2c972014-02-08 04:27:48 +0200937 :rtype: mixed
Michael1c7438f2013-10-06 15:21:21 +0300938
Andrey Andreev973a6542015-01-19 13:25:24 +0200939 Gets the value for a specific ``$_SESSION`` item that has
940 been marked as "tempdata", or an array of all "tempdata"
941 items if no key was specified.
942
943 .. note:: This is a legacy method kept only for backwards
944 compatibility with older applications. You should
945 directly access ``$_SESSION`` instead.
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200946
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200947 .. php:method:: set_tempdata($data[, $value = NULL])
Michael1c7438f2013-10-06 15:21:21 +0300948
Andrey Andreev973a6542015-01-19 13:25:24 +0200949 :param mixed $data: An array of key/value pairs to set as tempdata, or the key for a single item
950 :param mixed $value: The value to set for a specific session item, if $data is a key
951 :param int $ttl: Time-to-live value for the tempdata item(s), in seconds
Andrey Andreev28c2c972014-02-08 04:27:48 +0200952 :rtype: void
Michael1c7438f2013-10-06 15:21:21 +0300953
Andrey Andreev973a6542015-01-19 13:25:24 +0200954 Assigns data to the ``$_SESSION`` superglobal and marks it
955 as "tempdata".
Michael1c7438f2013-10-06 15:21:21 +0300956
Andrey Andreev973a6542015-01-19 13:25:24 +0200957 .. note:: This is a legacy method kept only for backwards
958 compatibility with older applications.
Michael1c7438f2013-10-06 15:21:21 +0300959
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200960 .. php:method:: sess_regenerate([$destroy = FALSE])
Michael1c7438f2013-10-06 15:21:21 +0300961
Andrey Andreev973a6542015-01-19 13:25:24 +0200962 :param bool $destroy: Whether to destroy session data
Andrey Andreev28c2c972014-02-08 04:27:48 +0200963 :rtype: void
Michael1c7438f2013-10-06 15:21:21 +0300964
Andrey Andreev973a6542015-01-19 13:25:24 +0200965 Regenerate session ID, optionally destroying the current
966 session's data.
Michael1c7438f2013-10-06 15:21:21 +0300967
Andrey Andreev973a6542015-01-19 13:25:24 +0200968 .. note:: This method is just an alias for PHP's native
969 `session_regenerate_id()
970 <http://php.net/session_regenerate_id>`_ function.
Michael1c7438f2013-10-06 15:21:21 +0300971
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200972 .. php:method:: sess_destroy()
Andrey Andreev973a6542015-01-19 13:25:24 +0200973
974 :rtype: void
975
976 Destroys the current session.
977
978 .. note:: This must be the *last* session-related function
979 that you call. All session data will be lost after
980 you do that.
981
982 .. note:: This method is just an alias for PHP's native
983 `session_destroy()
984 <http://php.net/session_destroy>`_ function.
985
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200986 .. php:method:: __get($key)
Andrey Andreev973a6542015-01-19 13:25:24 +0200987
988 :param string $key: Session item key
989 :returns: The requested session data item, or NULL if it doesn't exist
990 :rtype: mixed
991
992 A magic method that allows you to use
993 ``$this->session->item`` instead of ``$_SESSION['item']``,
994 if that's what you prefer.
995
996 It will also return the session ID by calling
997 ``session_id()`` if you try to access
998 ``$this->session->session_id``.
999
Andrey Andreevcd3d9db2015-02-02 13:41:01 +02001000 .. php:method:: __set($key, $value)
Andrey Andreev973a6542015-01-19 13:25:24 +02001001
1002 :param string $key: Session item key
1003 :param mixed $value: Value to assign to the session item key
1004 :returns: void
1005
1006 A magic method that allows you to assign items to
1007 ``$_SESSION`` by accessing them as ``$this->session``
1008 properties::
1009
1010 $this->session->foo = 'bar';
1011
1012 // Results in:
1013 // $_SESSION['foo'] = 'bar';