blob: be1a39ef2db8bca1c89fa022c8601b094e8885fb [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
Andrey Andreev973a6542015-01-19 13:25:24 +020050.. important:: Because the :doc:`Loader Class <../loader>` is instantiated
51 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 Andreev973a6542015-01-19 13:25:24 +0200206This method also accepts an associative array of items to unset::
Derek Jones8ede1a22011-10-05 13:34:52 -0500207
Derek Jones4b83d912011-10-05 15:42:30 -0500208 $array_items = array('username' => '', 'email' => '');
209
210 $this->session->unset_userdata($array_items);
Derek Jones8ede1a22011-10-05 13:34:52 -0500211
Derek Jones8ede1a22011-10-05 13:34:52 -0500212Flashdata
213=========
214
215CodeIgniter supports "flashdata", or session data that will only be
Andrey Andreev973a6542015-01-19 13:25:24 +0200216available for the next request, and is then automatically cleared.
Derek Jones8ede1a22011-10-05 13:34:52 -0500217
Andrey Andreev973a6542015-01-19 13:25:24 +0200218This can be very useful, especially for one-time informational, error or
219status messages (for example: "Record 2 deleted").
220
221It should be noted that flashdata variables are regular session vars,
222only marked in a specific way under the '__ci_vars' key (please don't touch
223that one, you've been warned).
224
225To mark an existing item as "flashdata"::
226
227 $this->session->mark_as_flash('item');
228
229If you want to mark multiple items as flashdata, simply pass the keys as an
230array::
231
232 $this->session->mark_as_flash(array('item', 'item2'));
Derek Jones8ede1a22011-10-05 13:34:52 -0500233
234To add flashdata::
235
Andrey Andreev973a6542015-01-19 13:25:24 +0200236 $_SESSION['item'] = 'value';
237 $this->session->mark_as_flash('item');
Derek Jones8ede1a22011-10-05 13:34:52 -0500238
Andrey Andreev973a6542015-01-19 13:25:24 +0200239Or alternatively, using the ``set_flashdata()`` method::
240
241 $this->session->set_flashdata('item', 'value');
Derek Jones8ede1a22011-10-05 13:34:52 -0500242
Michael5d4131b2013-09-30 12:22:29 +0300243You can also pass an array to ``set_flashdata()``, in the same manner as
244``set_userdata()``.
Derek Jones8ede1a22011-10-05 13:34:52 -0500245
Andrey Andreev973a6542015-01-19 13:25:24 +0200246Reading flashdata variables is the same as reading regular session data
247through ``$_SESSION``::
248
249 $_SESSION['item']
250
251.. important:: The ``userdata()`` method will NOT return flashdata items.
252
253However, if you want to be sure that you're reading "flashdata" (and not
254any other kind), you can also use the ``flashdata()`` method::
Derek Jones8ede1a22011-10-05 13:34:52 -0500255
256 $this->session->flashdata('item');
Johnathan Croom4beca5c2012-11-23 18:32:46 -0700257
Andrey Andreev973a6542015-01-19 13:25:24 +0200258Or to get an array with all flashdata, simply omit the key parameter::
Mike Funk7c26fab2012-02-24 09:45:02 -0500259
Andrey Andreevecc260e2014-01-24 14:20:13 +0200260 $this->session->flashdata();
Derek Jones8ede1a22011-10-05 13:34:52 -0500261
Andrey Andreev973a6542015-01-19 13:25:24 +0200262.. note:: The ``flashdata()`` method returns NULL if the item cannot be
263 found.
Derek Jones8ede1a22011-10-05 13:34:52 -0500264
265If you find that you need to preserve a flashdata variable through an
Andrey Andreev973a6542015-01-19 13:25:24 +0200266additional request, you can do so using the ``keep_flashdata()`` method.
Johnathan Croom4beca5c2012-11-23 18:32:46 -0700267You can either pass a single item or an array of flashdata items to keep.
Derek Jones8ede1a22011-10-05 13:34:52 -0500268
269::
270
271 $this->session->keep_flashdata('item');
Johnathan Croom4beca5c2012-11-23 18:32:46 -0700272 $this->session->keep_flashdata(array('item1', 'item2', 'item3'));
Derek Jones8ede1a22011-10-05 13:34:52 -0500273
dchill423169f262012-08-11 20:12:05 -0400274Tempdata
275========
276
277CodeIgniter also supports "tempdata", or session data with a specific
278expiration time. After the value expires, or the session expires or is
279deleted, the value is automatically removed.
280
Andrey Andreev973a6542015-01-19 13:25:24 +0200281Similarly to flashdata, tempdata variables are regular session vars that
282are marked in a specific way under the '__ci_vars' key (again, don't touch
283that one).
284
285To mark an existing item as "tempdata", simply pass its key and expiry time
286(in seconds!) to the ``mark_as_temp()`` method::
287
288 // 'item' will be erased after 300 seconds
289 $this->session->mark_as_temp('item', 300);
290
291You can mark multiple items as tempdata in two ways, depending on whether
292you want them all to have the same expiry time or not::
293
294 // Both 'item' and 'item2' will expire after 300 seconds
295 $this->session->mark_as_temp(array('item', 'item2'), 300);
296
297 // 'item' will be erased after 300 seconds, while 'item2'
298 // will do so after only 240 seconds
299 $this->session->mark_as_temp(array(
300 'item' => 300,
301 'item2' => 240
302 ));
303
dchill423169f262012-08-11 20:12:05 -0400304To add tempdata::
305
Andrey Andreev973a6542015-01-19 13:25:24 +0200306 $_SESSION['item'] = 'value';
307 $this->session->mark_as_temp('item', 300); // Expire in 5 minutes
dchill423169f262012-08-11 20:12:05 -0400308
Andrey Andreev973a6542015-01-19 13:25:24 +0200309Or alternatively, using the ``set_tempdata()`` method::
310
311 $this->session->set_tempdata('item', 'value', 300);
dchill423169f262012-08-11 20:12:05 -0400312
Michael5d4131b2013-09-30 12:22:29 +0300313You can also pass an array to ``set_tempdata()``::
dchill423169f262012-08-11 20:12:05 -0400314
315 $tempdata = array('newuser' => TRUE, 'message' => 'Thanks for joining!');
316
Andrey Andreev973a6542015-01-19 13:25:24 +0200317 $this->session->set_tempdata($tempdata, NULL, $expire);
dchill423169f262012-08-11 20:12:05 -0400318
Andrey Andreev973a6542015-01-19 13:25:24 +0200319.. note:: If the expiration is omitted or set to 0, the default
320 time-to-live value of 300 seconds (or 5 minutes) will be used.
dchill423169f262012-08-11 20:12:05 -0400321
Andrey Andreev973a6542015-01-19 13:25:24 +0200322To read a tempdata variable, again you can just access it through the
323``$_SESSION`` superglobal array::
324
325 $_SESSION['item']
326
327.. important:: The ``userdata()`` method will NOT return tempdata items.
328
329Or if you want to be sure that you're reading "flashdata" (and not any
330other kind), you can also use the ``tempdata()`` method::
dchill423169f262012-08-11 20:12:05 -0400331
332 $this->session->tempdata('item');
333
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200334And of course, if you want to retrieve all existing tempdata::
335
336 $this->session->tempdata();
337
Andrey Andreev973a6542015-01-19 13:25:24 +0200338.. note:: The ``tempdata()`` method returns NULL if the item cannot be
339 found.
340
341If you need to remove a tempdata value before it expires, you can directly
342unset it from the ``$_SESSION`` array::
343
344 unset($_SESSION['item']);
345
346However, this won't remove the marker that makes this specific item to be
347tempdata (it will be invalidated on the next HTTP request), so if you
348intend to reuse that same key in the same request, you'd want to use
349``unset_tempdata()``::
dchill423169f262012-08-11 20:12:05 -0400350
351 $this->session->unset_tempdata('item');
352
353Destroying a Session
354====================
355
Andrey Andreev973a6542015-01-19 13:25:24 +0200356To clear the current session (for example, during a logout), you may
357simply use either PHP's `session_destroy() <http://php.net/session_destroy>`_
358function, or the ``sess_destroy()`` method. Both will work in exactly the
359same way::
360
361 session_destroy();
362
363 // or
dchill423169f262012-08-11 20:12:05 -0400364
365 $this->session->sess_destroy();
366
Andrey Andreev973a6542015-01-19 13:25:24 +0200367.. note:: This must be the last session-related operation that you do
368 during the same request. All session data (including flashdata and
369 tempdata) will be destroyed permanently and functions will be
370 unusable during the same request after you destroy the session.
371
372Accessing session metadata
373==========================
374
375In previous CodeIgniter versions, the session data array included 4 items
376by default: 'session_id', 'ip_address', 'user_agent', 'last_activity'.
377
378This was due to the specifics of how sessions worked, but is now no longer
379necessary with our new implementation. However, it may happen that your
380application relied on these values, so here are alternative methods of
381accessing them::
382
383 - session_id: ``session_id()``
384 - ip_address: ``$_SERVER['REMOTE_ADDR']``
385 - user_agent: ``$this->input->user_agent()`` (unused by sessions)
386 - last_activity: Depends on the storage, no straightforward way. Sorry!
dchill423169f262012-08-11 20:12:05 -0400387
388Session Preferences
389===================
390
Andrey Andreev973a6542015-01-19 13:25:24 +0200391CodeIgniter will usually make everything work out of the box. However,
392Sessions are a very sensitive component of any application, so some
393careful configuration must be done. Please take your time to consider
394all of the options and their effects.
dchill423169f262012-08-11 20:12:05 -0400395
Andrey Andreev973a6542015-01-19 13:25:24 +0200396You'll find the following Session related preferences in your
397**application/config/config.php** file:
398
399======================== =============== ======================================== ============================================================================================
400Preference Default Options Description
401======================== =============== ======================================== ============================================================================================
402**sess_driver** files files/database/redis/memcached/*custom* The session storage driver to use.
403**sess_cookie_name** ci_session [A-Za-z_-] characters only The name used for the session cookie.
404**sess_expiration** 7200 (2 hours) Time in seconds (integer) The number of seconds you would like the session to last.
405 If you would like a non-expiring session (until browser is closed) set the value to zero: 0
406**sess_save_path** NULL None Specifies the storage location, depends on the driver being used.
Andrey Andreev9a0e6602015-01-19 14:54:08 +0200407**sess_time_to_update** 300 Time in seconds (integer) This option controls how often the session class will regenerate itself and create a new
408 session ID. Setting it to 0 will disable session ID regeneration.
Andrey Andreev973a6542015-01-19 13:25:24 +0200409**sess_match_ip** FALSE TRUE/FALSE (boolean) Whether to validate the user's IP address when reading the session cookie.
410 Note that some ISPs dynamically changes the IP, so if you want a non-expiring session you
411 will likely set this to FALSE.
412======================== =============== ======================================== ============================================================================================
413
414.. note:: As a last resort, the Session library will try to fetch PHP's
415 session related INI settings, as well as legacy CI settings such as
416 'sess_expire_on_close' when any of the above is not configured.
417 However, you should never rely on this behavior as it can cause
418 unexpected results or be changed in the future. Please configure
419 everything properly.
dchill423169f262012-08-11 20:12:05 -0400420
421In addition to the values above, the cookie and native drivers apply the
422following configuration values shared by the :doc:`Input <input>` and
423:doc:`Security <security>` classes:
424
Andrey Andreev973a6542015-01-19 13:25:24 +0200425================== =============== ===========================================================================
426Preference Default Description
427================== =============== ===========================================================================
428**cookie_domain** '' The domain for which the session is applicable
429**cookie_path** / The path to which the session is applicable
430**cookie_secure** FALSE Whether to create the session cookie only on encrypted (HTTPS) connections
431================== =============== ===========================================================================
432
433.. note:: The 'cookie_httponly' setting doesn't have an effect on sessions.
434 Instead the HttpOnly parameter is always enabled, for security
435 reasons. Additionaly, the 'cookie_prefix' setting is completely
436 ignored.
dchill423169f262012-08-11 20:12:05 -0400437
438Session Drivers
439===============
440
Andrey Andreev973a6542015-01-19 13:25:24 +0200441As already mentioned, the Session library comes with 4 drivers, or storage
442engines, that you can use:
dchill423169f262012-08-11 20:12:05 -0400443
Andrey Andreev973a6542015-01-19 13:25:24 +0200444 - files
445 - database
446 - redis
447 - memcached
dchill423169f262012-08-11 20:12:05 -0400448
Andrey Andreev973a6542015-01-19 13:25:24 +0200449By default, the `Files Driver`_ will be used when a session is initialized,
450because it is the most safe choice and is expected to work everywhere
451(virtually every environment has a file system).
dchill423169f262012-08-11 20:12:05 -0400452
Andrey Andreev973a6542015-01-19 13:25:24 +0200453However, any other driver may be selected via the ``$config['sess_driver']``
454line in your **application/config/config.php** file, if you chose to do so.
455Have it in mind though, every driver has different caveats, so be sure to
456get yourself familiar with them (below) before you make that choice.
dchill423169f262012-08-11 20:12:05 -0400457
Andrey Andreev973a6542015-01-19 13:25:24 +0200458In addition, you may also create and use `Custom Drivers`_, if the ones
459provided by default don't satisfy your use case.
dchill423169f262012-08-11 20:12:05 -0400460
Andrey Andreev973a6542015-01-19 13:25:24 +0200461.. note:: In previous CodeIgniter versions, a different, "cookie driver"
462 was the only option and we have received negative feedback on not
463 providing that option. While we do listen to feedback from the
464 community, we want to warn you that it was dropped because it is
465 **unsafe** and we advise you NOT to try to replicate it via a
466 custom driver.
dchill423169f262012-08-11 20:12:05 -0400467
Andrey Andreev973a6542015-01-19 13:25:24 +0200468Files Driver
469------------
dchill423169f262012-08-11 20:12:05 -0400470
Andrey Andreev973a6542015-01-19 13:25:24 +0200471The 'files' driver uses your file system for storing session data.
dchill423169f262012-08-11 20:12:05 -0400472
Andrey Andreev973a6542015-01-19 13:25:24 +0200473It can safely be said that it works exactly like PHP's own default session
474implementation, but in case this is an important detail for you, have it
475mind that it is in fact not the same code and it has some limitations
476(and advantages).
dchill423169f262012-08-11 20:12:05 -0400477
Andrey Andreev973a6542015-01-19 13:25:24 +0200478To be more specific, it doesn't support PHP's `directory level and mode
479formats used in session.save_path
480<http://php.net/manual/en/session.configuration.php#ini.session.save-path>`_,
481and it has most of the options hard-coded for safety. Instead, only
482absolute paths are supported for ``$config['sess_save_path']``.
dchill423169f262012-08-11 20:12:05 -0400483
Andrey Andreev973a6542015-01-19 13:25:24 +0200484Another important thing that you should know, is to make sure that you
485don't use a publicly-readable or shared directory for storing your session
486files. Make sure that *only you* have access to see the contents of your
487chosen *sess_save_path* directory. Otherwise, anybody who can do that, can
488also steal any of the current sessions (also known as "session fixation"
489attack).
dchill423169f262012-08-11 20:12:05 -0400490
Andrey Andreev973a6542015-01-19 13:25:24 +0200491On UNIX-like operating systems, this is usually achieved by setting the
4920600 mode permissions on that directory via the `chmod` command, which
493allows only the directory's owner to perform read and write operations on
494it. But be careful because the system user *running* the script is usually
495not your own, but something like 'www-data' instead, so only setting those
496permissions will probable break your application.
dchill423169f262012-08-11 20:12:05 -0400497
Andrey Andreev973a6542015-01-19 13:25:24 +0200498Instead, you should do something like this, depending on your environment
499::
dchill423169f262012-08-11 20:12:05 -0400500
Andrey Andreev973a6542015-01-19 13:25:24 +0200501 mkdir /<path to your application directory>/sessions/
502 chmod 0600 /<path to your application directory>/sessions/
503 chown www-data /<path to your application directory>/sessions/
dchill423169f262012-08-11 20:12:05 -0400504
Andrey Andreev973a6542015-01-19 13:25:24 +0200505Bonus Tip
506^^^^^^^^^
dchill423169f262012-08-11 20:12:05 -0400507
Andrey Andreev973a6542015-01-19 13:25:24 +0200508Some of you will probably opt to choose another session driver because
509file storage is usually slower. This is only half true.
dchill423169f262012-08-11 20:12:05 -0400510
Andrey Andreev973a6542015-01-19 13:25:24 +0200511A very basic test will probably trick you into believing that an SQL
512database is faster, but in 99% of the cases, this is only true while you
513only have a few current sessions. As the sessions count and server loads
514increase - which is the time when it matters - the file system will
515consistently outperform almost all relational database setups.
dchill423169f262012-08-11 20:12:05 -0400516
Andrey Andreev973a6542015-01-19 13:25:24 +0200517In addition, if performance is your only concern, you may want to look
518into using `tmpfs <http://eddmann.com/posts/storing-php-sessions-file-caches-in-memory-using-tmpfs/>`_,
519(warning: external resource), which can make your sessions blazing fast.
Derek Jones8ede1a22011-10-05 13:34:52 -0500520
Andrey Andreev973a6542015-01-19 13:25:24 +0200521Database Driver
522---------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500523
Andrey Andreev973a6542015-01-19 13:25:24 +0200524The 'database' driver uses a relational database such as MySQL or
525PostgreSQL to store sessions. This is a popular choice among many users,
526because it allows the developer easy access to the session data within
527an application - it is just another table in your database.
Derek Jones8ede1a22011-10-05 13:34:52 -0500528
Andrey Andreev973a6542015-01-19 13:25:24 +0200529However, there are some conditions that must be met:
Derek Jones8ede1a22011-10-05 13:34:52 -0500530
Andrey Andreev973a6542015-01-19 13:25:24 +0200531 - Only your **default** database connection (or the one that you access
532 as ``$this->db`` from your controllers) can be used.
533 - You can NOT use a persistent connection.
534 - You must have the :doc:`Query Builder </database/query_builder>`
535 enabled.
Derek Jones8ede1a22011-10-05 13:34:52 -0500536
Andrey Andreev973a6542015-01-19 13:25:24 +0200537In order to use the 'database' session driver, you must also create this
538table that we already mentioned and then set it as your
539``$config['sess_save_path']`` value.
540For example, if you would like to use 'ci_sessions' as your table name,
541you would do this:
542
543 $config['sess_driver'] = 'database';
544 $config['sess_save_path'] = 'ci_sessions';
545
546.. note:: If you've upgraded from a previous version of CodeIgniter and
547 you don't have 'sess_save_path' configured, then the Session
548 library will look for the old 'sess_table_name' setting and use
549 it instead. Please don't rely on this behavior as it will get
550 removed in the future.
551
552And then of course, create the database table ...
553
554For MySQL::
555
556 CREATE TABLE IF NOT EXISTS `ci_sessions` (
557 `session_id` varchar(40) NOT NULL,
558 `ip_address` varchar(45) NOT NULL,
559 `timestamp` int(10) unsigned DEFAULT 0 NOT NULL,
560 `data` blob DEFAULT '' NOT NULL,
561 PRIMARY KEY (session_id, ip_address),
562 KEY `ci_sessions_timestamp` (`timestamp`)
Derek Jones4b83d912011-10-05 15:42:30 -0500563 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500564
Andrey Andreev973a6542015-01-19 13:25:24 +0200565For PostgreSQL::
Andrey Andreev69e1b4f2014-01-07 17:29:25 +0200566
Andrey Andreev973a6542015-01-19 13:25:24 +0200567 CREATE TABLE "ci_sessions" (
568 "session_id" varchar(40) NOT NULL,
569 "ip_address" varchar(45) NOT NULL,
570 "timestamp" bigint DEFAULT 0 NOT NULL,
571 "data" text DEFAULT '' NOT NULL,
572 PRIMARY KEY ("session_id")
Andrey Andreev69e1b4f2014-01-07 17:29:25 +0200573 );
574
Andrey Andreev973a6542015-01-19 13:25:24 +0200575 CREATE INDEX "ci_sessions_timestamp" ON "ci_sessions" ("timestamp");
Andrey Andreev69e1b4f2014-01-07 17:29:25 +0200576
Andrey Andreev973a6542015-01-19 13:25:24 +0200577However, if you want to turn on the *sess_match_ip* setting, you should
578also do the following, after creating the table::
Derek Jones8ede1a22011-10-05 13:34:52 -0500579
Andrey Andreev973a6542015-01-19 13:25:24 +0200580 // Works both on MySQL and PostgreSQL
581 ALTER TABLE ci_sessions ADD CONSTRAINT ci_sessions_id_ip UNIQUE (session_id, ip_address);
Derek Jones8ede1a22011-10-05 13:34:52 -0500582
Andrey Andreev973a6542015-01-19 13:25:24 +0200583.. important:: Only MySQL and PostgreSQL databases are officially
Andrey Andreev052c02f2015-01-19 13:30:30 +0200584 supported, due to lack of advisory locking mechanisms on other
585 platforms. Using sessions without locks can cause all sorts of
586 problems, especially with heavy usage of AJAX, and we will not
587 support such cases. Use ``session_write_close()`` after you've
588 done processing session data if you're having performance
589 issues.
Derek Jones8ede1a22011-10-05 13:34:52 -0500590
Andrey Andreev973a6542015-01-19 13:25:24 +0200591Redis Driver
592------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500593
Andrey Andreev973a6542015-01-19 13:25:24 +0200594Redis is a storage engine typically used for caching and popular because
595of its high performance, which is also probably your reason to use the
596'redis' session driver.
Derek Jones8ede1a22011-10-05 13:34:52 -0500597
Andrey Andreev973a6542015-01-19 13:25:24 +0200598The downside is that it is not as ubiquitous as relational databases and
599requires the `phpredis <https://github.com/nicolasff/phpredis>`_ PHP
600extension to be installed on your system, and that one doesn't come
601bundled with PHP.
602Chances are, you're only be using the 'redis' driver only if you're already
603both familiar with Redis and using it for other purposes.
Derek Jones8ede1a22011-10-05 13:34:52 -0500604
Andrey Andreev973a6542015-01-19 13:25:24 +0200605Just as with the 'files' and 'database' drivers, you must also configure
606the storage location for your sessions via the
607``$config['sess_save_path']`` setting.
608The format here is a bit different and complicated at the same time. It is
609best explained by the *phpredis* extension's README file, so we'll simply
610link you to it:
Derek Jones8ede1a22011-10-05 13:34:52 -0500611
Andrey Andreev973a6542015-01-19 13:25:24 +0200612 https://github.com/phpredis/phpredis#php-session-handler
613
614.. warning:: CodeIgniter's Session library does NOT use the actual 'redis'
615 ``session.save_handler``. Take note **only** of the path format in
616 the link above.
617
618For the most common case however, a simple ``host:port`` pair should be
619sufficient::
620
621 $config['sess_driver'] = 'redis';
622 $config['sess_save_path'] = 'tcp://localhost:6379';
623
624Memcached Driver
625----------------
626
627The 'memcached' driver is very similar to the 'redis' one in all of its
628properties, except perhaps for availability, because PHP's `Memcached
629<http://php.net/memcached>`_ extension is distributed via PECL and some
630Linux distrubutions make it available as an easy to install package.
631
632Other than that, and without any intentional bias towards Redis, there's
633not much different to be said about Memcached - it is also a popular
634product that is usually used for caching and famed for its speed.
635
636However, it is worth noting that the only guarantee given by Memcached
637is that setting value X to expire after Y seconds will result in it being
638deleted after Y seconds have passed (but not necessarily that it won't
639expire earlier than that time). This happens very rarely, but should be
640considered as it may result in loss of sessions.
641
642The ``$config['sess_save_path']`` format is fairly straightforward here,
643being just a ``host:port`` pair::
644
645 $config['sess_driver'] = 'memcached';
646 $config['sess_save_path'] = 'localhost:11211';
647
648Bonus Tip
649^^^^^^^^^
650
651Multi-server configuration with an optional *weight* parameter as the
652third colon-separated (``:weight``) value is also supported, but we have
653to note that we haven't tested if that is reliable.
654
655If you want to experiment with this feature (on your own risk), simply
656separate the multiple server paths with commas::
657
658 // localhost will be given higher priority (5) here,
659 // compared to 192.0.2.1 with a weight of 1.
660 $config['sess_save_path'] = 'localhost:11211:5,192.0.2.1:11211:1';
Derek Jones8ede1a22011-10-05 13:34:52 -0500661
dchill423169f262012-08-11 20:12:05 -0400662Custom Drivers
663--------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500664
Andrey Andreev973a6542015-01-19 13:25:24 +0200665You may also create your own, custom session drivers. However, have it in
666mind that this is typically not an easy task, as it takes a lot of
667knowledge to do it properly.
Derek Jones8ede1a22011-10-05 13:34:52 -0500668
Andrey Andreev973a6542015-01-19 13:25:24 +0200669You need to know not only how sessions work in general, but also how they
670work specifically in PHP, how the underlying storage mechanism works, how
671to handle concurrency, avoid deadlocks (but NOT through lack of locks) and
672last but not least - how to handle the potential security issues, which
673is far from trivial.
Derek Jones8ede1a22011-10-05 13:34:52 -0500674
Andrey Andreev973a6542015-01-19 13:25:24 +0200675Long story short - if you don't know how to do that already in raw PHP,
676you shouldn't be trying to do it within CodeIgniter either. You've been
677warned.
Andrey Andreevb57b2ad2014-01-03 12:02:08 +0200678
Andrey Andreev973a6542015-01-19 13:25:24 +0200679If you only want to add some extra functionality to your sessions, just
680extend the base Session class, which is a lot more easier. Read the
681:doc:`Creating Libraries <../general/creating_libraries>` article to
682learn how to do that.
683
684Now, to the point - there are three general rules that you must follow
685when creating a session driver for CodeIgniter:
686
687 - Put your driver's file under **application/libraries/Session/drivers/**
688 and follow the naming conventions used by the Session class.
689
690 For example, if you were to create a 'dummy' driver, you would have
691 a ``Session_dummy_driver`` class name, that is declared in
692 *application/libraries/Session/drivers/Session_dummy_driver.php*.
693
694 - Extend the ``CI_Session_driver`` class.
695
696 This is just a basic class with a few internal helper methods. It is
697 also extendable like any other library, if you really need to do that,
698 but we are not going to explain how ... if you're familiar with how
699 class extensions/overrides work in CI, then you already know how to do
700 it. If not, well, you shouldn't be doing it in the first place.
701
702
703 - Implement the `SessionHandlerInterface
704 <http://php.net/sessionhandlerinterface>`_ interface.
705
706 .. note:: You may notice that ``SessionHandlerInterface`` is provided
707 by PHP since version 5.4.0. CodeIgniter will automatically declare
708 the same interface if you're running an older PHP version.
709
710 The link will explain why and how.
711
712So, based on our 'dummy' driver example above, you'd end up with something
713like this::
714
715 // application/libraries/Session/drivers/Session_dummy_driver.php:
716
717 class CI_Session_dummy_driver extends CI_Session_driver implements SessionHandlerInterface
718 {
719
720 public function __construct(&$params)
dchill423169f262012-08-11 20:12:05 -0400721 {
Andrey Andreev973a6542015-01-19 13:25:24 +0200722 // DO NOT forget this
723 parent::__construct($params);
724
725 // Configuration & other initializations
dchill423169f262012-08-11 20:12:05 -0400726 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500727
Andrey Andreev973a6542015-01-19 13:25:24 +0200728 public function open($save_path, $name)
dchill423169f262012-08-11 20:12:05 -0400729 {
Andrey Andreev973a6542015-01-19 13:25:24 +0200730 // Initialize storage mechanism (connection)
dchill423169f262012-08-11 20:12:05 -0400731 }
732
Andrey Andreev973a6542015-01-19 13:25:24 +0200733 public function read($session_id)
dchill423169f262012-08-11 20:12:05 -0400734 {
Andrey Andreev973a6542015-01-19 13:25:24 +0200735 // Read session data (if exists), acquire locks
dchill423169f262012-08-11 20:12:05 -0400736 }
737
Andrey Andreev973a6542015-01-19 13:25:24 +0200738 public function write($session_id, $session_data)
dchill423169f262012-08-11 20:12:05 -0400739 {
Andrey Andreev973a6542015-01-19 13:25:24 +0200740 // Create / update session data (it might not exist!)
dchill423169f262012-08-11 20:12:05 -0400741 }
742
Andrey Andreev973a6542015-01-19 13:25:24 +0200743 public function close()
dchill423169f262012-08-11 20:12:05 -0400744 {
Andrey Andreev973a6542015-01-19 13:25:24 +0200745 // Free locks, close connections / streams / etc.
746 }
747
748 public function destroy($session_id)
749 {
750 // Call close() method & destroy data for current session (order may differ)
751 }
752
753 public function gc($maxlifetime)
754 {
755 // Erase data for expired sessions
dchill423169f262012-08-11 20:12:05 -0400756 }
Andrey Andreevb57b2ad2014-01-03 12:02:08 +0200757
dchill423169f262012-08-11 20:12:05 -0400758 }
759
Andrey Andreev973a6542015-01-19 13:25:24 +0200760If you've done everything properly, you can now set your *sess_driver*
761configuration value to 'dummy' and use your own driver. Congratulations!
Michael1c7438f2013-10-06 15:21:21 +0300762
Michael1c7438f2013-10-06 15:21:21 +0300763***************
764Class Reference
765***************
766
767.. class:: CI_Session
768
Andrey Andreev973a6542015-01-19 13:25:24 +0200769 .. method:: userdata([$key = NULL])
Michaele0a631c2013-10-20 10:40:51 +0300770
Andrey Andreev973a6542015-01-19 13:25:24 +0200771 :param mixed $key: Session item key or NULL
772 :returns: Value of the specified item key, or an array of all userdata
Andrey Andreev28c2c972014-02-08 04:27:48 +0200773 :rtype: mixed
Michaele0a631c2013-10-20 10:40:51 +0300774
Andrey Andreev973a6542015-01-19 13:25:24 +0200775 Gets the value for a specific ``$_SESSION`` item, or an
776 array of all "userdata" items if not key was specified.
777
778 .. note:: This is a legacy method kept only for backwards
779 compatibility with older applications. You should
780 directly access ``$_SESSION`` instead.
Michaele0a631c2013-10-20 10:40:51 +0300781
782 .. method:: all_userdata()
783
Andrey Andreev28c2c972014-02-08 04:27:48 +0200784 :returns: An array of all userdata
785 :rtype: array
Michaele0a631c2013-10-20 10:40:51 +0300786
Andrey Andreev973a6542015-01-19 13:25:24 +0200787 Returns an array containing all "userdata" items.
Michaele0a631c2013-10-20 10:40:51 +0300788
Andrey Andreev973a6542015-01-19 13:25:24 +0200789 .. note:: This method is DEPRECATED. Use ``userdata()``
790 with no parameters instead.
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200791
Andrey Andreev973a6542015-01-19 13:25:24 +0200792 .. method:: &get_usedata()
Michaele0a631c2013-10-20 10:40:51 +0300793
Andrey Andreev973a6542015-01-19 13:25:24 +0200794 :returns: A reference to ``$_SESSION``
795 :rtype: array
Michaele0a631c2013-10-20 10:40:51 +0300796
Andrey Andreev973a6542015-01-19 13:25:24 +0200797 Returns a reference to the ``$_SESSION`` array.
Michael1c7438f2013-10-06 15:21:21 +0300798
Andrey Andreev973a6542015-01-19 13:25:24 +0200799 .. note:: This is a legacy method kept only for backwards
800 compatibility with older applications.
Michael1c7438f2013-10-06 15:21:21 +0300801
Andrey Andreev973a6542015-01-19 13:25:24 +0200802 .. method:: has_userdata($key)
Michael1c7438f2013-10-06 15:21:21 +0300803
Andrey Andreev973a6542015-01-19 13:25:24 +0200804 :param string $key: Session item key
805 :returns: TRUE if the specified key exists, FALSE if not
Andrey Andreev28c2c972014-02-08 04:27:48 +0200806 :rtype: bool
Michael1c7438f2013-10-06 15:21:21 +0300807
Andrey Andreev973a6542015-01-19 13:25:24 +0200808 Checks if an item exists in ``$_SESSION``.
Michael1c7438f2013-10-06 15:21:21 +0300809
Andrey Andreev973a6542015-01-19 13:25:24 +0200810 .. note:: This is a legacy method kept only for backwards
811 compatibility with older applications. It is just
812 an alias for ``isset($_SESSION[$key])`` - please
813 use that instead.
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200814
Andrey Andreev973a6542015-01-19 13:25:24 +0200815 .. method:: set_userdata($data[, $value = NULL])
816
817 :param mixed $data: An array of key/value pairs to set as session data, or the key for a single item
818 :param mixed $value: The value to set for a specific session item, if $data is a key
819 :rtype: void
820
821 Assigns data to the ``$_SESSION`` superglobal.
822
823 .. note:: This is a legacy method kept only for backwards
824 compatibility with older applications.
825
826 .. method:: unset_userdata($key)
827
828 :param mixed $key: Key for the session data item to unset, or an array of multiple keys
829 :rtype: void
830
831 Unsets the specified key(s) from the ``$_SESSION``
832 superglobal.
833
834 .. note:: This is a legacy method kept only for backwards
835 compatibility with older applications. It is just
836 an alias for ``unset($_SESSION[$key])`` - please
837 use that instead.
838
839 .. method:: mark_as_flash($key)
840
841 :param mixed $key: Key to mark as flashdata, or an array of multiple keys
842 :returns: TRUE on success, FALSE on failure
843 :rtype: bool
844
845 Marks a ``$_SESSION`` item key (or multiple ones) as
846 "flashdata".
847
848 .. method:: get_flash_keys()
849
850 :returns: Array containing the keys of all "flashdata" items.
851 :rtype: array
852
853 Gets a list of all ``$_SESSION`` that have been marked as
854 "flashdata".
855
856 .. method:: umark_flash($key)
857
858 :param mixed $key: Key to be un-marked as flashdata, or an array of multiple keys
859 :rtype: void
860
861 Unmarks a ``$_SESSION`` item key (or multiple ones) as
862 "flashdata".
863
864 .. method:: flashdata([$key = NULL])
865
866 :param mixed $key: Flashdata item key or NULL
867 :returns: Value of the specified item key, or an array of all flashdata
Andrey Andreev28c2c972014-02-08 04:27:48 +0200868 :rtype: mixed
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200869
Andrey Andreev973a6542015-01-19 13:25:24 +0200870 Gets the value for a specific ``$_SESSION`` item that has
871 been marked as "flashdata", or an array of all "flashdata"
872 items if no key was specified.
873
874 .. note:: This is a legacy method kept only for backwards
875 compatibility with older applications. You should
876 directly access ``$_SESSION`` instead.
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200877
Andrey Andreev973a6542015-01-19 13:25:24 +0200878 .. method:: keep_flashdata($key)
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200879
Andrey Andreev973a6542015-01-19 13:25:24 +0200880 :param mixed $key: Flashdata key to keep, or an array of multiple keys
881 :returns TRUE on success, FALSE on failure
882 :rtype: bool
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200883
Andrey Andreev973a6542015-01-19 13:25:24 +0200884 Retains the specified session data key(s) as "flashdata"
885 through the next request.
Michael1c7438f2013-10-06 15:21:21 +0300886
Andrey Andreev973a6542015-01-19 13:25:24 +0200887 .. note:: This is a legacy method kept only for backwards
888 compatibility with older applications. It is just
889 an alias for the ``mark_as_flash()`` method.
Michael1c7438f2013-10-06 15:21:21 +0300890
Andrey Andreev973a6542015-01-19 13:25:24 +0200891 .. method:: set_flashdata($data[, $value = NULL])
Michael1c7438f2013-10-06 15:21:21 +0300892
Andrey Andreev973a6542015-01-19 13:25:24 +0200893 :param mixed $data: An array of key/value pairs to set as flashdata, or the key for a single item
894 :param mixed $value: The value to set for a specific session item, if $data is a key
Andrey Andreev28c2c972014-02-08 04:27:48 +0200895 :rtype: void
Michaele0a631c2013-10-20 10:40:51 +0300896
Andrey Andreev973a6542015-01-19 13:25:24 +0200897 Assigns data to the ``$_SESSION`` superglobal and marks it
898 as "flashdata".
Michaele0a631c2013-10-20 10:40:51 +0300899
Andrey Andreev973a6542015-01-19 13:25:24 +0200900 .. note:: This is a legacy method kept only for backwards
901 compatibility with older applications.
Michael1c7438f2013-10-06 15:21:21 +0300902
Andrey Andreev973a6542015-01-19 13:25:24 +0200903 .. method:: mark_as_temp($key[, $ttl = 300])
904
905 :param mixed $key: Key to mark as tempdata, or an array of multiple keys
906 :param int $ttl: Time-to-live value for the tempdata, in seconds
907 :returns: TRUE on success, FALSE on failure
908 :rtype: bool
909
910 Marks a ``$_SESSION`` item key (or multiple ones) as
911 "tempdata".
912
913 .. method:: get_temp_keys()
914
915 :returns: Array containing the keys of all "tempdata" items.
916 :rtype: array
917
918 Gets a list of all ``$_SESSION`` that have been marked as
919 "tempdata".
920
921 .. method:: umark_temp($key)
922
923 :param mixed $key: Key to be un-marked as tempdata, or an array of multiple keys
924 :rtype: void
925
926 Unmarks a ``$_SESSION`` item key (or multiple ones) as
927 "tempdata".
928
929 .. method:: tempdata([$key = NULL])
930
931 :param mixed $key: Tempdata item key or NULL
932 :returns: Value of the specified item key, or an array of all tempdata
Andrey Andreev28c2c972014-02-08 04:27:48 +0200933 :rtype: mixed
Michael1c7438f2013-10-06 15:21:21 +0300934
Andrey Andreev973a6542015-01-19 13:25:24 +0200935 Gets the value for a specific ``$_SESSION`` item that has
936 been marked as "tempdata", or an array of all "tempdata"
937 items if no key was specified.
938
939 .. note:: This is a legacy method kept only for backwards
940 compatibility with older applications. You should
941 directly access ``$_SESSION`` instead.
Andrey Andreev8b9dd222014-01-24 14:41:22 +0200942
Andrey Andreev973a6542015-01-19 13:25:24 +0200943 .. method:: set_tempdata($data[, $value = NULL])
Michael1c7438f2013-10-06 15:21:21 +0300944
Andrey Andreev973a6542015-01-19 13:25:24 +0200945 :param mixed $data: An array of key/value pairs to set as tempdata, or the key for a single item
946 :param mixed $value: The value to set for a specific session item, if $data is a key
947 :param int $ttl: Time-to-live value for the tempdata item(s), in seconds
Andrey Andreev28c2c972014-02-08 04:27:48 +0200948 :rtype: void
Michael1c7438f2013-10-06 15:21:21 +0300949
Andrey Andreev973a6542015-01-19 13:25:24 +0200950 Assigns data to the ``$_SESSION`` superglobal and marks it
951 as "tempdata".
Michael1c7438f2013-10-06 15:21:21 +0300952
Andrey Andreev973a6542015-01-19 13:25:24 +0200953 .. note:: This is a legacy method kept only for backwards
954 compatibility with older applications.
Michael1c7438f2013-10-06 15:21:21 +0300955
Andrey Andreev973a6542015-01-19 13:25:24 +0200956 .. method:: sess_regenerate([$destroy = FALSE])
Michael1c7438f2013-10-06 15:21:21 +0300957
Andrey Andreev973a6542015-01-19 13:25:24 +0200958 :param bool $destroy: Whether to destroy session data
Andrey Andreev28c2c972014-02-08 04:27:48 +0200959 :rtype: void
Michael1c7438f2013-10-06 15:21:21 +0300960
Andrey Andreev973a6542015-01-19 13:25:24 +0200961 Regenerate session ID, optionally destroying the current
962 session's data.
Michael1c7438f2013-10-06 15:21:21 +0300963
Andrey Andreev973a6542015-01-19 13:25:24 +0200964 .. note:: This method is just an alias for PHP's native
965 `session_regenerate_id()
966 <http://php.net/session_regenerate_id>`_ function.
Michael1c7438f2013-10-06 15:21:21 +0300967
Andrey Andreev973a6542015-01-19 13:25:24 +0200968 .. method:: sess_destroy()
969
970 :rtype: void
971
972 Destroys the current session.
973
974 .. note:: This must be the *last* session-related function
975 that you call. All session data will be lost after
976 you do that.
977
978 .. note:: This method is just an alias for PHP's native
979 `session_destroy()
980 <http://php.net/session_destroy>`_ function.
981
982 .. method:: __get($key)
983
984 :param string $key: Session item key
985 :returns: The requested session data item, or NULL if it doesn't exist
986 :rtype: mixed
987
988 A magic method that allows you to use
989 ``$this->session->item`` instead of ``$_SESSION['item']``,
990 if that's what you prefer.
991
992 It will also return the session ID by calling
993 ``session_id()`` if you try to access
994 ``$this->session->session_id``.
995
996 .. method:: __set($key, $value)
997
998 :param string $key: Session item key
999 :param mixed $value: Value to assign to the session item key
1000 :returns: void
1001
1002 A magic method that allows you to assign items to
1003 ``$_SESSION`` by accessing them as ``$this->session``
1004 properties::
1005
1006 $this->session->foo = 'bar';
1007
1008 // Results in:
1009 // $_SESSION['foo'] = 'bar';