Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ########### |
| 2 | Input Class |
| 3 | ########### |
| 4 | |
| 5 | The Input Class serves two purposes: |
| 6 | |
| 7 | #. It pre-processes global input data for security. |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 8 | #. It provides some helper methods for fetching input data and pre-processing it. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 9 | |
| 10 | .. note:: This class is initialized automatically by the system so there |
| 11 | is no need to do it manually. |
| 12 | |
| 13 | Security Filtering |
| 14 | ================== |
| 15 | |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 16 | The security filtering method is called automatically when a new |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 17 | :doc:`controller <../general/controllers>` is invoked. It does the |
| 18 | following: |
| 19 | |
Phil Sturgeon | 55a6ddb | 2012-05-23 18:37:24 +0100 | [diff] [blame] | 20 | - If $config['allow_get_array'] is FALSE (default is TRUE), destroys |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 21 | the global GET array. |
| 22 | - Destroys all global variables in the event register_globals is |
| 23 | turned on. |
| 24 | - Filters the GET/POST/COOKIE array keys, permitting only alpha-numeric |
| 25 | (and a few other) characters. |
| 26 | - Provides XSS (Cross-site Scripting Hacks) filtering. This can be |
| 27 | enabled globally, or upon request. |
| 28 | - Standardizes newline characters to \\n(In Windows \\r\\n) |
| 29 | |
| 30 | XSS Filtering |
| 31 | ============= |
| 32 | |
| 33 | The Input class has the ability to filter input automatically to prevent |
| 34 | cross-site scripting attacks. If you want the filter to run |
| 35 | automatically every time it encounters POST or COOKIE data you can |
| 36 | enable it by opening your application/config/config.php file and setting |
| 37 | this:: |
| 38 | |
| 39 | $config['global_xss_filtering'] = TRUE; |
| 40 | |
| 41 | Please refer to the :doc:`Security class <security>` documentation for |
| 42 | information on using XSS Filtering in your application. |
| 43 | |
Andrey Andreev | 9cf12d1 | 2012-06-13 10:19:59 +0300 | [diff] [blame] | 44 | Using POST, GET, COOKIE, or SERVER Data |
| 45 | ======================================= |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 46 | |
Andrey Andreev | 9cf12d1 | 2012-06-13 10:19:59 +0300 | [diff] [blame] | 47 | CodeIgniter comes with four helper methods that let you fetch POST, GET, |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 48 | COOKIE or SERVER items. The main advantage of using the provided |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 49 | methods rather than fetching an item directly (``$_POST['something']``) |
Andrey Andreev | 9cf12d1 | 2012-06-13 10:19:59 +0300 | [diff] [blame] | 50 | is that the methods will check to see if the item is set and return |
| 51 | NULL if not. This lets you conveniently use data without |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 52 | having to test whether an item exists first. In other words, normally |
| 53 | you might do something like this:: |
| 54 | |
Phil Sturgeon | 55a6ddb | 2012-05-23 18:37:24 +0100 | [diff] [blame] | 55 | $something = isset($_POST['something']) ? $_POST['something'] : NULL; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 56 | |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 57 | With CodeIgniter's built in methods you can simply do this:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 58 | |
| 59 | $something = $this->input->post('something'); |
| 60 | |
Andrey Andreev | 22c3e73 | 2012-06-13 10:21:36 +0300 | [diff] [blame] | 61 | The four methods are: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 62 | |
| 63 | - $this->input->post() |
Andrey Andreev | 22c3e73 | 2012-06-13 10:21:36 +0300 | [diff] [blame] | 64 | - $this->input->get() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 65 | - $this->input->cookie() |
| 66 | - $this->input->server() |
| 67 | |
| 68 | $this->input->post() |
| 69 | ==================== |
| 70 | |
| 71 | The first parameter will contain the name of the POST item you are |
| 72 | looking for:: |
| 73 | |
| 74 | $this->input->post('some_data'); |
| 75 | |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 76 | The method returns NULL if the item you are attempting to retrieve |
Andrey Andreev | 9cf12d1 | 2012-06-13 10:19:59 +0300 | [diff] [blame] | 77 | does not exist. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 78 | |
| 79 | The second optional parameter lets you run the data through the XSS |
| 80 | filter. It's enabled by setting the second parameter to boolean TRUE; |
| 81 | |
| 82 | :: |
| 83 | |
| 84 | $this->input->post('some_data', TRUE); |
| 85 | |
| 86 | To return an array of all POST items call without any parameters. |
| 87 | |
| 88 | To return all POST items and pass them through the XSS filter set the |
| 89 | first parameter NULL while setting the second parameter to boolean; |
| 90 | |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 91 | The method returns NULL if there are no items in the POST. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 92 | |
| 93 | :: |
| 94 | |
Michiel Vugteveen | be0ca26 | 2012-03-07 19:09:51 +0100 | [diff] [blame] | 95 | $this->input->post(NULL, TRUE); // returns all POST items with XSS filter |
Derek Jones | 8831d11 | 2011-10-05 15:57:02 -0500 | [diff] [blame] | 96 | $this->input->post(); // returns all POST items without XSS filter |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 97 | |
| 98 | $this->input->get() |
| 99 | =================== |
| 100 | |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 101 | This method is identical to the post method, only it fetches get data |
| 102 | :: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 103 | |
| 104 | $this->input->get('some_data', TRUE); |
| 105 | |
| 106 | To return an array of all GET items call without any parameters. |
| 107 | |
| 108 | To return all GET items and pass them through the XSS filter set the |
| 109 | first parameter NULL while setting the second parameter to boolean; |
| 110 | |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 111 | The method returns NULL if there are no items in the GET. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 112 | |
| 113 | :: |
| 114 | |
Michiel Vugteveen | be0ca26 | 2012-03-07 19:09:51 +0100 | [diff] [blame] | 115 | $this->input->get(NULL, TRUE); // returns all GET items with XSS filter |
Derek Jones | 8831d11 | 2011-10-05 15:57:02 -0500 | [diff] [blame] | 116 | $this->input->get(); // returns all GET items without XSS filtering |
Michiel Vugteveen | be0ca26 | 2012-03-07 19:09:51 +0100 | [diff] [blame] | 117 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 118 | |
| 119 | $this->input->get_post() |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 120 | ======================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 121 | |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 122 | This method will search through both the post and get streams for |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 123 | data, looking first in post, and then in get:: |
| 124 | |
| 125 | $this->input->get_post('some_data', TRUE); |
| 126 | |
| 127 | $this->input->cookie() |
| 128 | ====================== |
| 129 | |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 130 | This method is identical to the post method, only it fetches cookie data |
| 131 | :: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 132 | |
Andrey Andreev | 9cf12d1 | 2012-06-13 10:19:59 +0300 | [diff] [blame] | 133 | $this->input->cookie('some_cookie'); |
| 134 | $this->input->cookie('some_cookie, TRUE); // with XSS filter |
| 135 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 136 | |
| 137 | $this->input->server() |
| 138 | ====================== |
| 139 | |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 140 | This method is identical to the above methods, only it fetches server |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 141 | server data:: |
| 142 | |
| 143 | $this->input->server('some_data'); |
| 144 | |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 145 | Using the php://input stream |
| 146 | ============================ |
| 147 | |
| 148 | If you want to utilize the PUT, DELETE, PATCH or other exotic request |
| 149 | methods, they can only be accessed via a special input stream, that |
| 150 | can only be read once. This isn't as easy as just reading from e.g. |
| 151 | the ``$_POST`` array, because it will always exist and you can try |
| 152 | and access multiple variables without caring that you might only have |
| 153 | one shot at all of the POST data. |
| 154 | |
| 155 | CodeIgniter will take care of that for you, and you can access data |
| 156 | from the **php://input** stream at any time, just by calling the |
| 157 | ``input_stream()`` method:: |
| 158 | |
| 159 | $this->input->input_stream('key'); |
| 160 | |
| 161 | Similar to the methods above, if the requested data is not found, it |
| 162 | will return NULL and you can also decide whether to run the data |
| 163 | through ``xss_clean()`` by passing a boolean value as the second |
| 164 | parameter:: |
| 165 | |
| 166 | $this->input->input_stream('key', TRUE); // XSS Clean |
| 167 | $this->input->input_stream('key', FALSE); // No XSS filter |
| 168 | |
| 169 | .. note:: You can utilize method() in order to know if you're reading |
| 170 | PUT, DELETE or PATCH data. |
| 171 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 172 | $this->input->set_cookie() |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 173 | ========================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 174 | |
| 175 | Sets a cookie containing the values you specify. There are two ways to |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 176 | pass information to this method so that a cookie can be set: Array |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 177 | Method, and Discrete Parameters: |
| 178 | |
| 179 | Array Method |
| 180 | ^^^^^^^^^^^^ |
| 181 | |
| 182 | Using this method, an associative array is passed to the first |
| 183 | parameter:: |
| 184 | |
Derek Jones | 8831d11 | 2011-10-05 15:57:02 -0500 | [diff] [blame] | 185 | $cookie = array( |
| 186 | 'name' => 'The Cookie Name', |
| 187 | 'value' => 'The Value', |
| 188 | 'expire' => '86500', |
| 189 | 'domain' => '.some-domain.com', |
| 190 | 'path' => '/', |
| 191 | 'prefix' => 'myprefix_', |
| 192 | 'secure' => TRUE |
| 193 | ); |
| 194 | |
| 195 | $this->input->set_cookie($cookie); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 196 | |
| 197 | **Notes:** |
| 198 | |
| 199 | Only the name and value are required. To delete a cookie set it with the |
| 200 | expiration blank. |
| 201 | |
| 202 | The expiration is set in **seconds**, which will be added to the current |
| 203 | time. Do not include the time, but rather only the number of seconds |
| 204 | from *now* that you wish the cookie to be valid. If the expiration is |
| 205 | set to zero the cookie will only last as long as the browser is open. |
| 206 | |
| 207 | For site-wide cookies regardless of how your site is requested, add your |
| 208 | URL to the **domain** starting with a period, like this: |
| 209 | .your-domain.com |
| 210 | |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 211 | The path is usually not needed since the method sets a root path. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 212 | |
| 213 | The prefix is only needed if you need to avoid name collisions with |
| 214 | other identically named cookies for your server. |
| 215 | |
| 216 | The secure boolean is only needed if you want to make it a secure cookie |
| 217 | by setting it to TRUE. |
| 218 | |
| 219 | Discrete Parameters |
| 220 | ^^^^^^^^^^^^^^^^^^^ |
| 221 | |
| 222 | If you prefer, you can set the cookie by passing data using individual |
| 223 | parameters:: |
| 224 | |
| 225 | $this->input->set_cookie($name, $value, $expire, $domain, $path, $prefix, $secure); |
| 226 | |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 227 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 228 | $this->input->ip_address() |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 229 | ========================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 230 | |
| 231 | Returns the IP address for the current user. If the IP address is not |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 232 | valid, the method will return an IP of: 0.0.0.0 |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 233 | |
| 234 | :: |
| 235 | |
| 236 | echo $this->input->ip_address(); |
| 237 | |
| 238 | $this->input->valid_ip($ip) |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 239 | =========================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 240 | |
| 241 | Takes an IP address as input and returns TRUE or FALSE (boolean) if it |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 242 | is valid or not. |
| 243 | |
| 244 | .. note:: The $this->input->ip_address() method above automatically |
| 245 | validates the IP address. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 246 | |
| 247 | :: |
| 248 | |
Derek Jones | 8831d11 | 2011-10-05 15:57:02 -0500 | [diff] [blame] | 249 | if ( ! $this->input->valid_ip($ip)) |
| 250 | { |
| 251 | echo 'Not Valid'; |
| 252 | } |
| 253 | else |
| 254 | { |
| 255 | echo 'Valid'; |
| 256 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 257 | |
Andrey Andreev | 5a25718 | 2012-06-10 06:18:14 +0300 | [diff] [blame] | 258 | Accepts an optional second string parameter of 'ipv4' or 'ipv6' to specify |
| 259 | an IP format. The default checks for both formats. |
| 260 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 261 | $this->input->user_agent() |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 262 | ========================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 263 | |
| 264 | Returns the user agent (web browser) being used by the current user. |
| 265 | Returns FALSE if it's not available. |
| 266 | |
| 267 | :: |
| 268 | |
| 269 | echo $this->input->user_agent(); |
| 270 | |
| 271 | See the :doc:`User Agent Class <user_agent>` for methods which extract |
| 272 | information from the user agent string. |
| 273 | |
| 274 | $this->input->request_headers() |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 275 | =============================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 276 | |
| 277 | Useful if running in a non-Apache environment where |
| 278 | `apache_request_headers() <http://php.net/apache_request_headers>`_ |
| 279 | will not be supported. Returns an array of headers. |
| 280 | |
| 281 | :: |
| 282 | |
| 283 | $headers = $this->input->request_headers(); |
| 284 | |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 285 | $this->input->get_request_header() |
| 286 | ================================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 287 | |
| 288 | Returns a single member of the request headers array. |
| 289 | |
| 290 | :: |
| 291 | |
| 292 | $this->input->get_request_header('some-header', TRUE); |
| 293 | |
| 294 | $this->input->is_ajax_request() |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 295 | =============================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 296 | |
| 297 | Checks to see if the HTTP_X_REQUESTED_WITH server header has been |
| 298 | set, and returns a boolean response. |
| 299 | |
| 300 | $this->input->is_cli_request() |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 301 | ============================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 302 | |
| 303 | Checks to see if the STDIN constant is set, which is a failsafe way to |
| 304 | see if PHP is being run on the command line. |
| 305 | |
| 306 | :: |
| 307 | |
| 308 | $this->input->is_cli_request() |
| 309 | |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 310 | $this->input->method() |
| 311 | ====================== |
Michiel Vugteveen | be0ca26 | 2012-03-07 19:09:51 +0100 | [diff] [blame] | 312 | |
Michiel Vugteveen | 1e9fb49 | 2012-03-07 20:51:25 +0100 | [diff] [blame] | 313 | Returns the $_SERVER['REQUEST_METHOD'], optional set uppercase or lowercase (default lowercase). |
Michiel Vugteveen | be0ca26 | 2012-03-07 19:09:51 +0100 | [diff] [blame] | 314 | |
| 315 | :: |
| 316 | |
Michiel Vugteveen | dc900df | 2012-03-07 20:41:37 +0100 | [diff] [blame] | 317 | echo $this->input->method(TRUE); // Outputs: POST |
| 318 | echo $this->input->method(FALSE); // Outputs: post |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 319 | echo $this->input->method(); // Outputs: post |