Andrey Andreev | 54e71b8 | 2015-01-19 13:57:05 +0200 | [diff] [blame] | 1 | ############### |
| 2 | Session Library |
| 3 | ############### |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 4 | |
| 5 | The Session class permits you maintain a user's "state" and track their |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 6 | activity while they browse your site. |
| 7 | |
| 8 | CodeIgniter comes with a few session storage drivers: |
| 9 | |
| 10 | - files (default; file-system based) |
| 11 | - database |
| 12 | - redis |
| 13 | - memcached |
| 14 | |
| 15 | In addition, you may create your own, custom session drivers based on other |
| 16 | kinds of storage, while still taking advantage of the features of the |
| 17 | Session class. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 18 | |
Andrey Andreev | cc04209 | 2014-01-03 17:08:27 +0200 | [diff] [blame] | 19 | .. contents:: |
| 20 | :local: |
| 21 | |
| 22 | .. raw:: html |
| 23 | |
| 24 | <div class="custom-index container"></div> |
| 25 | |
Andrey Andreev | de1fe7d | 2014-01-20 17:06:16 +0200 | [diff] [blame] | 26 | *********************** |
| 27 | Using the Session Class |
| 28 | *********************** |
| 29 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 30 | Initializing a Session |
| 31 | ====================== |
| 32 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 33 | Sessions will typically run globally with each page load, so the Session |
| 34 | class 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. |
| 37 | For the most part the session class will run unattended in the background, |
| 38 | so simply initializing the class will cause it to read, create, and update |
| 39 | sessions when necessary. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 40 | |
| 41 | To initialize the Session class manually in your controller constructor, |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 42 | use the ``$this->load->library()`` method:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 43 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 44 | $this->load->library('session'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 45 | |
Michael | 5d4131b | 2013-09-30 12:22:29 +0300 | [diff] [blame] | 46 | Once loaded, the Sessions library object will be available using:: |
| 47 | |
Andrey Andreev | cc04209 | 2014-01-03 17:08:27 +0200 | [diff] [blame] | 48 | $this->session |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 49 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 50 | .. 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 55 | How do Sessions work? |
| 56 | ===================== |
| 57 | |
| 58 | When a page is loaded, the session class will check to see if valid |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 59 | session 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 |
| 61 | expired) a new session will be created and saved. |
| 62 | |
| 63 | If a valid session does exist, its information will be updated. With each |
| 64 | update, the session ID may be regenerated if configured to do so. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 65 | |
| 66 | It's important for you to understand that once initialized, the Session |
| 67 | class runs automatically. There is nothing you need to do to cause the |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 68 | above behavior to happen. You can, as you'll see below, work with session |
| 69 | data, but the process of reading, writing, and updating a session is |
| 70 | automatic. |
| 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 74 | |
| 75 | What is Session Data? |
| 76 | ===================== |
| 77 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 78 | Session data is simply an array associated with a particular session ID |
| 79 | (cookie). |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 80 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 81 | If 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 84 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 85 | CodeIgniter gives access to its session data through the same means, as it |
| 86 | uses the session handlers' mechanism provided by PHP. Using session data is |
| 87 | as simple as manipulating (read, set and unset values) the ``$_SESSION`` |
| 88 | array. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 89 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 90 | In addition, CodeIgniter also provides 2 special types of session data |
| 91 | that are further explained below: flashdata and tempdata. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 92 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 93 | .. 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 97 | |
| 98 | Retrieving Session Data |
| 99 | ======================= |
| 100 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 101 | Any piece of information from the session array is available through the |
| 102 | ``$_SESSION`` superglobal:: |
| 103 | |
| 104 | $_SESSION['item'] |
| 105 | |
| 106 | Or through the magic getter:: |
| 107 | |
| 108 | $this->session->item |
| 109 | |
| 110 | And for backwards compatibility, through the ``userdata()`` method:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 111 | |
| 112 | $this->session->userdata('item'); |
| 113 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 114 | Where item is the array key corresponding to the item you wish to fetch. |
| 115 | For example, to assign a previously stored 'name' item to the ``$name`` |
| 116 | variable, you will do this:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 117 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 118 | $name = $_SESSION['name']; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 119 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 120 | // 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 130 | |
Andrey Andreev | 8b9dd22 | 2014-01-24 14:41:22 +0200 | [diff] [blame] | 131 | If you want to retrieve all of the existing userdata, you can simply |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 132 | omit the item key (magic getter only works for properties):: |
| 133 | |
| 134 | $_SESSION |
| 135 | |
| 136 | // or: |
Andrey Andreev | 8b9dd22 | 2014-01-24 14:41:22 +0200 | [diff] [blame] | 137 | |
| 138 | $this->session->userdata(); |
| 139 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 140 | Adding Session Data |
| 141 | =================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 142 | |
| 143 | Let's say a particular user logs into your site. Once authenticated, you |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 144 | could add their username and e-mail address to the session, making that |
| 145 | data globally available to you without having to run a database query when |
| 146 | you need it. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 147 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 148 | You can simply assign data to the ``$_SESSION`` array, as with any other |
| 149 | variable. Or as a property of ``$this->session``. |
| 150 | |
| 151 | Alternatively, the old method of assigning it as "userdata" is also |
| 152 | available. That however passing an array containing your new data to the |
| 153 | ``set_userdata()`` method:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 154 | |
| 155 | $this->session->set_userdata($array); |
| 156 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 157 | Where ``$array`` is an associative array containing your new data. Here's |
| 158 | an example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 159 | |
Derek Jones | 4b83d91 | 2011-10-05 15:42:30 -0500 | [diff] [blame] | 160 | $newdata = array( |
Michael | 5d4131b | 2013-09-30 12:22:29 +0300 | [diff] [blame] | 161 | 'username' => 'johndoe', |
| 162 | 'email' => 'johndoe@some-site.com', |
| 163 | 'logged_in' => TRUE |
| 164 | ); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 165 | |
Derek Jones | 4b83d91 | 2011-10-05 15:42:30 -0500 | [diff] [blame] | 166 | $this->session->set_userdata($newdata); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 167 | |
Michael | 5d4131b | 2013-09-30 12:22:29 +0300 | [diff] [blame] | 168 | If you want to add userdata one value at a time, ``set_userdata()`` also |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 169 | supports this syntax:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 170 | |
| 171 | $this->session->set_userdata('some_name', 'some_value'); |
| 172 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 173 | If you want to verify that a session value exists, simply check with |
| 174 | ``isset()``:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 175 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 176 | // returns FALSE if the 'some_name' item doesn't exist or is NULL, |
| 177 | // TRUE otherwise: |
| 178 | isset($_SESSION['some_name']) |
| 179 | |
| 180 | Or you can call ``has_userdata()``:: |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 181 | |
| 182 | $this->session->has_userdata('some_name'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 183 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 184 | Removing Session Data |
| 185 | ===================== |
| 186 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 187 | Just as with any other variable, unsetting a value in ``$_SESSION`` can be |
| 188 | done 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 | |
| 199 | Also, just as ``set_userdata()`` can be used to add information to a |
| 200 | session, ``unset_userdata()`` can be used to remove it, by passing the |
| 201 | session key. For example, if you wanted to remove 'some_name' from your |
| 202 | session data array:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 203 | |
| 204 | $this->session->unset_userdata('some_name'); |
| 205 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 206 | This method also accepts an associative array of items to unset:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 207 | |
Derek Jones | 4b83d91 | 2011-10-05 15:42:30 -0500 | [diff] [blame] | 208 | $array_items = array('username' => '', 'email' => ''); |
| 209 | |
| 210 | $this->session->unset_userdata($array_items); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 211 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 212 | Flashdata |
| 213 | ========= |
| 214 | |
| 215 | CodeIgniter supports "flashdata", or session data that will only be |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 216 | available for the next request, and is then automatically cleared. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 217 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 218 | This can be very useful, especially for one-time informational, error or |
| 219 | status messages (for example: "Record 2 deleted"). |
| 220 | |
| 221 | It should be noted that flashdata variables are regular session vars, |
| 222 | only marked in a specific way under the '__ci_vars' key (please don't touch |
| 223 | that one, you've been warned). |
| 224 | |
| 225 | To mark an existing item as "flashdata":: |
| 226 | |
| 227 | $this->session->mark_as_flash('item'); |
| 228 | |
| 229 | If you want to mark multiple items as flashdata, simply pass the keys as an |
| 230 | array:: |
| 231 | |
| 232 | $this->session->mark_as_flash(array('item', 'item2')); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 233 | |
| 234 | To add flashdata:: |
| 235 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 236 | $_SESSION['item'] = 'value'; |
| 237 | $this->session->mark_as_flash('item'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 238 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 239 | Or alternatively, using the ``set_flashdata()`` method:: |
| 240 | |
| 241 | $this->session->set_flashdata('item', 'value'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 242 | |
Michael | 5d4131b | 2013-09-30 12:22:29 +0300 | [diff] [blame] | 243 | You can also pass an array to ``set_flashdata()``, in the same manner as |
| 244 | ``set_userdata()``. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 245 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 246 | Reading flashdata variables is the same as reading regular session data |
| 247 | through ``$_SESSION``:: |
| 248 | |
| 249 | $_SESSION['item'] |
| 250 | |
| 251 | .. important:: The ``userdata()`` method will NOT return flashdata items. |
| 252 | |
| 253 | However, if you want to be sure that you're reading "flashdata" (and not |
| 254 | any other kind), you can also use the ``flashdata()`` method:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 255 | |
| 256 | $this->session->flashdata('item'); |
Johnathan Croom | 4beca5c | 2012-11-23 18:32:46 -0700 | [diff] [blame] | 257 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 258 | Or to get an array with all flashdata, simply omit the key parameter:: |
Mike Funk | 7c26fab | 2012-02-24 09:45:02 -0500 | [diff] [blame] | 259 | |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 260 | $this->session->flashdata(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 261 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 262 | .. note:: The ``flashdata()`` method returns NULL if the item cannot be |
| 263 | found. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 264 | |
| 265 | If you find that you need to preserve a flashdata variable through an |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 266 | additional request, you can do so using the ``keep_flashdata()`` method. |
Johnathan Croom | 4beca5c | 2012-11-23 18:32:46 -0700 | [diff] [blame] | 267 | You can either pass a single item or an array of flashdata items to keep. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 268 | |
| 269 | :: |
| 270 | |
| 271 | $this->session->keep_flashdata('item'); |
Johnathan Croom | 4beca5c | 2012-11-23 18:32:46 -0700 | [diff] [blame] | 272 | $this->session->keep_flashdata(array('item1', 'item2', 'item3')); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 273 | |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 274 | Tempdata |
| 275 | ======== |
| 276 | |
| 277 | CodeIgniter also supports "tempdata", or session data with a specific |
| 278 | expiration time. After the value expires, or the session expires or is |
| 279 | deleted, the value is automatically removed. |
| 280 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 281 | Similarly to flashdata, tempdata variables are regular session vars that |
| 282 | are marked in a specific way under the '__ci_vars' key (again, don't touch |
| 283 | that one). |
| 284 | |
| 285 | To 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 | |
| 291 | You can mark multiple items as tempdata in two ways, depending on whether |
| 292 | you 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 | |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 304 | To add tempdata:: |
| 305 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 306 | $_SESSION['item'] = 'value'; |
| 307 | $this->session->mark_as_temp('item', 300); // Expire in 5 minutes |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 308 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 309 | Or alternatively, using the ``set_tempdata()`` method:: |
| 310 | |
| 311 | $this->session->set_tempdata('item', 'value', 300); |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 312 | |
Michael | 5d4131b | 2013-09-30 12:22:29 +0300 | [diff] [blame] | 313 | You can also pass an array to ``set_tempdata()``:: |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 314 | |
| 315 | $tempdata = array('newuser' => TRUE, 'message' => 'Thanks for joining!'); |
| 316 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 317 | $this->session->set_tempdata($tempdata, NULL, $expire); |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 318 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 319 | .. 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. |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 321 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 322 | To 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 | |
| 329 | Or if you want to be sure that you're reading "flashdata" (and not any |
| 330 | other kind), you can also use the ``tempdata()`` method:: |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 331 | |
| 332 | $this->session->tempdata('item'); |
| 333 | |
Andrey Andreev | 8b9dd22 | 2014-01-24 14:41:22 +0200 | [diff] [blame] | 334 | And of course, if you want to retrieve all existing tempdata:: |
| 335 | |
| 336 | $this->session->tempdata(); |
| 337 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 338 | .. note:: The ``tempdata()`` method returns NULL if the item cannot be |
| 339 | found. |
| 340 | |
| 341 | If you need to remove a tempdata value before it expires, you can directly |
| 342 | unset it from the ``$_SESSION`` array:: |
| 343 | |
| 344 | unset($_SESSION['item']); |
| 345 | |
| 346 | However, this won't remove the marker that makes this specific item to be |
| 347 | tempdata (it will be invalidated on the next HTTP request), so if you |
| 348 | intend to reuse that same key in the same request, you'd want to use |
| 349 | ``unset_tempdata()``:: |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 350 | |
| 351 | $this->session->unset_tempdata('item'); |
| 352 | |
| 353 | Destroying a Session |
| 354 | ==================== |
| 355 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 356 | To clear the current session (for example, during a logout), you may |
| 357 | simply use either PHP's `session_destroy() <http://php.net/session_destroy>`_ |
| 358 | function, or the ``sess_destroy()`` method. Both will work in exactly the |
| 359 | same way:: |
| 360 | |
| 361 | session_destroy(); |
| 362 | |
| 363 | // or |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 364 | |
| 365 | $this->session->sess_destroy(); |
| 366 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 367 | .. 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 | |
| 372 | Accessing session metadata |
| 373 | ========================== |
| 374 | |
| 375 | In previous CodeIgniter versions, the session data array included 4 items |
| 376 | by default: 'session_id', 'ip_address', 'user_agent', 'last_activity'. |
| 377 | |
| 378 | This was due to the specifics of how sessions worked, but is now no longer |
| 379 | necessary with our new implementation. However, it may happen that your |
| 380 | application relied on these values, so here are alternative methods of |
| 381 | accessing 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! |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 387 | |
| 388 | Session Preferences |
| 389 | =================== |
| 390 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 391 | CodeIgniter will usually make everything work out of the box. However, |
| 392 | Sessions are a very sensitive component of any application, so some |
| 393 | careful configuration must be done. Please take your time to consider |
| 394 | all of the options and their effects. |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 395 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 396 | You'll find the following Session related preferences in your |
| 397 | **application/config/config.php** file: |
| 398 | |
| 399 | ======================== =============== ======================================== ============================================================================================ |
| 400 | Preference 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 Andreev | 9a0e660 | 2015-01-19 14:54:08 +0200 | [diff] [blame^] | 407 | **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 Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 409 | **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. |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 420 | |
| 421 | In addition to the values above, the cookie and native drivers apply the |
| 422 | following configuration values shared by the :doc:`Input <input>` and |
| 423 | :doc:`Security <security>` classes: |
| 424 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 425 | ================== =============== =========================================================================== |
| 426 | Preference 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. |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 437 | |
| 438 | Session Drivers |
| 439 | =============== |
| 440 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 441 | As already mentioned, the Session library comes with 4 drivers, or storage |
| 442 | engines, that you can use: |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 443 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 444 | - files |
| 445 | - database |
| 446 | - redis |
| 447 | - memcached |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 448 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 449 | By default, the `Files Driver`_ will be used when a session is initialized, |
| 450 | because it is the most safe choice and is expected to work everywhere |
| 451 | (virtually every environment has a file system). |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 452 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 453 | However, any other driver may be selected via the ``$config['sess_driver']`` |
| 454 | line in your **application/config/config.php** file, if you chose to do so. |
| 455 | Have it in mind though, every driver has different caveats, so be sure to |
| 456 | get yourself familiar with them (below) before you make that choice. |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 457 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 458 | In addition, you may also create and use `Custom Drivers`_, if the ones |
| 459 | provided by default don't satisfy your use case. |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 460 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 461 | .. 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. |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 467 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 468 | Files Driver |
| 469 | ------------ |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 470 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 471 | The 'files' driver uses your file system for storing session data. |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 472 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 473 | It can safely be said that it works exactly like PHP's own default session |
| 474 | implementation, but in case this is an important detail for you, have it |
| 475 | mind that it is in fact not the same code and it has some limitations |
| 476 | (and advantages). |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 477 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 478 | To be more specific, it doesn't support PHP's `directory level and mode |
| 479 | formats used in session.save_path |
| 480 | <http://php.net/manual/en/session.configuration.php#ini.session.save-path>`_, |
| 481 | and it has most of the options hard-coded for safety. Instead, only |
| 482 | absolute paths are supported for ``$config['sess_save_path']``. |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 483 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 484 | Another important thing that you should know, is to make sure that you |
| 485 | don't use a publicly-readable or shared directory for storing your session |
| 486 | files. Make sure that *only you* have access to see the contents of your |
| 487 | chosen *sess_save_path* directory. Otherwise, anybody who can do that, can |
| 488 | also steal any of the current sessions (also known as "session fixation" |
| 489 | attack). |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 490 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 491 | On UNIX-like operating systems, this is usually achieved by setting the |
| 492 | 0600 mode permissions on that directory via the `chmod` command, which |
| 493 | allows only the directory's owner to perform read and write operations on |
| 494 | it. But be careful because the system user *running* the script is usually |
| 495 | not your own, but something like 'www-data' instead, so only setting those |
| 496 | permissions will probable break your application. |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 497 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 498 | Instead, you should do something like this, depending on your environment |
| 499 | :: |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 500 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 501 | 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/ |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 504 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 505 | Bonus Tip |
| 506 | ^^^^^^^^^ |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 507 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 508 | Some of you will probably opt to choose another session driver because |
| 509 | file storage is usually slower. This is only half true. |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 510 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 511 | A very basic test will probably trick you into believing that an SQL |
| 512 | database is faster, but in 99% of the cases, this is only true while you |
| 513 | only have a few current sessions. As the sessions count and server loads |
| 514 | increase - which is the time when it matters - the file system will |
| 515 | consistently outperform almost all relational database setups. |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 516 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 517 | In addition, if performance is your only concern, you may want to look |
| 518 | into 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 520 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 521 | Database Driver |
| 522 | --------------- |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 523 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 524 | The 'database' driver uses a relational database such as MySQL or |
| 525 | PostgreSQL to store sessions. This is a popular choice among many users, |
| 526 | because it allows the developer easy access to the session data within |
| 527 | an application - it is just another table in your database. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 528 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 529 | However, there are some conditions that must be met: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 530 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 531 | - 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 536 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 537 | In order to use the 'database' session driver, you must also create this |
| 538 | table that we already mentioned and then set it as your |
| 539 | ``$config['sess_save_path']`` value. |
| 540 | For example, if you would like to use 'ci_sessions' as your table name, |
| 541 | you 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 | |
| 552 | And then of course, create the database table ... |
| 553 | |
| 554 | For 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 Jones | 4b83d91 | 2011-10-05 15:42:30 -0500 | [diff] [blame] | 563 | ); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 564 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 565 | For PostgreSQL:: |
Andrey Andreev | 69e1b4f | 2014-01-07 17:29:25 +0200 | [diff] [blame] | 566 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 567 | 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 Andreev | 69e1b4f | 2014-01-07 17:29:25 +0200 | [diff] [blame] | 573 | ); |
| 574 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 575 | CREATE INDEX "ci_sessions_timestamp" ON "ci_sessions" ("timestamp"); |
Andrey Andreev | 69e1b4f | 2014-01-07 17:29:25 +0200 | [diff] [blame] | 576 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 577 | However, if you want to turn on the *sess_match_ip* setting, you should |
| 578 | also do the following, after creating the table:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 579 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 580 | // Works both on MySQL and PostgreSQL |
| 581 | ALTER TABLE ci_sessions ADD CONSTRAINT ci_sessions_id_ip UNIQUE (session_id, ip_address); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 582 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 583 | .. important:: Only MySQL and PostgreSQL databases are officially |
Andrey Andreev | 052c02f | 2015-01-19 13:30:30 +0200 | [diff] [blame] | 584 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 590 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 591 | Redis Driver |
| 592 | ------------ |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 593 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 594 | Redis is a storage engine typically used for caching and popular because |
| 595 | of its high performance, which is also probably your reason to use the |
| 596 | 'redis' session driver. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 597 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 598 | The downside is that it is not as ubiquitous as relational databases and |
| 599 | requires the `phpredis <https://github.com/nicolasff/phpredis>`_ PHP |
| 600 | extension to be installed on your system, and that one doesn't come |
| 601 | bundled with PHP. |
| 602 | Chances are, you're only be using the 'redis' driver only if you're already |
| 603 | both familiar with Redis and using it for other purposes. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 604 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 605 | Just as with the 'files' and 'database' drivers, you must also configure |
| 606 | the storage location for your sessions via the |
| 607 | ``$config['sess_save_path']`` setting. |
| 608 | The format here is a bit different and complicated at the same time. It is |
| 609 | best explained by the *phpredis* extension's README file, so we'll simply |
| 610 | link you to it: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 611 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 612 | 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 | |
| 618 | For the most common case however, a simple ``host:port`` pair should be |
| 619 | sufficient:: |
| 620 | |
| 621 | $config['sess_driver'] = 'redis'; |
| 622 | $config['sess_save_path'] = 'tcp://localhost:6379'; |
| 623 | |
| 624 | Memcached Driver |
| 625 | ---------------- |
| 626 | |
| 627 | The 'memcached' driver is very similar to the 'redis' one in all of its |
| 628 | properties, except perhaps for availability, because PHP's `Memcached |
| 629 | <http://php.net/memcached>`_ extension is distributed via PECL and some |
| 630 | Linux distrubutions make it available as an easy to install package. |
| 631 | |
| 632 | Other than that, and without any intentional bias towards Redis, there's |
| 633 | not much different to be said about Memcached - it is also a popular |
| 634 | product that is usually used for caching and famed for its speed. |
| 635 | |
| 636 | However, it is worth noting that the only guarantee given by Memcached |
| 637 | is that setting value X to expire after Y seconds will result in it being |
| 638 | deleted after Y seconds have passed (but not necessarily that it won't |
| 639 | expire earlier than that time). This happens very rarely, but should be |
| 640 | considered as it may result in loss of sessions. |
| 641 | |
| 642 | The ``$config['sess_save_path']`` format is fairly straightforward here, |
| 643 | being just a ``host:port`` pair:: |
| 644 | |
| 645 | $config['sess_driver'] = 'memcached'; |
| 646 | $config['sess_save_path'] = 'localhost:11211'; |
| 647 | |
| 648 | Bonus Tip |
| 649 | ^^^^^^^^^ |
| 650 | |
| 651 | Multi-server configuration with an optional *weight* parameter as the |
| 652 | third colon-separated (``:weight``) value is also supported, but we have |
| 653 | to note that we haven't tested if that is reliable. |
| 654 | |
| 655 | If you want to experiment with this feature (on your own risk), simply |
| 656 | separate 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 661 | |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 662 | Custom Drivers |
| 663 | -------------- |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 664 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 665 | You may also create your own, custom session drivers. However, have it in |
| 666 | mind that this is typically not an easy task, as it takes a lot of |
| 667 | knowledge to do it properly. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 668 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 669 | You need to know not only how sessions work in general, but also how they |
| 670 | work specifically in PHP, how the underlying storage mechanism works, how |
| 671 | to handle concurrency, avoid deadlocks (but NOT through lack of locks) and |
| 672 | last but not least - how to handle the potential security issues, which |
| 673 | is far from trivial. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 674 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 675 | Long story short - if you don't know how to do that already in raw PHP, |
| 676 | you shouldn't be trying to do it within CodeIgniter either. You've been |
| 677 | warned. |
Andrey Andreev | b57b2ad | 2014-01-03 12:02:08 +0200 | [diff] [blame] | 678 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 679 | If you only want to add some extra functionality to your sessions, just |
| 680 | extend the base Session class, which is a lot more easier. Read the |
| 681 | :doc:`Creating Libraries <../general/creating_libraries>` article to |
| 682 | learn how to do that. |
| 683 | |
| 684 | Now, to the point - there are three general rules that you must follow |
| 685 | when 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 | |
| 712 | So, based on our 'dummy' driver example above, you'd end up with something |
| 713 | like 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) |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 721 | { |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 722 | // DO NOT forget this |
| 723 | parent::__construct($params); |
| 724 | |
| 725 | // Configuration & other initializations |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 726 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 727 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 728 | public function open($save_path, $name) |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 729 | { |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 730 | // Initialize storage mechanism (connection) |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 731 | } |
| 732 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 733 | public function read($session_id) |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 734 | { |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 735 | // Read session data (if exists), acquire locks |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 736 | } |
| 737 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 738 | public function write($session_id, $session_data) |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 739 | { |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 740 | // Create / update session data (it might not exist!) |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 741 | } |
| 742 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 743 | public function close() |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 744 | { |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 745 | // 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 |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 756 | } |
Andrey Andreev | b57b2ad | 2014-01-03 12:02:08 +0200 | [diff] [blame] | 757 | |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 758 | } |
| 759 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 760 | If you've done everything properly, you can now set your *sess_driver* |
| 761 | configuration value to 'dummy' and use your own driver. Congratulations! |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 762 | |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 763 | *************** |
| 764 | Class Reference |
| 765 | *************** |
| 766 | |
| 767 | .. class:: CI_Session |
| 768 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 769 | .. method:: userdata([$key = NULL]) |
Michael | e0a631c | 2013-10-20 10:40:51 +0300 | [diff] [blame] | 770 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 771 | :param mixed $key: Session item key or NULL |
| 772 | :returns: Value of the specified item key, or an array of all userdata |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 773 | :rtype: mixed |
Michael | e0a631c | 2013-10-20 10:40:51 +0300 | [diff] [blame] | 774 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 775 | 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. |
Michael | e0a631c | 2013-10-20 10:40:51 +0300 | [diff] [blame] | 781 | |
| 782 | .. method:: all_userdata() |
| 783 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 784 | :returns: An array of all userdata |
| 785 | :rtype: array |
Michael | e0a631c | 2013-10-20 10:40:51 +0300 | [diff] [blame] | 786 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 787 | Returns an array containing all "userdata" items. |
Michael | e0a631c | 2013-10-20 10:40:51 +0300 | [diff] [blame] | 788 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 789 | .. note:: This method is DEPRECATED. Use ``userdata()`` |
| 790 | with no parameters instead. |
Andrey Andreev | 8b9dd22 | 2014-01-24 14:41:22 +0200 | [diff] [blame] | 791 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 792 | .. method:: &get_usedata() |
Michael | e0a631c | 2013-10-20 10:40:51 +0300 | [diff] [blame] | 793 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 794 | :returns: A reference to ``$_SESSION`` |
| 795 | :rtype: array |
Michael | e0a631c | 2013-10-20 10:40:51 +0300 | [diff] [blame] | 796 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 797 | Returns a reference to the ``$_SESSION`` array. |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 798 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 799 | .. note:: This is a legacy method kept only for backwards |
| 800 | compatibility with older applications. |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 801 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 802 | .. method:: has_userdata($key) |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 803 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 804 | :param string $key: Session item key |
| 805 | :returns: TRUE if the specified key exists, FALSE if not |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 806 | :rtype: bool |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 807 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 808 | Checks if an item exists in ``$_SESSION``. |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 809 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 810 | .. 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 Andreev | 8b9dd22 | 2014-01-24 14:41:22 +0200 | [diff] [blame] | 814 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 815 | .. 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 Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 868 | :rtype: mixed |
Andrey Andreev | 8b9dd22 | 2014-01-24 14:41:22 +0200 | [diff] [blame] | 869 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 870 | 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 Andreev | 8b9dd22 | 2014-01-24 14:41:22 +0200 | [diff] [blame] | 877 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 878 | .. method:: keep_flashdata($key) |
Andrey Andreev | 8b9dd22 | 2014-01-24 14:41:22 +0200 | [diff] [blame] | 879 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 880 | :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 Andreev | 8b9dd22 | 2014-01-24 14:41:22 +0200 | [diff] [blame] | 883 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 884 | Retains the specified session data key(s) as "flashdata" |
| 885 | through the next request. |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 886 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 887 | .. 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. |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 890 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 891 | .. method:: set_flashdata($data[, $value = NULL]) |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 892 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 893 | :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 Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 895 | :rtype: void |
Michael | e0a631c | 2013-10-20 10:40:51 +0300 | [diff] [blame] | 896 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 897 | Assigns data to the ``$_SESSION`` superglobal and marks it |
| 898 | as "flashdata". |
Michael | e0a631c | 2013-10-20 10:40:51 +0300 | [diff] [blame] | 899 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 900 | .. note:: This is a legacy method kept only for backwards |
| 901 | compatibility with older applications. |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 902 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 903 | .. 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 Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 933 | :rtype: mixed |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 934 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 935 | 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 Andreev | 8b9dd22 | 2014-01-24 14:41:22 +0200 | [diff] [blame] | 942 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 943 | .. method:: set_tempdata($data[, $value = NULL]) |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 944 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 945 | :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 Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 948 | :rtype: void |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 949 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 950 | Assigns data to the ``$_SESSION`` superglobal and marks it |
| 951 | as "tempdata". |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 952 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 953 | .. note:: This is a legacy method kept only for backwards |
| 954 | compatibility with older applications. |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 955 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 956 | .. method:: sess_regenerate([$destroy = FALSE]) |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 957 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 958 | :param bool $destroy: Whether to destroy session data |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 959 | :rtype: void |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 960 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 961 | Regenerate session ID, optionally destroying the current |
| 962 | session's data. |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 963 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 964 | .. note:: This method is just an alias for PHP's native |
| 965 | `session_regenerate_id() |
| 966 | <http://php.net/session_regenerate_id>`_ function. |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 967 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 968 | .. 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'; |