dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 1 | ############## |
| 2 | Session Driver |
| 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 |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 6 | activity while they browse your site. CodeIgniter offers two default |
| 7 | session drivers: the classic `Cookie Driver`_, and the `Native Driver`_, |
| 8 | which supports usage of the native PHP Session mechanism. In addition, |
| 9 | you may create your own `Custom Drivers`_ to store session data however |
| 10 | you wish, while still taking advantage of the features of the Session class. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 11 | |
Andrey Andreev | cc04209 | 2014-01-03 17:08:27 +0200 | [diff] [blame] | 12 | .. contents:: |
| 13 | :local: |
| 14 | |
| 15 | .. raw:: html |
| 16 | |
| 17 | <div class="custom-index container"></div> |
| 18 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 19 | Initializing a Session |
| 20 | ====================== |
| 21 | |
| 22 | Sessions will typically run globally with each page load, so the session |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 23 | class must either be :doc:`initialized <../general/drivers>` in your |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 24 | :doc:`controller <../general/controllers>` constructors, or it can be |
| 25 | :doc:`auto-loaded <../general/autoloader>` by the system. For the most |
| 26 | part the session class will run unattended in the background, so simply |
| 27 | initializing the class will cause it to read, create, and update |
| 28 | sessions. |
| 29 | |
| 30 | To initialize the Session class manually in your controller constructor, |
Michael | 5d4131b | 2013-09-30 12:22:29 +0300 | [diff] [blame] | 31 | use the ``$this->load->driver`` function:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 32 | |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 33 | $this->load->driver('session'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 34 | |
Michael | 5d4131b | 2013-09-30 12:22:29 +0300 | [diff] [blame] | 35 | Once loaded, the Sessions library object will be available using:: |
| 36 | |
Andrey Andreev | cc04209 | 2014-01-03 17:08:27 +0200 | [diff] [blame] | 37 | $this->session |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 38 | |
| 39 | How do Sessions work? |
| 40 | ===================== |
| 41 | |
| 42 | When a page is loaded, the session class will check to see if valid |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 43 | session data exists in the user's session. If sessions data does **not** |
| 44 | exist (or if it has expired) a new session will be created and saved. |
| 45 | If a session does exist, its information will be updated. With each update, |
| 46 | the session_id will be regenerated. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 47 | |
| 48 | It's important for you to understand that once initialized, the Session |
| 49 | class runs automatically. There is nothing you need to do to cause the |
| 50 | above behavior to happen. You can, as you'll see below, work with |
| 51 | session data or even add your own data to a user's session, but the |
| 52 | process of reading, writing, and updating a session is automatic. |
| 53 | |
| 54 | What is Session Data? |
| 55 | ===================== |
| 56 | |
| 57 | A *session*, as far as CodeIgniter is concerned, is simply an array |
| 58 | containing the following information: |
| 59 | |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 60 | - The user's unique Session ID (this is a statistically random string |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 61 | with very strong entropy, hashed with MD5 for portability, and |
| 62 | regenerated (by default) every five minutes) |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 63 | - The user's IP Address |
| 64 | - The user's User Agent data (the first 120 characters of the browser |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 65 | data string) |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 66 | - The "last activity" time stamp. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 67 | |
| 68 | The above data is stored in a cookie as a serialized array with this |
| 69 | prototype:: |
| 70 | |
Derek Jones | 4b83d91 | 2011-10-05 15:42:30 -0500 | [diff] [blame] | 71 | [array] |
| 72 | ( |
Michael | 5d4131b | 2013-09-30 12:22:29 +0300 | [diff] [blame] | 73 | 'session_id' => random hash, |
| 74 | 'ip_address' => 'string - user IP address', |
| 75 | 'user_agent' => 'string - user agent data', |
| 76 | 'last_activity' => timestamp |
Derek Jones | 4b83d91 | 2011-10-05 15:42:30 -0500 | [diff] [blame] | 77 | ) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 78 | |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 79 | .. note:: Sessions are only updated every five minutes by default to |
| 80 | reduce processor load. If you repeatedly reload a page you'll notice |
| 81 | that the "last activity" time only updates if five minutes or more has |
| 82 | passed since the last time the cookie was written. This time is |
| 83 | configurable by changing the $config['sess_time_to_update'] line in |
| 84 | your system/config/config.php file. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 85 | |
| 86 | Retrieving Session Data |
| 87 | ======================= |
| 88 | |
| 89 | Any piece of information from the session array is available using the |
| 90 | following function:: |
| 91 | |
| 92 | $this->session->userdata('item'); |
| 93 | |
| 94 | Where item is the array index corresponding to the item you wish to |
| 95 | fetch. For example, to fetch the session ID you will do this:: |
| 96 | |
| 97 | $session_id = $this->session->userdata('session_id'); |
| 98 | |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 99 | .. note:: The function returns NULL if the item you are |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 100 | trying to access does not exist. |
| 101 | |
| 102 | Adding Custom Session Data |
| 103 | ========================== |
| 104 | |
| 105 | A useful aspect of the session array is that you can add your own data |
| 106 | to it and it will be stored in the user's cookie. Why would you want to |
| 107 | do this? Here's one example: |
| 108 | |
| 109 | Let's say a particular user logs into your site. Once authenticated, you |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 110 | could add their username and email address to the session, making |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 111 | that data globally available to you without having to run a database |
| 112 | query when you need it. |
| 113 | |
| 114 | To add your data to the session array involves passing an array |
| 115 | containing your new data to this function:: |
| 116 | |
| 117 | $this->session->set_userdata($array); |
| 118 | |
| 119 | Where $array is an associative array containing your new data. Here's an |
| 120 | example:: |
| 121 | |
Derek Jones | 4b83d91 | 2011-10-05 15:42:30 -0500 | [diff] [blame] | 122 | $newdata = array( |
Michael | 5d4131b | 2013-09-30 12:22:29 +0300 | [diff] [blame] | 123 | 'username' => 'johndoe', |
| 124 | 'email' => 'johndoe@some-site.com', |
| 125 | 'logged_in' => TRUE |
| 126 | ); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 127 | |
Derek Jones | 4b83d91 | 2011-10-05 15:42:30 -0500 | [diff] [blame] | 128 | $this->session->set_userdata($newdata); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 129 | |
Michael | 5d4131b | 2013-09-30 12:22:29 +0300 | [diff] [blame] | 130 | If you want to add userdata one value at a time, ``set_userdata()`` also |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 131 | supports this syntax. |
| 132 | |
| 133 | :: |
| 134 | |
| 135 | $this->session->set_userdata('some_name', 'some_value'); |
| 136 | |
Michael | 5d4131b | 2013-09-30 12:22:29 +0300 | [diff] [blame] | 137 | If you want to verify that a userdata value exists, call ``has_userdata()``. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 138 | |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 139 | :: |
| 140 | |
| 141 | $this->session->has_userdata('some_name'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 142 | |
| 143 | Retrieving All Session Data |
| 144 | =========================== |
| 145 | |
| 146 | An array of all userdata can be retrieved as follows:: |
| 147 | |
| 148 | $this->session->all_userdata() |
| 149 | |
| 150 | And returns an associative array like the following:: |
| 151 | |
Derek Jones | 4b83d91 | 2011-10-05 15:42:30 -0500 | [diff] [blame] | 152 | Array |
| 153 | ( |
Michael | 5d4131b | 2013-09-30 12:22:29 +0300 | [diff] [blame] | 154 | [session_id] => 4a5a5dca22728fb0a84364eeb405b601 |
| 155 | [ip_address] => 127.0.0.1 |
| 156 | [user_agent] => Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; |
| 157 | [last_activity] => 1303142623 |
Derek Jones | 4b83d91 | 2011-10-05 15:42:30 -0500 | [diff] [blame] | 158 | ) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 159 | |
| 160 | Removing Session Data |
| 161 | ===================== |
| 162 | |
| 163 | Just as set_userdata() can be used to add information into a session, |
| 164 | unset_userdata() can be used to remove it, by passing the session key. |
| 165 | For example, if you wanted to remove 'some_name' from your session |
| 166 | information:: |
| 167 | |
| 168 | $this->session->unset_userdata('some_name'); |
| 169 | |
| 170 | |
| 171 | This function can also be passed an associative array of items to unset. |
| 172 | |
| 173 | :: |
| 174 | |
Derek Jones | 4b83d91 | 2011-10-05 15:42:30 -0500 | [diff] [blame] | 175 | $array_items = array('username' => '', 'email' => ''); |
| 176 | |
| 177 | $this->session->unset_userdata($array_items); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 178 | |
| 179 | |
| 180 | Flashdata |
| 181 | ========= |
| 182 | |
| 183 | CodeIgniter supports "flashdata", or session data that will only be |
| 184 | available for the next server request, and are then automatically |
| 185 | cleared. These can be very useful, and are typically used for |
| 186 | informational or status messages (for example: "record 2 deleted"). |
| 187 | |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 188 | .. note:: Flash variables are prefaced with "flash\_" so avoid this prefix |
| 189 | in your own session names. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 190 | |
| 191 | To add flashdata:: |
| 192 | |
| 193 | $this->session->set_flashdata('item', 'value'); |
| 194 | |
| 195 | |
Michael | 5d4131b | 2013-09-30 12:22:29 +0300 | [diff] [blame] | 196 | You can also pass an array to ``set_flashdata()``, in the same manner as |
| 197 | ``set_userdata()``. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 198 | |
| 199 | To read a flashdata variable:: |
| 200 | |
| 201 | $this->session->flashdata('item'); |
Johnathan Croom | 4beca5c | 2012-11-23 18:32:46 -0700 | [diff] [blame] | 202 | |
Mike Funk | 7c26fab | 2012-02-24 09:45:02 -0500 | [diff] [blame] | 203 | An array of all flashdata can be retrieved as follows:: |
| 204 | |
| 205 | $this->session->all_flashdata(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 206 | |
| 207 | |
| 208 | If you find that you need to preserve a flashdata variable through an |
Michael | 5d4131b | 2013-09-30 12:22:29 +0300 | [diff] [blame] | 209 | additional request, you can do so using the ``keep_flashdata()`` function. |
Johnathan Croom | 4beca5c | 2012-11-23 18:32:46 -0700 | [diff] [blame] | 210 | 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] | 211 | |
| 212 | :: |
| 213 | |
| 214 | $this->session->keep_flashdata('item'); |
Johnathan Croom | 4beca5c | 2012-11-23 18:32:46 -0700 | [diff] [blame] | 215 | $this->session->keep_flashdata(array('item1', 'item2', 'item3')); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 216 | |
Michael | 5d4131b | 2013-09-30 12:22:29 +0300 | [diff] [blame] | 217 | .. note:: The function will return NULL if the item cannot be found. |
| 218 | |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 219 | Tempdata |
| 220 | ======== |
| 221 | |
| 222 | CodeIgniter also supports "tempdata", or session data with a specific |
| 223 | expiration time. After the value expires, or the session expires or is |
| 224 | deleted, the value is automatically removed. |
| 225 | |
| 226 | To add tempdata:: |
| 227 | |
| 228 | $expire = 300; // Expire in 5 minutes |
| 229 | |
| 230 | $this->session->set_tempdata('item', 'value', $expire); |
| 231 | |
Michael | 5d4131b | 2013-09-30 12:22:29 +0300 | [diff] [blame] | 232 | You can also pass an array to ``set_tempdata()``:: |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 233 | |
| 234 | $tempdata = array('newuser' => TRUE, 'message' => 'Thanks for joining!'); |
| 235 | |
| 236 | $this->session->set_tempdata($tempdata, '', $expire); |
| 237 | |
| 238 | .. note:: If the expiration is omitted or set to 0, the default expiration of |
| 239 | 5 minutes will be used. |
| 240 | |
| 241 | To read a tempdata variable:: |
| 242 | |
| 243 | $this->session->tempdata('item'); |
| 244 | |
| 245 | If you need to remove a tempdata value before it expires, |
Michael | 5d4131b | 2013-09-30 12:22:29 +0300 | [diff] [blame] | 246 | use ``unset_tempdata()``:: |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 247 | |
| 248 | $this->session->unset_tempdata('item'); |
| 249 | |
| 250 | Destroying a Session |
| 251 | ==================== |
| 252 | |
| 253 | To clear the current session:: |
| 254 | |
| 255 | $this->session->sess_destroy(); |
| 256 | |
| 257 | .. note:: This function should be the last one called, and even flash |
| 258 | variables will no longer be available. If you only want some items |
Michael | 5d4131b | 2013-09-30 12:22:29 +0300 | [diff] [blame] | 259 | destroyed and not all, use ``unset_userdata()``. |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 260 | |
| 261 | Session Preferences |
| 262 | =================== |
| 263 | |
| 264 | You'll find the following Session related preferences in your |
| 265 | application/config/config.php file: |
| 266 | |
| 267 | =========================== =============== =========================== ========================================================================== |
| 268 | Preference Default Options Description |
| 269 | =========================== =============== =========================== ========================================================================== |
| 270 | **sess_driver** cookie cookie/native/*custom* The initial session driver to load. |
| 271 | **sess_valid_drivers** cookie, native None Additional valid drivers which may be loaded. |
| 272 | **sess_cookie_name** ci_session None The name you want the session cookie saved as (data for Cookie driver or |
| 273 | session ID for Native driver). |
| 274 | **sess_expiration** 7200 None The number of seconds you would like the session to last. The default |
| 275 | value is 2 hours (7200 seconds). If you would like a non-expiring |
| 276 | session set the value to zero: 0 |
| 277 | **sess_expire_on_close** FALSE TRUE/FALSE (boolean) Whether to cause the session to expire automatically when the browser |
| 278 | window is closed. |
| 279 | **sess_encrypt_cookie** FALSE TRUE/FALSE (boolean) Whether to encrypt the session data (Cookie driver only). |
| 280 | **sess_use_database** FALSE TRUE/FALSE (boolean) Whether to save the session data to a database. You must create the |
| 281 | table before enabling this option (Cookie driver only). |
| 282 | **sess_table_name** ci_sessions Any valid SQL table name The name of the session database table (Cookie driver only). |
| 283 | **sess_time_to_update** 300 Time in seconds This options controls how often the session class will regenerate itself |
| 284 | and create a new session id. |
| 285 | **sess_match_ip** FALSE TRUE/FALSE (boolean) Whether to match the user's IP address when reading the session data. |
| 286 | Note that some ISPs dynamically changes the IP, so if you want a |
| 287 | non-expiring session you will likely set this to FALSE. |
| 288 | **sess_match_useragent** TRUE TRUE/FALSE (boolean) Whether to match the User Agent when reading the session data. |
| 289 | =========================== =============== =========================== ========================================================================== |
| 290 | |
| 291 | In addition to the values above, the cookie and native drivers apply the |
| 292 | following configuration values shared by the :doc:`Input <input>` and |
| 293 | :doc:`Security <security>` classes: |
| 294 | |
| 295 | =========================== =============== ========================================================================== |
| 296 | Preference Default Description |
| 297 | =========================== =============== ========================================================================== |
| 298 | **cookie_prefix** '' Set a cookie name prefix in order to avoid name collisions |
| 299 | **cookie_domain** '' The domain for which the session is applicable |
| 300 | **cookie_path** / The path to which the session is applicable |
| 301 | =========================== =============== ========================================================================== |
| 302 | |
| 303 | Session Drivers |
| 304 | =============== |
| 305 | |
| 306 | By default, the `Cookie Driver`_ is loaded when a session is initialized. |
| 307 | However, any valid driver may be selected with the $config['sess_driver'] |
| 308 | line in your config.php file. |
| 309 | |
| 310 | The session driver library comes with the cookie and native drivers |
| 311 | installed, and `Custom Drivers`_ may also be installed by the user. |
| 312 | |
| 313 | Typically, only one driver will be used at a time, but CodeIgniter does |
| 314 | support loading multiple drivers. If a specific valid driver is called, it |
| 315 | will be automatically loaded. Or, an additional driver may be explicitly |
Michael | 5d4131b | 2013-09-30 12:22:29 +0300 | [diff] [blame] | 316 | loaded by ``calling load_driver()``:: |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 317 | |
| 318 | $this->session->load_driver('native'); |
| 319 | |
| 320 | The Session library keeps track of the most recently selected driver to call |
| 321 | for driver methods. Normally, session class methods are called directly on |
| 322 | the parent class, as illustrated above. However, any methods called through |
| 323 | a specific driver will select that driver before invoking the parent method. |
| 324 | |
| 325 | So, alternation between multiple drivers can be achieved by specifying which |
| 326 | driver to use for each call:: |
| 327 | |
| 328 | $this->session->native->set_userdata('foo', 'bar'); |
| 329 | |
| 330 | $this->session->cookie->userdata('foo'); |
| 331 | |
| 332 | $this->session->native->unset_userdata('foo'); |
| 333 | |
| 334 | Notice in the previous example that the *native* userdata value 'foo' |
| 335 | would be set to 'bar', which would NOT be returned by the call for |
| 336 | the *cookie* userdata 'foo', nor would the *cookie* value be unset by |
| 337 | the call to unset the *native* 'foo' value. The drivers maintain independent |
| 338 | sets of values, regardless of key names. |
| 339 | |
| 340 | A specific driver may also be explicitly selected for use by pursuant |
Michael | 5d4131b | 2013-09-30 12:22:29 +0300 | [diff] [blame] | 341 | methods with the ``select_driver()`` call:: |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 342 | |
| 343 | $this->session->select_driver('native'); |
| 344 | |
| 345 | $this->session->userdata('item'); // Uses the native driver |
| 346 | |
| 347 | Cookie Driver |
| 348 | ------------- |
| 349 | |
| 350 | The Cookie driver stores session information for each user as serialized |
| 351 | (and optionally encrypted) data in a cookie. It can also store the session |
| 352 | data in a database table for added security, as this permits the session ID |
| 353 | in the user's cookie to be matched against the stored session ID. By default |
| 354 | only the cookie is saved. If you choose to use the database option you'll |
| 355 | need to create the session table as indicated below. |
| 356 | |
| 357 | If you have the encryption option enabled, the serialized array will be |
| 358 | encrypted before being stored in the cookie, making the data highly |
| 359 | secure and impervious to being read or altered by someone. More info |
| 360 | regarding encryption can be :doc:`found here <encryption>`, although |
| 361 | the Session class will take care of initializing and encrypting the data |
| 362 | automatically. |
| 363 | |
| 364 | .. note:: Even if you are not using encrypted sessions, you must set |
| 365 | an :doc:`encryption key <./encryption>` in your config file which is used |
| 366 | to aid in preventing session data manipulation. |
| 367 | |
| 368 | .. note:: Cookies can only hold 4KB of data, so be careful not to exceed |
| 369 | the capacity. The encryption process in particular produces a longer |
| 370 | data string than the original so keep careful track of how much data you |
| 371 | are storing. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 372 | |
| 373 | Saving Session Data to a Database |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 374 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 375 | |
| 376 | While the session data array stored in the user's cookie contains a |
| 377 | Session ID, unless you store session data in a database there is no way |
| 378 | to validate it. For some applications that require little or no |
| 379 | security, session ID validation may not be needed, but if your |
| 380 | application requires security, validation is mandatory. Otherwise, an |
| 381 | old session could be restored by a user modifying their cookies. |
| 382 | |
| 383 | When session data is available in a database, every time a valid session |
| 384 | is found in the user's cookie, a database query is performed to match |
| 385 | it. If the session ID does not match, the session is destroyed. Session |
| 386 | IDs can never be updated, they can only be generated when a new session |
| 387 | is created. |
| 388 | |
| 389 | In order to store sessions, you must first create a database table for |
| 390 | this purpose. Here is the basic prototype (for MySQL) required by the |
Derek Jones | 4b83d91 | 2011-10-05 15:42:30 -0500 | [diff] [blame] | 391 | session class:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 392 | |
Derek Jones | 4b83d91 | 2011-10-05 15:42:30 -0500 | [diff] [blame] | 393 | CREATE TABLE IF NOT EXISTS `ci_sessions` ( |
| 394 | session_id varchar(40) DEFAULT '0' NOT NULL, |
Andrey Andreev | 5a25718 | 2012-06-10 06:18:14 +0300 | [diff] [blame] | 395 | ip_address varchar(45) DEFAULT '0' NOT NULL, |
Derek Jones | 4b83d91 | 2011-10-05 15:42:30 -0500 | [diff] [blame] | 396 | user_agent varchar(120) NOT NULL, |
| 397 | last_activity int(10) unsigned DEFAULT 0 NOT NULL, |
| 398 | user_data text NOT NULL, |
Andrey Andreev | e2afc88 | 2012-11-01 01:35:34 +0200 | [diff] [blame] | 399 | PRIMARY KEY (session_id, ip_address, user_agent), |
Derek Jones | 4b83d91 | 2011-10-05 15:42:30 -0500 | [diff] [blame] | 400 | KEY `last_activity_idx` (`last_activity`) |
| 401 | ); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 402 | |
| 403 | .. note:: By default the table is called ci_sessions, but you can name |
| 404 | it anything you want as long as you update the |
| 405 | application/config/config.php file so that it contains the name you have |
| 406 | chosen. Once you have created your database table you can enable the |
| 407 | database option in your config.php file as follows:: |
| 408 | |
| 409 | $config['sess_use_database'] = TRUE; |
| 410 | |
| 411 | Once enabled, the Session class will store session data in the DB. |
| 412 | |
| 413 | Make sure you've specified the table name in your config file as well:: |
| 414 | |
| 415 | $config['sess_table_name'] = 'ci_sessions'; |
| 416 | |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 417 | .. note:: The Cookie driver has built-in garbage collection which clears |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 418 | out expired sessions so you do not need to write your own routine to do |
| 419 | it. |
| 420 | |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 421 | Native Driver |
| 422 | ------------- |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 423 | |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 424 | The Native driver relies on native PHP sessions to store data in the |
| 425 | $_SESSION superglobal array. All stored values continue to be available |
| 426 | through $_SESSION, but flash- and temp- data items carry special prefixes. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 427 | |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 428 | Custom Drivers |
| 429 | -------------- |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 430 | |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 431 | You may also :doc:`create your own <../general/creating_drivers>` custom |
| 432 | session drivers. A session driver basically manages an array of name/value |
| 433 | pairs with some sort of storage mechanism. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 434 | |
Michael | 5d4131b | 2013-09-30 12:22:29 +0300 | [diff] [blame] | 435 | To make a new driver, extend CI_Session_driver. Overload the ``initialize()`` |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 436 | method and read or create session data. Then implement a save handler to |
| 437 | write changed data to storage (sess_save), a destroy handler to remove |
dchill42 | b3816b7 | 2012-08-13 09:47:58 -0400 | [diff] [blame] | 438 | deleted data (sess_destroy), a regenerate handler to make a new session ID |
| 439 | (sess_regenerate), and an access handler to expose the data (get_userdata). |
| 440 | Your initial class might look like:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 441 | |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 442 | class CI_Session_custom extends CI_Session_driver { |
Andrey Andreev | b57b2ad | 2014-01-03 12:02:08 +0200 | [diff] [blame] | 443 | |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 444 | protected function initialize() |
| 445 | { |
| 446 | // Read existing session data or create a new one |
| 447 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 448 | |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 449 | public function sess_save() |
| 450 | { |
| 451 | // Save current data to storage |
| 452 | } |
| 453 | |
| 454 | public function sess_destroy() |
| 455 | { |
| 456 | // Destroy the current session and clean up storage |
| 457 | } |
| 458 | |
| 459 | public function sess_regenerate() |
| 460 | { |
| 461 | // Create new session ID |
| 462 | } |
| 463 | |
| 464 | public function &get_userdata() |
| 465 | { |
| 466 | // Return a reference to your userdata array |
| 467 | } |
Andrey Andreev | b57b2ad | 2014-01-03 12:02:08 +0200 | [diff] [blame] | 468 | |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 469 | } |
| 470 | |
Michael | 5d4131b | 2013-09-30 12:22:29 +0300 | [diff] [blame] | 471 | Notice that ``get_userdata()`` returns a reference so the parent library is |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 472 | accessing the same array the driver object is using. This saves memory |
| 473 | and avoids synchronization issues during usage. |
| 474 | |
| 475 | Put your driver in the libraries/Session/drivers folder anywhere in your |
| 476 | package paths. This includes the application directory, the system directory, |
Michael | 5d4131b | 2013-09-30 12:22:29 +0300 | [diff] [blame] | 477 | or any path you add with ``$CI->load->add_package_path()``. Your driver must be |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 478 | named CI_Session_<name>, and your filename must be Session_<name>.php, |
| 479 | preferably also capitalized, such as:: |
| 480 | |
| 481 | CI_Session_foo in libraries/Session/drivers/Session_foo.php |
| 482 | |
| 483 | Then specify the driver by setting 'sess_driver' in your config.php file or as a |
| 484 | parameter when loading the CI_Session object:: |
| 485 | |
| 486 | $config['sess_driver'] = 'foo'; |
| 487 | |
| 488 | OR:: |
| 489 | |
| 490 | $CI->load->driver('session', array('sess_driver' => 'foo')); |
| 491 | |
| 492 | The driver specified by 'sess_driver' is automatically included as a valid |
| 493 | driver. However, if you want to make a custom driver available as an option |
| 494 | without making it the initially loaded driver, set 'sess_valid_drivers' in |
| 495 | your config.php file to an array including your driver name:: |
| 496 | |
| 497 | $config['sess_valid_drivers'] = array('sess_driver'); |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 498 | |
| 499 | |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 500 | *************** |
| 501 | Class Reference |
| 502 | *************** |
| 503 | |
| 504 | .. class:: CI_Session |
| 505 | |
Michael | e0a631c | 2013-10-20 10:40:51 +0300 | [diff] [blame] | 506 | .. method:: load_driver($driver) |
| 507 | |
| 508 | :param string $driver: Driver name |
Andrey Andreev | 1ee5a99 | 2014-01-06 13:08:47 +0200 | [diff] [blame^] | 509 | :returns: object |
Michael | e0a631c | 2013-10-20 10:40:51 +0300 | [diff] [blame] | 510 | |
| 511 | Loads a session storage driver |
| 512 | |
| 513 | .. method:: select_driver($driver) |
| 514 | |
| 515 | :param string $driver: Driver name |
| 516 | :returns: void |
| 517 | |
| 518 | Selects default session storage driver. |
| 519 | |
| 520 | .. method:: sess_destroy() |
| 521 | |
| 522 | Destroys current session |
Michael | e0a631c | 2013-10-20 10:40:51 +0300 | [diff] [blame] | 523 | |
Andrey Andreev | 1ee5a99 | 2014-01-06 13:08:47 +0200 | [diff] [blame^] | 524 | .. note:: This method should be the last one called, and even flash |
| 525 | variables will no longer be available after it is used. |
| 526 | If you only want some items destroyed and not all, use |
| 527 | ``unset_userdata()``. |
Michael | e0a631c | 2013-10-20 10:40:51 +0300 | [diff] [blame] | 528 | |
Andrey Andreev | 1ee5a99 | 2014-01-06 13:08:47 +0200 | [diff] [blame^] | 529 | .. method:: sess_regenerate([$destroy = FALSE]) |
| 530 | |
| 531 | :param bool $destroy: Whether to destroy session data |
Michael | e0a631c | 2013-10-20 10:40:51 +0300 | [diff] [blame] | 532 | :returns: void |
| 533 | |
Andrey Andreev | 1ee5a99 | 2014-01-06 13:08:47 +0200 | [diff] [blame^] | 534 | Regenerate the current session data. |
Michael | e0a631c | 2013-10-20 10:40:51 +0300 | [diff] [blame] | 535 | |
| 536 | .. method:: userdata($item) |
| 537 | |
Andrey Andreev | 1ee5a99 | 2014-01-06 13:08:47 +0200 | [diff] [blame^] | 538 | :param string $item: Session item name |
Michael | e0a631c | 2013-10-20 10:40:51 +0300 | [diff] [blame] | 539 | :returns: string |
| 540 | |
Andrey Andreev | 1ee5a99 | 2014-01-06 13:08:47 +0200 | [diff] [blame^] | 541 | Returns a string containing the value of the passed item or NULL if the item is not found. |
| 542 | Example:: |
Michael | e0a631c | 2013-10-20 10:40:51 +0300 | [diff] [blame] | 543 | |
| 544 | $this->session->userdata('user'); |
| 545 | //returns example@example.com considering the set_userdata example. |
| 546 | |
| 547 | .. method:: all_userdata() |
| 548 | |
| 549 | :returns: array |
| 550 | |
Andrey Andreev | 1ee5a99 | 2014-01-06 13:08:47 +0200 | [diff] [blame^] | 551 | Retruns an array with all of the session userdata items. |
Michael | e0a631c | 2013-10-20 10:40:51 +0300 | [diff] [blame] | 552 | |
| 553 | .. method:: all_flashdata() |
| 554 | |
| 555 | :returns: array |
| 556 | |
Andrey Andreev | 1ee5a99 | 2014-01-06 13:08:47 +0200 | [diff] [blame^] | 557 | Retruns an array with all of the session flashdata items. |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 558 | |
Andrey Andreev | 1ee5a99 | 2014-01-06 13:08:47 +0200 | [diff] [blame^] | 559 | .. method:: set_userdata($newdata[, $newval = '']) |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 560 | |
| 561 | :param mixed $newdata: Item name or array of items |
| 562 | :param mixed $newval: Item value or empty string (not required if $newdata is array) |
| 563 | :returns: void |
| 564 | |
| 565 | Sets items into session example usages:: |
| 566 | |
| 567 | $this->session->set_userdata('user', 'example@example.com'); |
| 568 | // adds item user with value example@example.com to the session |
| 569 | |
| 570 | $this->session->set_userdata(array('user'=>'example@example.com')); |
| 571 | // does the same as the above example - adds item user with value example@example.com to the session |
| 572 | |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 573 | .. method:: unset_userdata($item) |
| 574 | |
Andrey Andreev | 1ee5a99 | 2014-01-06 13:08:47 +0200 | [diff] [blame^] | 575 | :param mixed $item: Item name or an array containing multiple items |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 576 | :returns: void |
| 577 | |
Andrey Andreev | 1ee5a99 | 2014-01-06 13:08:47 +0200 | [diff] [blame^] | 578 | Unsets previously set items from the session. Example:: |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 579 | |
| 580 | $this->session->unset_userdata('user'); |
| 581 | //unsets 'user' from session data. |
| 582 | |
| 583 | $this->session->unset_userdata(array('user', 'useremail')); |
Michael | e0a631c | 2013-10-20 10:40:51 +0300 | [diff] [blame] | 584 | //unsets both 'user' and 'useremail' from the session data. |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 585 | |
| 586 | .. method:: has_userdata($item) |
| 587 | |
Andrey Andreev | 1ee5a99 | 2014-01-06 13:08:47 +0200 | [diff] [blame^] | 588 | :param string $item: Item name |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 589 | :returns: bool |
| 590 | |
| 591 | Checks if an item exists in the session. |
| 592 | |
Andrey Andreev | 1ee5a99 | 2014-01-06 13:08:47 +0200 | [diff] [blame^] | 593 | .. method:: set_flashdata($newdata[, $newval = '']) |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 594 | |
Andrey Andreev | 1ee5a99 | 2014-01-06 13:08:47 +0200 | [diff] [blame^] | 595 | :param mixed $newdata: Item name or an array of items |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 596 | :param mixed $newval: Item value or empty string (not required if $newdata is array) |
| 597 | :returns: void |
| 598 | |
| 599 | Sets items into session flashdata example usages:: |
| 600 | |
| 601 | $this->session->set_flashdata('message', 'Test message.'); |
| 602 | // adds item 'message' with value 'Test message.' to the session flashdata |
| 603 | |
| 604 | $this->session->set_flashdata(array('message'=>'Test message.')); |
| 605 | // does the same as the above example - adds item 'message' with value 'Test message.' |
| 606 | to the session flashdata |
| 607 | |
Michael | e0a631c | 2013-10-20 10:40:51 +0300 | [diff] [blame] | 608 | .. method:: keep_flashdata($item) |
| 609 | |
Andrey Andreev | 1ee5a99 | 2014-01-06 13:08:47 +0200 | [diff] [blame^] | 610 | :param mixed $item: Item name or an array containing multiple flashdata items |
Michael | e0a631c | 2013-10-20 10:40:51 +0300 | [diff] [blame] | 611 | :returns: void |
| 612 | |
Andrey Andreev | 1ee5a99 | 2014-01-06 13:08:47 +0200 | [diff] [blame^] | 613 | Keeps items into flashdata for one more request. |
Michael | e0a631c | 2013-10-20 10:40:51 +0300 | [diff] [blame] | 614 | |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 615 | .. method:: flashdata($item) |
| 616 | |
Andrey Andreev | 1ee5a99 | 2014-01-06 13:08:47 +0200 | [diff] [blame^] | 617 | :param string $item: Flashdata item name |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 618 | :returns: string |
| 619 | |
Andrey Andreev | 1ee5a99 | 2014-01-06 13:08:47 +0200 | [diff] [blame^] | 620 | Returns a string containing the value of the passed item or NULL if the item is not found. |
| 621 | Example:: |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 622 | |
| 623 | $this->session->flashdata('message'); |
| 624 | //returns 'Test message.' considering the set_flashdata example. |
| 625 | |
Andrey Andreev | 1ee5a99 | 2014-01-06 13:08:47 +0200 | [diff] [blame^] | 626 | .. method:: set_tempdata($newdata[, $newval = ''[, $expire = 0]]) |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 627 | |
Andrey Andreev | 1ee5a99 | 2014-01-06 13:08:47 +0200 | [diff] [blame^] | 628 | :param mixed $newdata: Item name or array containing multiple items |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 629 | :param string $newval: Item value or empty string (not required if $newdata is array) |
Andrey Andreev | 1ee5a99 | 2014-01-06 13:08:47 +0200 | [diff] [blame^] | 630 | :param int $expire: Lifetime in seconds (0 for default) |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 631 | :returns: void |
| 632 | |
| 633 | Sets items into session tempdata example:: |
| 634 | |
| 635 | $this->session->set_tempdata('message', 'Test message.', '60'); |
| 636 | // adds item 'message' with value 'Test message.' to the session tempdata for 60 seconds |
| 637 | |
| 638 | $this->session->set_tempdata(array('message'=>'Test message.')); |
| 639 | // does the same as the above example - adds item 'message' with value 'Test message.' |
| 640 | to the session tempdata for the default value of |
| 641 | |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 642 | .. method:: unset_tempdata($item) |
| 643 | |
Andrey Andreev | 1ee5a99 | 2014-01-06 13:08:47 +0200 | [diff] [blame^] | 644 | :param mixed $item: Item name or an array containing multiple items |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 645 | :returns: void |
| 646 | |
Andrey Andreev | 1ee5a99 | 2014-01-06 13:08:47 +0200 | [diff] [blame^] | 647 | Unsets previously set items from tempdata. Example:: |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 648 | |
| 649 | $this->session->unset_tempdata('user'); |
| 650 | //unsets 'user' from tempdata. |
| 651 | |
| 652 | $this->session->unset_tempdata(array('user', 'useremail')); |
Michael | e0a631c | 2013-10-20 10:40:51 +0300 | [diff] [blame] | 653 | //unsets both 'user' and 'useremail' from the tempdata. |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 654 | |
Michael | e0a631c | 2013-10-20 10:40:51 +0300 | [diff] [blame] | 655 | .. method:: tempdata($item) |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 656 | |
Andrey Andreev | 1ee5a99 | 2014-01-06 13:08:47 +0200 | [diff] [blame^] | 657 | :param string $item: Tempdata item name |
Michael | e0a631c | 2013-10-20 10:40:51 +0300 | [diff] [blame] | 658 | :returns: string |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 659 | |
Andrey Andreev | 1ee5a99 | 2014-01-06 13:08:47 +0200 | [diff] [blame^] | 660 | Returns a string containing the value of the passed item or NULL if the item is not found. |
| 661 | Example:: |
Michael | 1c7438f | 2013-10-06 15:21:21 +0300 | [diff] [blame] | 662 | |
Michael | e0a631c | 2013-10-20 10:40:51 +0300 | [diff] [blame] | 663 | $this->session->tempdata('message'); |
Andrey Andreev | b57b2ad | 2014-01-03 12:02:08 +0200 | [diff] [blame] | 664 | //returns 'Test message.' considering the set_tempdata example. |