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 | |
| 21 | - If $config['allow_get_array'] is FALSE(default is TRUE), destroys |
| 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 | |
| 45 | Using POST, COOKIE, or SERVER Data |
| 46 | ================================== |
| 47 | |
| 48 | CodeIgniter comes with three helper functions that let you fetch POST, |
| 49 | COOKIE or SERVER items. The main advantage of using the provided |
| 50 | functions rather than fetching an item directly ($_POST['something']) |
| 51 | is that the functions will check to see if the item is set and return |
| 52 | false (boolean) if not. This lets you conveniently use data without |
| 53 | having to test whether an item exists first. In other words, normally |
| 54 | you might do something like this:: |
| 55 | |
Derek Jones | 8831d11 | 2011-10-05 15:57:02 -0500 | [diff] [blame] | 56 | if ( ! isset($_POST['something'])) |
| 57 | { |
| 58 | $something = FALSE; |
| 59 | } |
| 60 | else |
| 61 | { |
| 62 | $something = $_POST['something']; |
| 63 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 64 | |
| 65 | With CodeIgniter's built in functions you can simply do this:: |
| 66 | |
| 67 | $something = $this->input->post('something'); |
| 68 | |
| 69 | The three functions are: |
| 70 | |
| 71 | - $this->input->post() |
| 72 | - $this->input->cookie() |
| 73 | - $this->input->server() |
| 74 | |
| 75 | $this->input->post() |
| 76 | ==================== |
| 77 | |
| 78 | The first parameter will contain the name of the POST item you are |
| 79 | looking for:: |
| 80 | |
| 81 | $this->input->post('some_data'); |
| 82 | |
| 83 | The function returns FALSE (boolean) if the item you are attempting to |
| 84 | retrieve does not exist. |
| 85 | |
| 86 | The second optional parameter lets you run the data through the XSS |
| 87 | filter. It's enabled by setting the second parameter to boolean TRUE; |
| 88 | |
| 89 | :: |
| 90 | |
| 91 | $this->input->post('some_data', TRUE); |
| 92 | |
| 93 | To return an array of all POST items call without any parameters. |
| 94 | |
| 95 | To return all POST items and pass them through the XSS filter set the |
| 96 | first parameter NULL while setting the second parameter to boolean; |
| 97 | |
| 98 | The function returns FALSE (boolean) if there are no items in the POST. |
| 99 | |
| 100 | :: |
| 101 | |
Derek Jones | 8831d11 | 2011-10-05 15:57:02 -0500 | [diff] [blame] | 102 | $this->input->post(NULL, TRUE); // returns all POST items with XSS filter |
| 103 | $this->input->post(); // returns all POST items without XSS filter |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 104 | |
| 105 | $this->input->get() |
| 106 | =================== |
| 107 | |
| 108 | This function is identical to the post function, only it fetches get |
| 109 | data:: |
| 110 | |
| 111 | $this->input->get('some_data', TRUE); |
| 112 | |
| 113 | To return an array of all GET items call without any parameters. |
| 114 | |
| 115 | To return all GET items and pass them through the XSS filter set the |
| 116 | first parameter NULL while setting the second parameter to boolean; |
| 117 | |
| 118 | The function returns FALSE (boolean) if there are no items in the GET. |
| 119 | |
| 120 | :: |
| 121 | |
Derek Jones | 8831d11 | 2011-10-05 15:57:02 -0500 | [diff] [blame] | 122 | $this->input->get(NULL, TRUE); // returns all GET items with XSS filter |
| 123 | $this->input->get(); // returns all GET items without XSS filtering |
| 124 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 125 | |
| 126 | $this->input->get_post() |
| 127 | ========================= |
| 128 | |
| 129 | This function will search through both the post and get streams for |
| 130 | data, looking first in post, and then in get:: |
| 131 | |
| 132 | $this->input->get_post('some_data', TRUE); |
| 133 | |
| 134 | $this->input->cookie() |
| 135 | ====================== |
| 136 | |
| 137 | This function is identical to the post function, only it fetches cookie |
| 138 | data:: |
| 139 | |
| 140 | $this->input->cookie('some_data', TRUE); |
| 141 | |
| 142 | $this->input->server() |
| 143 | ====================== |
| 144 | |
| 145 | This function is identical to the above functions, only it fetches |
| 146 | server data:: |
| 147 | |
| 148 | $this->input->server('some_data'); |
| 149 | |
| 150 | $this->input->set_cookie() |
| 151 | =========================== |
| 152 | |
| 153 | Sets a cookie containing the values you specify. There are two ways to |
| 154 | pass information to this function so that a cookie can be set: Array |
| 155 | Method, and Discrete Parameters: |
| 156 | |
| 157 | Array Method |
| 158 | ^^^^^^^^^^^^ |
| 159 | |
| 160 | Using this method, an associative array is passed to the first |
| 161 | parameter:: |
| 162 | |
Derek Jones | 8831d11 | 2011-10-05 15:57:02 -0500 | [diff] [blame] | 163 | $cookie = array( |
| 164 | 'name' => 'The Cookie Name', |
| 165 | 'value' => 'The Value', |
| 166 | 'expire' => '86500', |
| 167 | 'domain' => '.some-domain.com', |
| 168 | 'path' => '/', |
| 169 | 'prefix' => 'myprefix_', |
| 170 | 'secure' => TRUE |
| 171 | ); |
| 172 | |
| 173 | $this->input->set_cookie($cookie); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 174 | |
| 175 | **Notes:** |
| 176 | |
| 177 | Only the name and value are required. To delete a cookie set it with the |
| 178 | expiration blank. |
| 179 | |
| 180 | The expiration is set in **seconds**, which will be added to the current |
| 181 | time. Do not include the time, but rather only the number of seconds |
| 182 | from *now* that you wish the cookie to be valid. If the expiration is |
| 183 | set to zero the cookie will only last as long as the browser is open. |
| 184 | |
| 185 | For site-wide cookies regardless of how your site is requested, add your |
| 186 | URL to the **domain** starting with a period, like this: |
| 187 | .your-domain.com |
| 188 | |
| 189 | The path is usually not needed since the function sets a root path. |
| 190 | |
| 191 | The prefix is only needed if you need to avoid name collisions with |
| 192 | other identically named cookies for your server. |
| 193 | |
| 194 | The secure boolean is only needed if you want to make it a secure cookie |
| 195 | by setting it to TRUE. |
| 196 | |
| 197 | Discrete Parameters |
| 198 | ^^^^^^^^^^^^^^^^^^^ |
| 199 | |
| 200 | If you prefer, you can set the cookie by passing data using individual |
| 201 | parameters:: |
| 202 | |
| 203 | $this->input->set_cookie($name, $value, $expire, $domain, $path, $prefix, $secure); |
| 204 | |
| 205 | $this->input->cookie() |
| 206 | ====================== |
| 207 | |
| 208 | Lets you fetch a cookie. The first parameter will contain the name of |
| 209 | the cookie you are looking for (including any prefixes):: |
| 210 | |
| 211 | cookie('some_cookie'); |
| 212 | |
| 213 | The function returns FALSE (boolean) if the item you are attempting to |
| 214 | retrieve does not exist. |
| 215 | |
| 216 | The second optional parameter lets you run the data through the XSS |
| 217 | filter. It's enabled by setting the second parameter to boolean TRUE; |
| 218 | |
| 219 | :: |
| 220 | |
| 221 | cookie('some_cookie', TRUE); |
| 222 | |
| 223 | |
| 224 | $this->input->ip_address() |
| 225 | =========================== |
| 226 | |
| 227 | Returns the IP address for the current user. If the IP address is not |
| 228 | valid, the function will return an IP of: 0.0.0.0 |
| 229 | |
| 230 | :: |
| 231 | |
| 232 | echo $this->input->ip_address(); |
| 233 | |
| 234 | $this->input->valid_ip($ip) |
| 235 | ============================ |
| 236 | |
| 237 | Takes an IP address as input and returns TRUE or FALSE (boolean) if it |
| 238 | is valid or not. Note: The $this->input->ip_address() function above |
| 239 | validates the IP automatically. |
| 240 | |
| 241 | :: |
| 242 | |
Derek Jones | 8831d11 | 2011-10-05 15:57:02 -0500 | [diff] [blame] | 243 | if ( ! $this->input->valid_ip($ip)) |
| 244 | { |
| 245 | echo 'Not Valid'; |
| 246 | } |
| 247 | else |
| 248 | { |
| 249 | echo 'Valid'; |
| 250 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 251 | |
| 252 | $this->input->user_agent() |
| 253 | =========================== |
| 254 | |
| 255 | Returns the user agent (web browser) being used by the current user. |
| 256 | Returns FALSE if it's not available. |
| 257 | |
| 258 | :: |
| 259 | |
| 260 | echo $this->input->user_agent(); |
| 261 | |
| 262 | See the :doc:`User Agent Class <user_agent>` for methods which extract |
| 263 | information from the user agent string. |
| 264 | |
| 265 | $this->input->request_headers() |
| 266 | ================================ |
| 267 | |
| 268 | Useful if running in a non-Apache environment where |
| 269 | `apache_request_headers() <http://php.net/apache_request_headers>`_ |
| 270 | will not be supported. Returns an array of headers. |
| 271 | |
| 272 | :: |
| 273 | |
| 274 | $headers = $this->input->request_headers(); |
| 275 | |
| 276 | $this->input->get_request_header(); |
| 277 | ===================================== |
| 278 | |
| 279 | Returns a single member of the request headers array. |
| 280 | |
| 281 | :: |
| 282 | |
| 283 | $this->input->get_request_header('some-header', TRUE); |
| 284 | |
| 285 | $this->input->is_ajax_request() |
| 286 | ================================= |
| 287 | |
| 288 | Checks to see if the HTTP_X_REQUESTED_WITH server header has been |
| 289 | set, and returns a boolean response. |
| 290 | |
| 291 | $this->input->is_cli_request() |
| 292 | ================================ |
| 293 | |
| 294 | Checks to see if the STDIN constant is set, which is a failsafe way to |
| 295 | see if PHP is being run on the command line. |
| 296 | |
| 297 | :: |
| 298 | |
| 299 | $this->input->is_cli_request() |
| 300 | |