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