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