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 | |
vlakoff | a2dee7d | 2015-01-20 04:22:29 +0100 | [diff] [blame] | 50 | .. important:: Because the :doc:`Loader Class </libraries/loader>` is instantiated |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 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 | |
Andrey Andreev | eccac6e | 2015-02-04 16:04:31 +0200 | [diff] [blame] | 75 | A note about concurrency |
| 76 | ------------------------ |
| 77 | |
| 78 | Unless you're developing a website with heavy AJAX usage, you can skip this |
| 79 | section. If you are, however, and if you're experiencing performance |
| 80 | issues, then this note is exactly what you're looking for. |
| 81 | |
| 82 | Sessions in previous versions of CodeIgniter didn't implement locking, |
| 83 | which meant that two HTTP requests using the same session could run exactly |
| 84 | at the same time. To use a more appropriate technical term - requests were |
| 85 | non-blocking. |
| 86 | |
| 87 | However, non-blocking requests in the context of sessions also means |
| 88 | unsafe, because modifications to session data (or session ID regeneration) |
| 89 | in one request can interfere with the execution of a second, concurrent |
| 90 | request. This detail was at the root of many issues and the main reason why |
| 91 | CodeIgniter 3.0 has a completely re-written Session library. |
| 92 | |
| 93 | Why are we telling you this? Because it is likely that after trying to |
| 94 | find the reason for your performance issues, you may conclude that locking |
| 95 | is the issue and therefore look into how to remove the locks ... |
| 96 | |
| 97 | DO NOT DO THAT! Removing locks would be **wrong** and it will cause you |
| 98 | more problems! |
| 99 | |
| 100 | Locking is not the issue, it is a solution. Your issue is that you still |
| 101 | have the session open, while you've already processed it and therefore no |
| 102 | longer need it. So, what you need is to close the session for the |
| 103 | current request after you no longer need it. |
| 104 | |
| 105 | Long story short - call ``session_write_close()`` once you no longer need |
| 106 | anything to do with session variables. |
| 107 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 108 | What is Session Data? |
| 109 | ===================== |
| 110 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 111 | Session data is simply an array associated with a particular session ID |
| 112 | (cookie). |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 113 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 114 | If you've used sessions in PHP before, you should be familiar with PHP's |
| 115 | `$_SESSION superglobal <http://php.net/manual/en/reserved.variables.session.php>`_ |
| 116 | (if not, please read the content on that link). |
Derek 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 | CodeIgniter gives access to its session data through the same means, as it |
| 119 | uses the session handlers' mechanism provided by PHP. Using session data is |
| 120 | as simple as manipulating (read, set and unset values) the ``$_SESSION`` |
| 121 | array. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 122 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 123 | In addition, CodeIgniter also provides 2 special types of session data |
| 124 | that are further explained below: flashdata and tempdata. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 125 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 126 | .. note:: In previous versions, regular session data in CodeIgniter was |
| 127 | referred to as 'userdata'. Have this in mind if that term is used |
| 128 | elsewhere in the manual. Most of it is written to explain how |
| 129 | the custom 'userdata' methods work. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 130 | |
| 131 | Retrieving Session Data |
| 132 | ======================= |
| 133 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 134 | Any piece of information from the session array is available through the |
| 135 | ``$_SESSION`` superglobal:: |
| 136 | |
| 137 | $_SESSION['item'] |
| 138 | |
| 139 | Or through the magic getter:: |
| 140 | |
| 141 | $this->session->item |
| 142 | |
| 143 | And for backwards compatibility, through the ``userdata()`` method:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 144 | |
| 145 | $this->session->userdata('item'); |
| 146 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 147 | Where item is the array key corresponding to the item you wish to fetch. |
| 148 | For example, to assign a previously stored 'name' item to the ``$name`` |
| 149 | variable, you will do this:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 150 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 151 | $name = $_SESSION['name']; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 152 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 153 | // or: |
| 154 | |
| 155 | $name = $this->session->name |
| 156 | |
| 157 | // or: |
| 158 | |
| 159 | $name = $this->session->userdata('name'); |
| 160 | |
| 161 | .. note:: The ``userdata()`` method returns NULL if the item you are trying |
| 162 | to access does not exist. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 163 | |
Andrey Andreev | 8b9dd22 | 2014-01-24 14:41:22 +0200 | [diff] [blame] | 164 | 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] | 165 | omit the item key (magic getter only works for properties):: |
| 166 | |
| 167 | $_SESSION |
| 168 | |
| 169 | // or: |
Andrey Andreev | 8b9dd22 | 2014-01-24 14:41:22 +0200 | [diff] [blame] | 170 | |
| 171 | $this->session->userdata(); |
| 172 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 173 | Adding Session Data |
| 174 | =================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 175 | |
| 176 | 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] | 177 | could add their username and e-mail address to the session, making that |
| 178 | data globally available to you without having to run a database query when |
| 179 | you need it. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 180 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 181 | You can simply assign data to the ``$_SESSION`` array, as with any other |
| 182 | variable. Or as a property of ``$this->session``. |
| 183 | |
| 184 | Alternatively, the old method of assigning it as "userdata" is also |
| 185 | available. That however passing an array containing your new data to the |
| 186 | ``set_userdata()`` method:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 187 | |
| 188 | $this->session->set_userdata($array); |
| 189 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 190 | Where ``$array`` is an associative array containing your new data. Here's |
| 191 | an example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 192 | |
Derek Jones | 4b83d91 | 2011-10-05 15:42:30 -0500 | [diff] [blame] | 193 | $newdata = array( |
Michael | 5d4131b | 2013-09-30 12:22:29 +0300 | [diff] [blame] | 194 | 'username' => 'johndoe', |
| 195 | 'email' => 'johndoe@some-site.com', |
| 196 | 'logged_in' => TRUE |
| 197 | ); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 198 | |
Derek Jones | 4b83d91 | 2011-10-05 15:42:30 -0500 | [diff] [blame] | 199 | $this->session->set_userdata($newdata); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 200 | |
Michael | 5d4131b | 2013-09-30 12:22:29 +0300 | [diff] [blame] | 201 | 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] | 202 | supports this syntax:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 203 | |
| 204 | $this->session->set_userdata('some_name', 'some_value'); |
| 205 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 206 | If you want to verify that a session value exists, simply check with |
| 207 | ``isset()``:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 208 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 209 | // returns FALSE if the 'some_name' item doesn't exist or is NULL, |
| 210 | // TRUE otherwise: |
| 211 | isset($_SESSION['some_name']) |
| 212 | |
| 213 | Or you can call ``has_userdata()``:: |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 214 | |
| 215 | $this->session->has_userdata('some_name'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 216 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 217 | Removing Session Data |
| 218 | ===================== |
| 219 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 220 | Just as with any other variable, unsetting a value in ``$_SESSION`` can be |
| 221 | done through ``unset()``:: |
| 222 | |
| 223 | unset($_SESSION['some_name']); |
| 224 | |
| 225 | // or multiple values: |
| 226 | |
| 227 | unset( |
| 228 | $_SESSION['some_name'], |
| 229 | $_SESSION['another_name'] |
| 230 | ); |
| 231 | |
| 232 | Also, just as ``set_userdata()`` can be used to add information to a |
| 233 | session, ``unset_userdata()`` can be used to remove it, by passing the |
| 234 | session key. For example, if you wanted to remove 'some_name' from your |
| 235 | session data array:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 236 | |
| 237 | $this->session->unset_userdata('some_name'); |
| 238 | |
Andrey Andreev | b339df3 | 2015-02-02 11:45:11 +0200 | [diff] [blame] | 239 | This method also accepts an array of item keys to unset:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 240 | |
Andrey Andreev | b339df3 | 2015-02-02 11:45:11 +0200 | [diff] [blame] | 241 | $array_items = array('username', 'email'); |
Derek Jones | 4b83d91 | 2011-10-05 15:42:30 -0500 | [diff] [blame] | 242 | |
| 243 | $this->session->unset_userdata($array_items); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 244 | |
Andrey Andreev | b339df3 | 2015-02-02 11:45:11 +0200 | [diff] [blame] | 245 | .. note:: In previous versions, the ``unset_userdata()`` method used |
| 246 | to accept an associative array of ``key => 'dummy value'`` |
| 247 | pairs. This is no longer supported. |
| 248 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 249 | Flashdata |
| 250 | ========= |
| 251 | |
| 252 | CodeIgniter supports "flashdata", or session data that will only be |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 253 | available for the next request, and is then automatically cleared. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 254 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 255 | This can be very useful, especially for one-time informational, error or |
| 256 | status messages (for example: "Record 2 deleted"). |
| 257 | |
| 258 | It should be noted that flashdata variables are regular session vars, |
| 259 | only marked in a specific way under the '__ci_vars' key (please don't touch |
| 260 | that one, you've been warned). |
| 261 | |
| 262 | To mark an existing item as "flashdata":: |
| 263 | |
| 264 | $this->session->mark_as_flash('item'); |
| 265 | |
| 266 | If you want to mark multiple items as flashdata, simply pass the keys as an |
| 267 | array:: |
| 268 | |
| 269 | $this->session->mark_as_flash(array('item', 'item2')); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 270 | |
| 271 | To add flashdata:: |
| 272 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 273 | $_SESSION['item'] = 'value'; |
| 274 | $this->session->mark_as_flash('item'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 275 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 276 | Or alternatively, using the ``set_flashdata()`` method:: |
| 277 | |
| 278 | $this->session->set_flashdata('item', 'value'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 279 | |
Michael | 5d4131b | 2013-09-30 12:22:29 +0300 | [diff] [blame] | 280 | You can also pass an array to ``set_flashdata()``, in the same manner as |
| 281 | ``set_userdata()``. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 282 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 283 | Reading flashdata variables is the same as reading regular session data |
| 284 | through ``$_SESSION``:: |
| 285 | |
| 286 | $_SESSION['item'] |
| 287 | |
| 288 | .. important:: The ``userdata()`` method will NOT return flashdata items. |
| 289 | |
| 290 | However, if you want to be sure that you're reading "flashdata" (and not |
| 291 | any other kind), you can also use the ``flashdata()`` method:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 292 | |
| 293 | $this->session->flashdata('item'); |
Johnathan Croom | 4beca5c | 2012-11-23 18:32:46 -0700 | [diff] [blame] | 294 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 295 | 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] | 296 | |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 297 | $this->session->flashdata(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 298 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 299 | .. note:: The ``flashdata()`` method returns NULL if the item cannot be |
| 300 | found. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 301 | |
| 302 | 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] | 303 | additional request, you can do so using the ``keep_flashdata()`` method. |
Johnathan Croom | 4beca5c | 2012-11-23 18:32:46 -0700 | [diff] [blame] | 304 | 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] | 305 | |
| 306 | :: |
| 307 | |
| 308 | $this->session->keep_flashdata('item'); |
Johnathan Croom | 4beca5c | 2012-11-23 18:32:46 -0700 | [diff] [blame] | 309 | $this->session->keep_flashdata(array('item1', 'item2', 'item3')); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 310 | |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 311 | Tempdata |
| 312 | ======== |
| 313 | |
| 314 | CodeIgniter also supports "tempdata", or session data with a specific |
| 315 | expiration time. After the value expires, or the session expires or is |
| 316 | deleted, the value is automatically removed. |
| 317 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 318 | Similarly to flashdata, tempdata variables are regular session vars that |
| 319 | are marked in a specific way under the '__ci_vars' key (again, don't touch |
| 320 | that one). |
| 321 | |
| 322 | To mark an existing item as "tempdata", simply pass its key and expiry time |
| 323 | (in seconds!) to the ``mark_as_temp()`` method:: |
| 324 | |
| 325 | // 'item' will be erased after 300 seconds |
| 326 | $this->session->mark_as_temp('item', 300); |
| 327 | |
| 328 | You can mark multiple items as tempdata in two ways, depending on whether |
| 329 | you want them all to have the same expiry time or not:: |
| 330 | |
| 331 | // Both 'item' and 'item2' will expire after 300 seconds |
| 332 | $this->session->mark_as_temp(array('item', 'item2'), 300); |
| 333 | |
| 334 | // 'item' will be erased after 300 seconds, while 'item2' |
| 335 | // will do so after only 240 seconds |
| 336 | $this->session->mark_as_temp(array( |
| 337 | 'item' => 300, |
| 338 | 'item2' => 240 |
| 339 | )); |
| 340 | |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 341 | To add tempdata:: |
| 342 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 343 | $_SESSION['item'] = 'value'; |
| 344 | $this->session->mark_as_temp('item', 300); // Expire in 5 minutes |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 345 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 346 | Or alternatively, using the ``set_tempdata()`` method:: |
| 347 | |
| 348 | $this->session->set_tempdata('item', 'value', 300); |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 349 | |
Michael | 5d4131b | 2013-09-30 12:22:29 +0300 | [diff] [blame] | 350 | You can also pass an array to ``set_tempdata()``:: |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 351 | |
| 352 | $tempdata = array('newuser' => TRUE, 'message' => 'Thanks for joining!'); |
| 353 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 354 | $this->session->set_tempdata($tempdata, NULL, $expire); |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 355 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 356 | .. note:: If the expiration is omitted or set to 0, the default |
| 357 | time-to-live value of 300 seconds (or 5 minutes) will be used. |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 358 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 359 | To read a tempdata variable, again you can just access it through the |
| 360 | ``$_SESSION`` superglobal array:: |
| 361 | |
| 362 | $_SESSION['item'] |
| 363 | |
| 364 | .. important:: The ``userdata()`` method will NOT return tempdata items. |
| 365 | |
Master Yoda | 7762c59 | 2015-03-06 16:08:59 -0800 | [diff] [blame] | 366 | Or if you want to be sure that you're reading "tempdata" (and not any |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 367 | other kind), you can also use the ``tempdata()`` method:: |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 368 | |
| 369 | $this->session->tempdata('item'); |
| 370 | |
Andrey Andreev | 8b9dd22 | 2014-01-24 14:41:22 +0200 | [diff] [blame] | 371 | And of course, if you want to retrieve all existing tempdata:: |
| 372 | |
| 373 | $this->session->tempdata(); |
| 374 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 375 | .. note:: The ``tempdata()`` method returns NULL if the item cannot be |
| 376 | found. |
| 377 | |
| 378 | If you need to remove a tempdata value before it expires, you can directly |
| 379 | unset it from the ``$_SESSION`` array:: |
| 380 | |
| 381 | unset($_SESSION['item']); |
| 382 | |
| 383 | However, this won't remove the marker that makes this specific item to be |
| 384 | tempdata (it will be invalidated on the next HTTP request), so if you |
| 385 | intend to reuse that same key in the same request, you'd want to use |
| 386 | ``unset_tempdata()``:: |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 387 | |
| 388 | $this->session->unset_tempdata('item'); |
| 389 | |
| 390 | Destroying a Session |
| 391 | ==================== |
| 392 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 393 | To clear the current session (for example, during a logout), you may |
| 394 | simply use either PHP's `session_destroy() <http://php.net/session_destroy>`_ |
| 395 | function, or the ``sess_destroy()`` method. Both will work in exactly the |
| 396 | same way:: |
| 397 | |
| 398 | session_destroy(); |
| 399 | |
| 400 | // or |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 401 | |
| 402 | $this->session->sess_destroy(); |
| 403 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 404 | .. note:: This must be the last session-related operation that you do |
| 405 | during the same request. All session data (including flashdata and |
| 406 | tempdata) will be destroyed permanently and functions will be |
| 407 | unusable during the same request after you destroy the session. |
| 408 | |
| 409 | Accessing session metadata |
| 410 | ========================== |
| 411 | |
| 412 | In previous CodeIgniter versions, the session data array included 4 items |
| 413 | by default: 'session_id', 'ip_address', 'user_agent', 'last_activity'. |
| 414 | |
| 415 | This was due to the specifics of how sessions worked, but is now no longer |
| 416 | necessary with our new implementation. However, it may happen that your |
| 417 | application relied on these values, so here are alternative methods of |
Andrey Andreev | d8e25e9 | 2015-01-19 22:03:57 +0200 | [diff] [blame] | 418 | accessing them: |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 419 | |
| 420 | - session_id: ``session_id()`` |
| 421 | - ip_address: ``$_SERVER['REMOTE_ADDR']`` |
| 422 | - user_agent: ``$this->input->user_agent()`` (unused by sessions) |
| 423 | - last_activity: Depends on the storage, no straightforward way. Sorry! |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 424 | |
| 425 | Session Preferences |
| 426 | =================== |
| 427 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 428 | CodeIgniter will usually make everything work out of the box. However, |
| 429 | Sessions are a very sensitive component of any application, so some |
| 430 | careful configuration must be done. Please take your time to consider |
| 431 | all of the options and their effects. |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 432 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 433 | You'll find the following Session related preferences in your |
| 434 | **application/config/config.php** file: |
| 435 | |
Andrey Andreev | 789b1fe | 2015-02-07 19:30:30 +0200 | [diff] [blame] | 436 | ============================ =============== ======================================== ============================================================================================ |
| 437 | Preference Default Options Description |
| 438 | ============================ =============== ======================================== ============================================================================================ |
| 439 | **sess_driver** files files/database/redis/memcached/*custom* The session storage driver to use. |
| 440 | **sess_cookie_name** ci_session [A-Za-z\_-] characters only The name used for the session cookie. |
| 441 | **sess_expiration** 7200 (2 hours) Time in seconds (integer) The number of seconds you would like the session to last. |
| 442 | If you would like a non-expiring session (until browser is closed) set the value to zero: 0 |
| 443 | **sess_save_path** NULL None Specifies the storage location, depends on the driver being used. |
| 444 | **sess_match_ip** FALSE TRUE/FALSE (boolean) Whether to validate the user's IP address when reading the session cookie. |
| 445 | Note that some ISPs dynamically changes the IP, so if you want a non-expiring session you |
| 446 | will likely set this to FALSE. |
| 447 | **sess_time_to_update** 300 Time in seconds (integer) This option controls how often the session class will regenerate itself and create a new |
| 448 | session ID. Setting it to 0 will disable session ID regeneration. |
| 449 | **sess_regenerate_destroy** FALSE TRUE/FALSE (boolean) Whether to destroy session data associated with the old session ID when auto-regenerating |
| 450 | the session ID. When set to FALSE, the data will be later deleted by the garbage collector. |
| 451 | ============================ =============== ======================================== ============================================================================================ |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 452 | |
| 453 | .. note:: As a last resort, the Session library will try to fetch PHP's |
| 454 | session related INI settings, as well as legacy CI settings such as |
| 455 | 'sess_expire_on_close' when any of the above is not configured. |
| 456 | However, you should never rely on this behavior as it can cause |
| 457 | unexpected results or be changed in the future. Please configure |
| 458 | everything properly. |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 459 | |
| 460 | In addition to the values above, the cookie and native drivers apply the |
| 461 | following configuration values shared by the :doc:`Input <input>` and |
| 462 | :doc:`Security <security>` classes: |
| 463 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 464 | ================== =============== =========================================================================== |
| 465 | Preference Default Description |
| 466 | ================== =============== =========================================================================== |
| 467 | **cookie_domain** '' The domain for which the session is applicable |
| 468 | **cookie_path** / The path to which the session is applicable |
| 469 | **cookie_secure** FALSE Whether to create the session cookie only on encrypted (HTTPS) connections |
| 470 | ================== =============== =========================================================================== |
| 471 | |
| 472 | .. note:: The 'cookie_httponly' setting doesn't have an effect on sessions. |
| 473 | Instead the HttpOnly parameter is always enabled, for security |
Andrey Andreev | 71d8f72 | 2017-01-17 12:01:00 +0200 | [diff] [blame] | 474 | reasons. Additionally, the 'cookie_prefix' setting is completely |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 475 | ignored. |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 476 | |
| 477 | Session Drivers |
| 478 | =============== |
| 479 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 480 | As already mentioned, the Session library comes with 4 drivers, or storage |
| 481 | engines, that you can use: |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 482 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 483 | - files |
| 484 | - database |
| 485 | - redis |
| 486 | - memcached |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 487 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 488 | By default, the `Files Driver`_ will be used when a session is initialized, |
| 489 | because it is the most safe choice and is expected to work everywhere |
| 490 | (virtually every environment has a file system). |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 491 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 492 | However, any other driver may be selected via the ``$config['sess_driver']`` |
| 493 | line in your **application/config/config.php** file, if you chose to do so. |
| 494 | Have it in mind though, every driver has different caveats, so be sure to |
| 495 | get yourself familiar with them (below) before you make that choice. |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 496 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 497 | In addition, you may also create and use `Custom Drivers`_, if the ones |
| 498 | provided by default don't satisfy your use case. |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 499 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 500 | .. note:: In previous CodeIgniter versions, a different, "cookie driver" |
| 501 | was the only option and we have received negative feedback on not |
| 502 | providing that option. While we do listen to feedback from the |
| 503 | community, we want to warn you that it was dropped because it is |
| 504 | **unsafe** and we advise you NOT to try to replicate it via a |
| 505 | custom driver. |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 506 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 507 | Files Driver |
| 508 | ------------ |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 509 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 510 | The 'files' driver uses your file system for storing session data. |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 511 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 512 | It can safely be said that it works exactly like PHP's own default session |
| 513 | implementation, but in case this is an important detail for you, have it |
| 514 | mind that it is in fact not the same code and it has some limitations |
| 515 | (and advantages). |
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 | To be more specific, it doesn't support PHP's `directory level and mode |
| 518 | formats used in session.save_path |
| 519 | <http://php.net/manual/en/session.configuration.php#ini.session.save-path>`_, |
| 520 | and it has most of the options hard-coded for safety. Instead, only |
| 521 | absolute paths are supported for ``$config['sess_save_path']``. |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 522 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 523 | Another important thing that you should know, is to make sure that you |
| 524 | don't use a publicly-readable or shared directory for storing your session |
| 525 | files. Make sure that *only you* have access to see the contents of your |
| 526 | chosen *sess_save_path* directory. Otherwise, anybody who can do that, can |
| 527 | also steal any of the current sessions (also known as "session fixation" |
| 528 | attack). |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 529 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 530 | On UNIX-like operating systems, this is usually achieved by setting the |
Andrey Andreev | 6e8a202 | 2015-02-03 10:53:05 +0200 | [diff] [blame] | 531 | 0700 mode permissions on that directory via the `chmod` command, which |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 532 | allows only the directory's owner to perform read and write operations on |
| 533 | it. But be careful because the system user *running* the script is usually |
| 534 | not your own, but something like 'www-data' instead, so only setting those |
| 535 | permissions will probable break your application. |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 536 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 537 | Instead, you should do something like this, depending on your environment |
| 538 | :: |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 539 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 540 | mkdir /<path to your application directory>/sessions/ |
Andrey Andreev | 6e8a202 | 2015-02-03 10:53:05 +0200 | [diff] [blame] | 541 | chmod 0700 /<path to your application directory>/sessions/ |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 542 | chown www-data /<path to your application directory>/sessions/ |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 543 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 544 | Bonus Tip |
| 545 | ^^^^^^^^^ |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 546 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 547 | Some of you will probably opt to choose another session driver because |
| 548 | file storage is usually slower. This is only half true. |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 549 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 550 | A very basic test will probably trick you into believing that an SQL |
| 551 | database is faster, but in 99% of the cases, this is only true while you |
| 552 | only have a few current sessions. As the sessions count and server loads |
| 553 | increase - which is the time when it matters - the file system will |
| 554 | consistently outperform almost all relational database setups. |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 555 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 556 | In addition, if performance is your only concern, you may want to look |
| 557 | into using `tmpfs <http://eddmann.com/posts/storing-php-sessions-file-caches-in-memory-using-tmpfs/>`_, |
| 558 | (warning: external resource), which can make your sessions blazing fast. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 559 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 560 | Database Driver |
| 561 | --------------- |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 562 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 563 | The 'database' driver uses a relational database such as MySQL or |
| 564 | PostgreSQL to store sessions. This is a popular choice among many users, |
| 565 | because it allows the developer easy access to the session data within |
| 566 | an application - it is just another table in your database. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 567 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 568 | However, there are some conditions that must be met: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 569 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 570 | - Only your **default** database connection (or the one that you access |
| 571 | as ``$this->db`` from your controllers) can be used. |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 572 | - You must have the :doc:`Query Builder </database/query_builder>` |
| 573 | enabled. |
Andrey Andreev | 737a566 | 2015-03-21 12:41:38 +0200 | [diff] [blame] | 574 | - You can NOT use a persistent connection. |
| 575 | - You can NOT use a connection with the *cache_on* setting enabled. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 576 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 577 | In order to use the 'database' session driver, you must also create this |
| 578 | table that we already mentioned and then set it as your |
| 579 | ``$config['sess_save_path']`` value. |
| 580 | For example, if you would like to use 'ci_sessions' as your table name, |
Andrey Andreev | eccac6e | 2015-02-04 16:04:31 +0200 | [diff] [blame] | 581 | you would do this:: |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 582 | |
| 583 | $config['sess_driver'] = 'database'; |
| 584 | $config['sess_save_path'] = 'ci_sessions'; |
| 585 | |
| 586 | .. note:: If you've upgraded from a previous version of CodeIgniter and |
| 587 | you don't have 'sess_save_path' configured, then the Session |
| 588 | library will look for the old 'sess_table_name' setting and use |
| 589 | it instead. Please don't rely on this behavior as it will get |
| 590 | removed in the future. |
| 591 | |
| 592 | And then of course, create the database table ... |
| 593 | |
| 594 | For MySQL:: |
| 595 | |
| 596 | CREATE TABLE IF NOT EXISTS `ci_sessions` ( |
Andrey Andreev | 6c6ee1a | 2016-10-22 16:33:06 +0300 | [diff] [blame] | 597 | `id` varchar(128) NOT NULL, |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 598 | `ip_address` varchar(45) NOT NULL, |
| 599 | `timestamp` int(10) unsigned DEFAULT 0 NOT NULL, |
Andrey Andreev | ff7563e | 2015-02-18 21:38:01 +0200 | [diff] [blame] | 600 | `data` blob NOT NULL, |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 601 | KEY `ci_sessions_timestamp` (`timestamp`) |
Derek Jones | 4b83d91 | 2011-10-05 15:42:30 -0500 | [diff] [blame] | 602 | ); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 603 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 604 | For PostgreSQL:: |
Andrey Andreev | 69e1b4f | 2014-01-07 17:29:25 +0200 | [diff] [blame] | 605 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 606 | CREATE TABLE "ci_sessions" ( |
Andrey Andreev | 6c6ee1a | 2016-10-22 16:33:06 +0300 | [diff] [blame] | 607 | "id" varchar(128) NOT NULL, |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 608 | "ip_address" varchar(45) NOT NULL, |
| 609 | "timestamp" bigint DEFAULT 0 NOT NULL, |
Andrey Andreev | 20573bd | 2015-09-01 12:46:06 +0300 | [diff] [blame] | 610 | "data" text DEFAULT '' NOT NULL |
Andrey Andreev | 69e1b4f | 2014-01-07 17:29:25 +0200 | [diff] [blame] | 611 | ); |
| 612 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 613 | CREATE INDEX "ci_sessions_timestamp" ON "ci_sessions" ("timestamp"); |
Andrey Andreev | 69e1b4f | 2014-01-07 17:29:25 +0200 | [diff] [blame] | 614 | |
Andrey Andreev | 20573bd | 2015-09-01 12:46:06 +0300 | [diff] [blame] | 615 | You will also need to add a PRIMARY KEY **depending on your 'sess_match_ip' |
| 616 | setting**. The examples below work both on MySQL and PostgreSQL:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 617 | |
Andrey Andreev | 20573bd | 2015-09-01 12:46:06 +0300 | [diff] [blame] | 618 | // When sess_match_ip = TRUE |
| 619 | ALTER TABLE ci_sessions ADD PRIMARY KEY (id, ip_address); |
| 620 | |
| 621 | // When sess_match_ip = FALSE |
| 622 | ALTER TABLE ci_sessions ADD PRIMARY KEY (id); |
| 623 | |
| 624 | // To drop a previously created primary key (use when changing the setting) |
| 625 | ALTER TABLE ci_sessions DROP PRIMARY KEY; |
| 626 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 627 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 628 | .. important:: Only MySQL and PostgreSQL databases are officially |
Andrey Andreev | 052c02f | 2015-01-19 13:30:30 +0200 | [diff] [blame] | 629 | supported, due to lack of advisory locking mechanisms on other |
| 630 | platforms. Using sessions without locks can cause all sorts of |
| 631 | problems, especially with heavy usage of AJAX, and we will not |
| 632 | support such cases. Use ``session_write_close()`` after you've |
| 633 | done processing session data if you're having performance |
| 634 | issues. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 635 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 636 | Redis Driver |
| 637 | ------------ |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 638 | |
Andrey Andreev | b7cea9c | 2015-02-14 21:16:48 +0200 | [diff] [blame] | 639 | .. note:: Since Redis doesn't have a locking mechanism exposed, locks for |
| 640 | this driver are emulated by a separate value that is kept for up |
Andrey Andreev | e1a5bb3 | 2015-03-04 13:33:39 +0200 | [diff] [blame] | 641 | to 300 seconds. |
Andrey Andreev | b7cea9c | 2015-02-14 21:16:48 +0200 | [diff] [blame] | 642 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 643 | Redis is a storage engine typically used for caching and popular because |
| 644 | of its high performance, which is also probably your reason to use the |
| 645 | 'redis' session driver. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 646 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 647 | The downside is that it is not as ubiquitous as relational databases and |
Master Yoda | bd2a7e4 | 2015-03-25 02:36:31 -0700 | [diff] [blame] | 648 | requires the `phpredis <https://github.com/phpredis/phpredis>`_ PHP |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 649 | extension to be installed on your system, and that one doesn't come |
| 650 | bundled with PHP. |
| 651 | Chances are, you're only be using the 'redis' driver only if you're already |
| 652 | both familiar with Redis and using it for other purposes. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 653 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 654 | Just as with the 'files' and 'database' drivers, you must also configure |
| 655 | the storage location for your sessions via the |
| 656 | ``$config['sess_save_path']`` setting. |
| 657 | The format here is a bit different and complicated at the same time. It is |
| 658 | best explained by the *phpredis* extension's README file, so we'll simply |
| 659 | link you to it: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 660 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 661 | https://github.com/phpredis/phpredis#php-session-handler |
| 662 | |
| 663 | .. warning:: CodeIgniter's Session library does NOT use the actual 'redis' |
| 664 | ``session.save_handler``. Take note **only** of the path format in |
| 665 | the link above. |
| 666 | |
| 667 | For the most common case however, a simple ``host:port`` pair should be |
| 668 | sufficient:: |
| 669 | |
| 670 | $config['sess_driver'] = 'redis'; |
| 671 | $config['sess_save_path'] = 'tcp://localhost:6379'; |
| 672 | |
| 673 | Memcached Driver |
| 674 | ---------------- |
| 675 | |
Andrey Andreev | b7cea9c | 2015-02-14 21:16:48 +0200 | [diff] [blame] | 676 | .. note:: Since Memcache doesn't have a locking mechanism exposed, locks |
| 677 | for this driver are emulated by a separate value that is kept for |
Andrey Andreev | e1a5bb3 | 2015-03-04 13:33:39 +0200 | [diff] [blame] | 678 | up to 300 seconds. |
Andrey Andreev | b7cea9c | 2015-02-14 21:16:48 +0200 | [diff] [blame] | 679 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 680 | The 'memcached' driver is very similar to the 'redis' one in all of its |
| 681 | properties, except perhaps for availability, because PHP's `Memcached |
| 682 | <http://php.net/memcached>`_ extension is distributed via PECL and some |
| 683 | Linux distrubutions make it available as an easy to install package. |
| 684 | |
| 685 | Other than that, and without any intentional bias towards Redis, there's |
| 686 | not much different to be said about Memcached - it is also a popular |
| 687 | product that is usually used for caching and famed for its speed. |
| 688 | |
| 689 | However, it is worth noting that the only guarantee given by Memcached |
| 690 | is that setting value X to expire after Y seconds will result in it being |
| 691 | deleted after Y seconds have passed (but not necessarily that it won't |
| 692 | expire earlier than that time). This happens very rarely, but should be |
| 693 | considered as it may result in loss of sessions. |
| 694 | |
| 695 | The ``$config['sess_save_path']`` format is fairly straightforward here, |
| 696 | being just a ``host:port`` pair:: |
| 697 | |
| 698 | $config['sess_driver'] = 'memcached'; |
| 699 | $config['sess_save_path'] = 'localhost:11211'; |
| 700 | |
| 701 | Bonus Tip |
| 702 | ^^^^^^^^^ |
| 703 | |
| 704 | Multi-server configuration with an optional *weight* parameter as the |
| 705 | third colon-separated (``:weight``) value is also supported, but we have |
| 706 | to note that we haven't tested if that is reliable. |
| 707 | |
| 708 | If you want to experiment with this feature (on your own risk), simply |
| 709 | separate the multiple server paths with commas:: |
| 710 | |
| 711 | // localhost will be given higher priority (5) here, |
| 712 | // compared to 192.0.2.1 with a weight of 1. |
| 713 | $config['sess_save_path'] = 'localhost:11211:5,192.0.2.1:11211:1'; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 714 | |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 715 | Custom Drivers |
| 716 | -------------- |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 717 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 718 | You may also create your own, custom session drivers. However, have it in |
| 719 | mind that this is typically not an easy task, as it takes a lot of |
| 720 | knowledge to do it properly. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 721 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 722 | You need to know not only how sessions work in general, but also how they |
| 723 | work specifically in PHP, how the underlying storage mechanism works, how |
| 724 | to handle concurrency, avoid deadlocks (but NOT through lack of locks) and |
| 725 | last but not least - how to handle the potential security issues, which |
| 726 | is far from trivial. |
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 | Long story short - if you don't know how to do that already in raw PHP, |
| 729 | you shouldn't be trying to do it within CodeIgniter either. You've been |
| 730 | warned. |
Andrey Andreev | b57b2ad | 2014-01-03 12:02:08 +0200 | [diff] [blame] | 731 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 732 | If you only want to add some extra functionality to your sessions, just |
| 733 | extend the base Session class, which is a lot more easier. Read the |
| 734 | :doc:`Creating Libraries <../general/creating_libraries>` article to |
| 735 | learn how to do that. |
| 736 | |
| 737 | Now, to the point - there are three general rules that you must follow |
| 738 | when creating a session driver for CodeIgniter: |
| 739 | |
| 740 | - Put your driver's file under **application/libraries/Session/drivers/** |
| 741 | and follow the naming conventions used by the Session class. |
| 742 | |
| 743 | For example, if you were to create a 'dummy' driver, you would have |
| 744 | a ``Session_dummy_driver`` class name, that is declared in |
| 745 | *application/libraries/Session/drivers/Session_dummy_driver.php*. |
| 746 | |
| 747 | - Extend the ``CI_Session_driver`` class. |
| 748 | |
| 749 | This is just a basic class with a few internal helper methods. It is |
| 750 | also extendable like any other library, if you really need to do that, |
| 751 | but we are not going to explain how ... if you're familiar with how |
| 752 | class extensions/overrides work in CI, then you already know how to do |
| 753 | it. If not, well, you shouldn't be doing it in the first place. |
| 754 | |
| 755 | |
| 756 | - Implement the `SessionHandlerInterface |
| 757 | <http://php.net/sessionhandlerinterface>`_ interface. |
| 758 | |
| 759 | .. note:: You may notice that ``SessionHandlerInterface`` is provided |
Andrey Andreev | 4a56778 | 2017-11-20 09:58:03 +0200 | [diff] [blame] | 760 | by PHP since version 5.4.0. CodeIgniter will automatically declare |
| 761 | the same interface if you're running an older PHP version. |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 762 | |
| 763 | The link will explain why and how. |
| 764 | |
| 765 | So, based on our 'dummy' driver example above, you'd end up with something |
| 766 | like this:: |
| 767 | |
| 768 | // application/libraries/Session/drivers/Session_dummy_driver.php: |
| 769 | |
| 770 | class CI_Session_dummy_driver extends CI_Session_driver implements SessionHandlerInterface |
| 771 | { |
| 772 | |
| 773 | public function __construct(&$params) |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 774 | { |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 775 | // DO NOT forget this |
| 776 | parent::__construct($params); |
| 777 | |
| 778 | // Configuration & other initializations |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 779 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 780 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 781 | public function open($save_path, $name) |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 782 | { |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 783 | // Initialize storage mechanism (connection) |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 784 | } |
| 785 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 786 | public function read($session_id) |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 787 | { |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 788 | // Read session data (if exists), acquire locks |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 789 | } |
| 790 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 791 | public function write($session_id, $session_data) |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 792 | { |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 793 | // Create / update session data (it might not exist!) |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 794 | } |
| 795 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 796 | public function close() |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 797 | { |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 798 | // Free locks, close connections / streams / etc. |
| 799 | } |
| 800 | |
| 801 | public function destroy($session_id) |
| 802 | { |
| 803 | // Call close() method & destroy data for current session (order may differ) |
| 804 | } |
| 805 | |
| 806 | public function gc($maxlifetime) |
| 807 | { |
| 808 | // Erase data for expired sessions |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 809 | } |
Andrey Andreev | b57b2ad | 2014-01-03 12:02:08 +0200 | [diff] [blame] | 810 | |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 811 | } |
| 812 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 813 | If you've done everything properly, you can now set your *sess_driver* |
| 814 | configuration value to 'dummy' and use your own driver. Congratulations! |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 815 | |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 816 | *************** |
| 817 | Class Reference |
| 818 | *************** |
| 819 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 820 | .. php:class:: CI_Session |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 821 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 822 | .. php:method:: userdata([$key = NULL]) |
Michael | e0a631c | 2013-10-20 10:40:51 +0300 | [diff] [blame] | 823 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 824 | :param mixed $key: Session item key or NULL |
| 825 | :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] | 826 | :rtype: mixed |
Michael | e0a631c | 2013-10-20 10:40:51 +0300 | [diff] [blame] | 827 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 828 | Gets the value for a specific ``$_SESSION`` item, or an |
| 829 | array of all "userdata" items if not key was specified. |
| 830 | |
| 831 | .. note:: This is a legacy method kept only for backwards |
| 832 | compatibility with older applications. You should |
| 833 | directly access ``$_SESSION`` instead. |
Michael | e0a631c | 2013-10-20 10:40:51 +0300 | [diff] [blame] | 834 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 835 | .. php:method:: all_userdata() |
Michael | e0a631c | 2013-10-20 10:40:51 +0300 | [diff] [blame] | 836 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 837 | :returns: An array of all userdata |
| 838 | :rtype: array |
Michael | e0a631c | 2013-10-20 10:40:51 +0300 | [diff] [blame] | 839 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 840 | Returns an array containing all "userdata" items. |
Michael | e0a631c | 2013-10-20 10:40:51 +0300 | [diff] [blame] | 841 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 842 | .. note:: This method is DEPRECATED. Use ``userdata()`` |
| 843 | with no parameters instead. |
Andrey Andreev | 8b9dd22 | 2014-01-24 14:41:22 +0200 | [diff] [blame] | 844 | |
Andrey Andreev | f4cb8f9 | 2015-03-19 11:54:47 +0200 | [diff] [blame] | 845 | .. php:method:: &get_userdata() |
Michael | e0a631c | 2013-10-20 10:40:51 +0300 | [diff] [blame] | 846 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 847 | :returns: A reference to ``$_SESSION`` |
| 848 | :rtype: array |
Michael | e0a631c | 2013-10-20 10:40:51 +0300 | [diff] [blame] | 849 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 850 | Returns a reference to the ``$_SESSION`` array. |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 851 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 852 | .. note:: This is a legacy method kept only for backwards |
| 853 | compatibility with older applications. |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 854 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 855 | .. php:method:: has_userdata($key) |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 856 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 857 | :param string $key: Session item key |
| 858 | :returns: TRUE if the specified key exists, FALSE if not |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 859 | :rtype: bool |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 860 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 861 | Checks if an item exists in ``$_SESSION``. |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 862 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 863 | .. note:: This is a legacy method kept only for backwards |
| 864 | compatibility with older applications. It is just |
| 865 | an alias for ``isset($_SESSION[$key])`` - please |
| 866 | use that instead. |
Andrey Andreev | 8b9dd22 | 2014-01-24 14:41:22 +0200 | [diff] [blame] | 867 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 868 | .. php:method:: set_userdata($data[, $value = NULL]) |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 869 | |
| 870 | :param mixed $data: An array of key/value pairs to set as session data, or the key for a single item |
| 871 | :param mixed $value: The value to set for a specific session item, if $data is a key |
| 872 | :rtype: void |
| 873 | |
| 874 | Assigns data to the ``$_SESSION`` superglobal. |
| 875 | |
| 876 | .. note:: This is a legacy method kept only for backwards |
| 877 | compatibility with older applications. |
| 878 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 879 | .. php:method:: unset_userdata($key) |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 880 | |
| 881 | :param mixed $key: Key for the session data item to unset, or an array of multiple keys |
| 882 | :rtype: void |
| 883 | |
| 884 | Unsets the specified key(s) from the ``$_SESSION`` |
| 885 | superglobal. |
| 886 | |
| 887 | .. note:: This is a legacy method kept only for backwards |
| 888 | compatibility with older applications. It is just |
| 889 | an alias for ``unset($_SESSION[$key])`` - please |
| 890 | use that instead. |
| 891 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 892 | .. php:method:: mark_as_flash($key) |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 893 | |
| 894 | :param mixed $key: Key to mark as flashdata, or an array of multiple keys |
| 895 | :returns: TRUE on success, FALSE on failure |
| 896 | :rtype: bool |
| 897 | |
| 898 | Marks a ``$_SESSION`` item key (or multiple ones) as |
| 899 | "flashdata". |
| 900 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 901 | .. php:method:: get_flash_keys() |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 902 | |
| 903 | :returns: Array containing the keys of all "flashdata" items. |
| 904 | :rtype: array |
| 905 | |
| 906 | Gets a list of all ``$_SESSION`` that have been marked as |
| 907 | "flashdata". |
| 908 | |
Andrey Andreev | 71789ce | 2016-07-21 12:45:58 +0300 | [diff] [blame] | 909 | .. php:method:: unmark_flash($key) |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 910 | |
| 911 | :param mixed $key: Key to be un-marked as flashdata, or an array of multiple keys |
| 912 | :rtype: void |
| 913 | |
| 914 | Unmarks a ``$_SESSION`` item key (or multiple ones) as |
| 915 | "flashdata". |
| 916 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 917 | .. php:method:: flashdata([$key = NULL]) |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 918 | |
| 919 | :param mixed $key: Flashdata item key or NULL |
| 920 | :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] | 921 | :rtype: mixed |
Andrey Andreev | 8b9dd22 | 2014-01-24 14:41:22 +0200 | [diff] [blame] | 922 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 923 | Gets the value for a specific ``$_SESSION`` item that has |
| 924 | been marked as "flashdata", or an array of all "flashdata" |
| 925 | items if no key was specified. |
| 926 | |
| 927 | .. note:: This is a legacy method kept only for backwards |
| 928 | compatibility with older applications. You should |
| 929 | directly access ``$_SESSION`` instead. |
Andrey Andreev | 8b9dd22 | 2014-01-24 14:41:22 +0200 | [diff] [blame] | 930 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 931 | .. php:method:: keep_flashdata($key) |
Andrey Andreev | 8b9dd22 | 2014-01-24 14:41:22 +0200 | [diff] [blame] | 932 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 933 | :param mixed $key: Flashdata key to keep, or an array of multiple keys |
vlakoff | a2dee7d | 2015-01-20 04:22:29 +0100 | [diff] [blame] | 934 | :returns: TRUE on success, FALSE on failure |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 935 | :rtype: bool |
Andrey Andreev | 8b9dd22 | 2014-01-24 14:41:22 +0200 | [diff] [blame] | 936 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 937 | Retains the specified session data key(s) as "flashdata" |
| 938 | through the next request. |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 939 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 940 | .. note:: This is a legacy method kept only for backwards |
| 941 | compatibility with older applications. It is just |
| 942 | an alias for the ``mark_as_flash()`` method. |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 943 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 944 | .. php:method:: set_flashdata($data[, $value = NULL]) |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 945 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 946 | :param mixed $data: An array of key/value pairs to set as flashdata, or the key for a single item |
| 947 | :param mixed $value: The value to set for a specific session item, if $data is a key |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 948 | :rtype: void |
Michael | e0a631c | 2013-10-20 10:40:51 +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 "flashdata". |
Michael | e0a631c | 2013-10-20 10:40:51 +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 | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 956 | .. php:method:: mark_as_temp($key[, $ttl = 300]) |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 957 | |
| 958 | :param mixed $key: Key to mark as tempdata, or an array of multiple keys |
| 959 | :param int $ttl: Time-to-live value for the tempdata, in seconds |
| 960 | :returns: TRUE on success, FALSE on failure |
| 961 | :rtype: bool |
| 962 | |
| 963 | Marks a ``$_SESSION`` item key (or multiple ones) as |
| 964 | "tempdata". |
| 965 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 966 | .. php:method:: get_temp_keys() |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 967 | |
| 968 | :returns: Array containing the keys of all "tempdata" items. |
| 969 | :rtype: array |
| 970 | |
| 971 | Gets a list of all ``$_SESSION`` that have been marked as |
| 972 | "tempdata". |
| 973 | |
Andrey Andreev | 71789ce | 2016-07-21 12:45:58 +0300 | [diff] [blame] | 974 | .. php:method:: unmark_temp($key) |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 975 | |
| 976 | :param mixed $key: Key to be un-marked as tempdata, or an array of multiple keys |
| 977 | :rtype: void |
| 978 | |
| 979 | Unmarks a ``$_SESSION`` item key (or multiple ones) as |
| 980 | "tempdata". |
| 981 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 982 | .. php:method:: tempdata([$key = NULL]) |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 983 | |
| 984 | :param mixed $key: Tempdata item key or NULL |
| 985 | :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] | 986 | :rtype: mixed |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 987 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 988 | Gets the value for a specific ``$_SESSION`` item that has |
| 989 | been marked as "tempdata", or an array of all "tempdata" |
| 990 | items if no key was specified. |
| 991 | |
| 992 | .. note:: This is a legacy method kept only for backwards |
| 993 | compatibility with older applications. You should |
| 994 | directly access ``$_SESSION`` instead. |
Andrey Andreev | 8b9dd22 | 2014-01-24 14:41:22 +0200 | [diff] [blame] | 995 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 996 | .. php:method:: set_tempdata($data[, $value = NULL]) |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 997 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 998 | :param mixed $data: An array of key/value pairs to set as tempdata, or the key for a single item |
| 999 | :param mixed $value: The value to set for a specific session item, if $data is a key |
| 1000 | :param int $ttl: Time-to-live value for the tempdata item(s), in seconds |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 1001 | :rtype: void |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 1002 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 1003 | Assigns data to the ``$_SESSION`` superglobal and marks it |
| 1004 | as "tempdata". |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 1005 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 1006 | .. note:: This is a legacy method kept only for backwards |
| 1007 | compatibility with older applications. |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 1008 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1009 | .. php:method:: sess_regenerate([$destroy = FALSE]) |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 1010 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 1011 | :param bool $destroy: Whether to destroy session data |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 1012 | :rtype: void |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 1013 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 1014 | Regenerate session ID, optionally destroying the current |
| 1015 | session's data. |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 1016 | |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 1017 | .. note:: This method is just an alias for PHP's native |
| 1018 | `session_regenerate_id() |
| 1019 | <http://php.net/session_regenerate_id>`_ function. |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 1020 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1021 | .. php:method:: sess_destroy() |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 1022 | |
| 1023 | :rtype: void |
| 1024 | |
| 1025 | Destroys the current session. |
| 1026 | |
| 1027 | .. note:: This must be the *last* session-related function |
| 1028 | that you call. All session data will be lost after |
| 1029 | you do that. |
| 1030 | |
| 1031 | .. note:: This method is just an alias for PHP's native |
| 1032 | `session_destroy() |
| 1033 | <http://php.net/session_destroy>`_ function. |
| 1034 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1035 | .. php:method:: __get($key) |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 1036 | |
| 1037 | :param string $key: Session item key |
| 1038 | :returns: The requested session data item, or NULL if it doesn't exist |
| 1039 | :rtype: mixed |
| 1040 | |
| 1041 | A magic method that allows you to use |
| 1042 | ``$this->session->item`` instead of ``$_SESSION['item']``, |
| 1043 | if that's what you prefer. |
| 1044 | |
| 1045 | It will also return the session ID by calling |
| 1046 | ``session_id()`` if you try to access |
| 1047 | ``$this->session->session_id``. |
| 1048 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1049 | .. php:method:: __set($key, $value) |
Andrey Andreev | 973a654 | 2015-01-19 13:25:24 +0200 | [diff] [blame] | 1050 | |
| 1051 | :param string $key: Session item key |
| 1052 | :param mixed $value: Value to assign to the session item key |
| 1053 | :returns: void |
| 1054 | |
| 1055 | A magic method that allows you to assign items to |
| 1056 | ``$_SESSION`` by accessing them as ``$this->session`` |
| 1057 | properties:: |
| 1058 | |
| 1059 | $this->session->foo = 'bar'; |
| 1060 | |
| 1061 | // Results in: |
Andrey Andreev | f4cb8f9 | 2015-03-19 11:54:47 +0200 | [diff] [blame] | 1062 | // $_SESSION['foo'] = 'bar'; |