blob: 4d8fdaf1523a970649de8eb1aa62e2e66665ff7f [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001###########
2Input Class
3###########
4
5The Input Class serves two purposes:
6
7#. It pre-processes global input data for security.
Andrey Andreev303eef02012-11-06 14:55:48 +02008#. It provides some helper methods for fetching input data and pre-processing it.
Derek Jones8ede1a22011-10-05 13:34:52 -05009
10.. note:: This class is initialized automatically by the system so there
11 is no need to do it manually.
12
Andrey Andreev04535c72014-01-06 10:57:05 +020013.. contents::
14 :local:
15
16.. raw:: html
17
18 <div class="custom-index container"></div>
19
Derek Jones8ede1a22011-10-05 13:34:52 -050020Security Filtering
21==================
22
Andrey Andreev303eef02012-11-06 14:55:48 +020023The security filtering method is called automatically when a new
Derek Jones8ede1a22011-10-05 13:34:52 -050024:doc:`controller <../general/controllers>` is invoked. It does the
25following:
26
Andrey Andreev04535c72014-01-06 10:57:05 +020027- If ``$config['allow_get_array']`` is FALSE (default is TRUE), destroys
Derek Jones8ede1a22011-10-05 13:34:52 -050028 the global GET array.
29- Destroys all global variables in the event register_globals is
30 turned on.
31- Filters the GET/POST/COOKIE array keys, permitting only alpha-numeric
32 (and a few other) characters.
33- Provides XSS (Cross-site Scripting Hacks) filtering. This can be
34 enabled globally, or upon request.
35- Standardizes newline characters to \\n(In Windows \\r\\n)
36
37XSS Filtering
38=============
39
40The Input class has the ability to filter input automatically to prevent
41cross-site scripting attacks. If you want the filter to run
42automatically every time it encounters POST or COOKIE data you can
Andrey Andreev04535c72014-01-06 10:57:05 +020043enable it by opening your *application/config/config.php* file and setting
Derek Jones8ede1a22011-10-05 13:34:52 -050044this::
45
46 $config['global_xss_filtering'] = TRUE;
47
48Please refer to the :doc:`Security class <security>` documentation for
49information on using XSS Filtering in your application.
50
Andrey Andreev9cf12d12012-06-13 10:19:59 +030051Using POST, GET, COOKIE, or SERVER Data
52=======================================
Derek Jones8ede1a22011-10-05 13:34:52 -050053
Andrey Andreev04535c72014-01-06 10:57:05 +020054CodeIgniter comes with helper methods that let you fetch POST, GET,
Derek Jones8ede1a22011-10-05 13:34:52 -050055COOKIE or SERVER items. The main advantage of using the provided
Andrey Andreev303eef02012-11-06 14:55:48 +020056methods rather than fetching an item directly (``$_POST['something']``)
Andrey Andreev9cf12d12012-06-13 10:19:59 +030057is that the methods will check to see if the item is set and return
58NULL if not. This lets you conveniently use data without
Derek Jones8ede1a22011-10-05 13:34:52 -050059having to test whether an item exists first. In other words, normally
60you might do something like this::
61
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +010062 $something = isset($_POST['something']) ? $_POST['something'] : NULL;
Derek Jones8ede1a22011-10-05 13:34:52 -050063
Andrey Andreev303eef02012-11-06 14:55:48 +020064With CodeIgniter's built in methods you can simply do this::
Derek Jones8ede1a22011-10-05 13:34:52 -050065
66 $something = $this->input->post('something');
67
Andrey Andreev04535c72014-01-06 10:57:05 +020068The main methods are:
Derek Jones8ede1a22011-10-05 13:34:52 -050069
70- $this->input->post()
Andrey Andreev22c3e732012-06-13 10:21:36 +030071- $this->input->get()
Derek Jones8ede1a22011-10-05 13:34:52 -050072- $this->input->cookie()
73- $this->input->server()
74
Andrey Andreev303eef02012-11-06 14:55:48 +020075Using the php://input stream
76============================
77
78If you want to utilize the PUT, DELETE, PATCH or other exotic request
79methods, they can only be accessed via a special input stream, that
80can only be read once. This isn't as easy as just reading from e.g.
81the ``$_POST`` array, because it will always exist and you can try
82and access multiple variables without caring that you might only have
83one shot at all of the POST data.
84
85CodeIgniter will take care of that for you, and you can access data
86from the **php://input** stream at any time, just by calling the
87``input_stream()`` method::
88
89 $this->input->input_stream('key');
90
Andrey Andreev04535c72014-01-06 10:57:05 +020091Similar to other methods such as ``get()`` and ``post()``, if the
92requested data is not found, it will return NULL and you can also
93decide whether to run the data through ``xss_clean()`` by passing
94a boolean value as the second parameter::
Andrey Andreev303eef02012-11-06 14:55:48 +020095
96 $this->input->input_stream('key', TRUE); // XSS Clean
97 $this->input->input_stream('key', FALSE); // No XSS filter
98
Andrey Andreev04535c72014-01-06 10:57:05 +020099.. note:: You can utilize ``method()`` in order to know if you're reading
Andrey Andreev303eef02012-11-06 14:55:48 +0200100 PUT, DELETE or PATCH data.
101
Andrey Andreev04535c72014-01-06 10:57:05 +0200102***************
103Class Reference
104***************
Derek Jones8ede1a22011-10-05 13:34:52 -0500105
Andrey Andreev04535c72014-01-06 10:57:05 +0200106.. class:: CI_Input
Derek Jones8ede1a22011-10-05 13:34:52 -0500107
Andrey Andreev88ebdf72014-01-08 17:28:02 +0200108 .. method:: post([$index = NULL[, $xss_clean = NULL]])
Derek Jones8ede1a22011-10-05 13:34:52 -0500109
Andrey Andreev04535c72014-01-06 10:57:05 +0200110 :param string $index: POST parameter name
111 :param bool $xss_clean: Whether to apply XSS filtering
112 :returns: mixed
Derek Jones8ede1a22011-10-05 13:34:52 -0500113
Andrey Andreev04535c72014-01-06 10:57:05 +0200114 The first parameter will contain the name of the POST item you are
115 looking for::
Derek Jones8831d112011-10-05 15:57:02 -0500116
Andrey Andreev04535c72014-01-06 10:57:05 +0200117 $this->input->post('some_data');
Derek Jones8ede1a22011-10-05 13:34:52 -0500118
Andrey Andreev04535c72014-01-06 10:57:05 +0200119 The method returns NULL if the item you are attempting to retrieve
120 does not exist.
Derek Jones8ede1a22011-10-05 13:34:52 -0500121
Andrey Andreev04535c72014-01-06 10:57:05 +0200122 The second optional parameter lets you run the data through the XSS
Andrey Andreev88ebdf72014-01-08 17:28:02 +0200123 filter. It's enabled by setting the second parameter to boolean TRUE
124 or by setting your ``$config['global_xss_filtering']`` to TRUE.
Andrey Andreev04535c72014-01-06 10:57:05 +0200125 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500126
Andrey Andreev04535c72014-01-06 10:57:05 +0200127 $this->input->post('some_data', TRUE);
Derek Jones8ede1a22011-10-05 13:34:52 -0500128
Andrey Andreev04535c72014-01-06 10:57:05 +0200129 To return an array of all POST items call without any parameters.
Derek Jones8ede1a22011-10-05 13:34:52 -0500130
Andrey Andreev04535c72014-01-06 10:57:05 +0200131 To return all POST items and pass them through the XSS filter set the
132 first parameter NULL while setting the second parameter to boolean TRUE.
133 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500134
Andrey Andreev04535c72014-01-06 10:57:05 +0200135 $this->input->post(NULL, TRUE); // returns all POST items with XSS filter
Andrey Andreev88ebdf72014-01-08 17:28:02 +0200136 $this->input->post(NULL, FALSE); // returns all POST items without XSS filter
Derek Jones8ede1a22011-10-05 13:34:52 -0500137
Andrey Andreev88ebdf72014-01-08 17:28:02 +0200138 .. method:: get([$index = NULL[, $xss_clean = NULL]])
Derek Jones8ede1a22011-10-05 13:34:52 -0500139
Andrey Andreev04535c72014-01-06 10:57:05 +0200140 :param string $index: GET parameter name
141 :param bool $xss_clean: Whether to apply XSS filtering
142 :returns: mixed
Derek Jones8ede1a22011-10-05 13:34:52 -0500143
Andrey Andreev04535c72014-01-06 10:57:05 +0200144 This method is identical to ``post()``, only it fetches GET data.
145 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500146
Andrey Andreev04535c72014-01-06 10:57:05 +0200147 $this->input->get('some_data', TRUE);
148
149 To return an array of all GET items call without any parameters.
150
151 To return all GET items and pass them through the XSS filter set the
152 first parameter NULL while setting the second parameter to boolean TRUE.
153 ::
154
155 $this->input->get(NULL, TRUE); // returns all GET items with XSS filter
Andrey Andreev88ebdf72014-01-08 17:28:02 +0200156 $this->input->get(NULL, FALSE); // returns all GET items without XSS filtering
Andrey Andreev04535c72014-01-06 10:57:05 +0200157
Andrey Andreev88ebdf72014-01-08 17:28:02 +0200158 .. method:: get_post([$index = ''[, $xss_clean = NULL]])
Andrey Andreev04535c72014-01-06 10:57:05 +0200159
160 :param string $index: GET/POST parameter name
161 :param bool $xss_clean: Whether to apply XSS filtering
162 :returns: mixed
163
164 This method works the same way as ``post()`` and ``get()``, only combined.
165 It will search through both POST and GET streams for data, looking first
166 in POST, and then in GET::
167
168 $this->input->get_post('some_data', TRUE);
169
Andrey Andreev88ebdf72014-01-08 17:28:02 +0200170 .. method:: cookie([$index = ''[, $xss_clean = NULL]])
Andrey Andreev04535c72014-01-06 10:57:05 +0200171
172 :param string $index: COOKIE parameter name
173 :param bool $xss_clean: Whether to apply XSS filtering
174 :returns: mixed
175
176 This method is identical to ``post()`` and ``get()``, only it fetches cookie
177 data::
178
179 $this->input->cookie('some_cookie');
180 $this->input->cookie('some_cookie, TRUE); // with XSS filter
181
Andrey Andreev88ebdf72014-01-08 17:28:02 +0200182 .. method:: server([$index = ''[, $xss_clean = NULL]])
Andrey Andreev04535c72014-01-06 10:57:05 +0200183
184 :param string $index: Value name
185 :param bool $xss_clean: Whether to apply XSS filtering
186 :returns: mixed
187
188 This method is identical to the ``post()``, ``get()`` and ``cookie()`` methods,
189 only it fetches server data (``$_SERVER``)::
190
191 $this->input->server('some_data');
192
Andrey Andreev88ebdf72014-01-08 17:28:02 +0200193 .. method:: input_stream([$index = ''[, $xss_clean = NULL]])
Andrey Andreev04535c72014-01-06 10:57:05 +0200194
195 :param string $index: Key name
196 :param bool $xss_clean: Whether to apply XSS filtering
197 :returns: mixed
198
199 This method is identical to ``get()``, ``post()`` and ``cookie()``,
200 only it fetches the *php://input* stream data.
201
202 .. method:: set_cookie($name = ''[, $value = ''[, $expire = ''[, $domain = ''[, $path = '/'[, $prefix = ''[, $secure = FALSE[, $httponly = FALSE]]]]]]])
203
204 :param mixed $name: Cookie name or an array of parameters
205 :param string $value: Cookie value
206 :param int $expire: Cookie expiration time in seconds
207 :param string $domain: Cookie domain
208 :param string $path: Cookie path
209 :param string $prefix: Cookie name prefix
210 :param bool $secure: Whether to only transfer the cookie through HTTPS
211 :param bool $httponly: Whether to only make the cookie accessible for HTTP requests (no JavaScript)
212 :returns: void
213
214 Sets a cookie containing the values you specify. There are two ways to
215 pass information to this method so that a cookie can be set: Array
216 Method, and Discrete Parameters:
217
218 Array Method
219 ^^^^^^^^^^^^
220
221 Using this method, an associative array is passed to the first
222 parameter::
223
224 $cookie = array(
225 'name' => 'The Cookie Name',
226 'value' => 'The Value',
227 'expire' => '86500',
228 'domain' => '.some-domain.com',
229 'path' => '/',
230 'prefix' => 'myprefix_',
231 'secure' => TRUE
232 );
233
234 $this->input->set_cookie($cookie);
235
236 **Notes:**
237
238 Only the name and value are required. To delete a cookie set it with the
239 expiration blank.
240
241 The expiration is set in **seconds**, which will be added to the current
242 time. Do not include the time, but rather only the number of seconds
243 from *now* that you wish the cookie to be valid. If the expiration is
244 set to zero the cookie will only last as long as the browser is open.
245
246 For site-wide cookies regardless of how your site is requested, add your
247 URL to the **domain** starting with a period, like this:
248 .your-domain.com
249
250 The path is usually not needed since the method sets a root path.
251
252 The prefix is only needed if you need to avoid name collisions with
253 other identically named cookies for your server.
254
255 The secure boolean is only needed if you want to make it a secure cookie
256 by setting it to TRUE.
257
258 Discrete Parameters
259 ^^^^^^^^^^^^^^^^^^^
260
261 If you prefer, you can set the cookie by passing data using individual
262 parameters::
263
264 $this->input->set_cookie($name, $value, $expire, $domain, $path, $prefix, $secure);
Derek Jones8ede1a22011-10-05 13:34:52 -0500265
Andrey Andreev303eef02012-11-06 14:55:48 +0200266
Andrey Andreev04535c72014-01-06 10:57:05 +0200267 .. method:: ip_address()
Derek Jones8ede1a22011-10-05 13:34:52 -0500268
Andrey Andreev04535c72014-01-06 10:57:05 +0200269 :returns: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500270
Andrey Andreev04535c72014-01-06 10:57:05 +0200271 Returns the IP address for the current user. If the IP address is not
272 valid, the method will return '0.0.0.0'::
Derek Jones8ede1a22011-10-05 13:34:52 -0500273
Andrey Andreev04535c72014-01-06 10:57:05 +0200274 echo $this->input->ip_address();
Derek Jones8ede1a22011-10-05 13:34:52 -0500275
Andrey Andreev04535c72014-01-06 10:57:05 +0200276 .. important:: This method takes into account the ``$config['proxy_ips']``
277 setting and will return the reported HTTP_X_FORWARDED_FOR,
278 HTTP_CLIENT_IP, HTTP_X_CLIENT_IP or HTTP_X_CLUSTER_CLIENT_IP
279 address for the allowed IP addresses.
Derek Jones8ede1a22011-10-05 13:34:52 -0500280
Andrey Andreev04535c72014-01-06 10:57:05 +0200281 .. method:: valid_ip($ip[, $which = ''])
Andrey Andreev303eef02012-11-06 14:55:48 +0200282
Andrey Andreev04535c72014-01-06 10:57:05 +0200283 :param string $ip: IP address
284 :param string $which: IP protocol ('ipv4' or 'ipv6')
285 :returns: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500286
Andrey Andreev04535c72014-01-06 10:57:05 +0200287 Takes an IP address as input and returns TRUE or FALSE (boolean) depending
288 on whether it is valid or not.
Derek Jones8ede1a22011-10-05 13:34:52 -0500289
Andrey Andreev04535c72014-01-06 10:57:05 +0200290 .. note:: The $this->input->ip_address() method above automatically
291 validates the IP address.
Derek Jones8ede1a22011-10-05 13:34:52 -0500292
Andrey Andreev04535c72014-01-06 10:57:05 +0200293 ::
Andrey Andreev5a257182012-06-10 06:18:14 +0300294
Andrey Andreev04535c72014-01-06 10:57:05 +0200295 if ( ! $this->input->valid_ip($ip))
296 {
297 echo 'Not Valid';
298 }
299 else
300 {
301 echo 'Valid';
302 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500303
Andrey Andreev04535c72014-01-06 10:57:05 +0200304 Accepts an optional second string parameter of 'ipv4' or 'ipv6' to specify
305 an IP format. The default checks for both formats.
Derek Jones8ede1a22011-10-05 13:34:52 -0500306
Andrey Andreev04535c72014-01-06 10:57:05 +0200307 .. method:: user_agent()
Derek Jones8ede1a22011-10-05 13:34:52 -0500308
Andrey Andreev04535c72014-01-06 10:57:05 +0200309 :returns: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500310
Andrey Andreev04535c72014-01-06 10:57:05 +0200311 Returns the user agent string (web browser) being used by the current user,
312 or NULL if it's not available.
313 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500314
Andrey Andreev04535c72014-01-06 10:57:05 +0200315 echo $this->input->user_agent();
Derek Jones8ede1a22011-10-05 13:34:52 -0500316
Andrey Andreev04535c72014-01-06 10:57:05 +0200317 See the :doc:`User Agent Class <user_agent>` for methods which extract
318 information from the user agent string.
Derek Jones8ede1a22011-10-05 13:34:52 -0500319
Andrey Andreev04535c72014-01-06 10:57:05 +0200320 .. method:: request_headers([$xss_clean = FALSE])
Derek Jones8ede1a22011-10-05 13:34:52 -0500321
Andrey Andreev04535c72014-01-06 10:57:05 +0200322 :param bool $xss_clean: Whether to apply XSS filtering
323 :returns: array
Derek Jones8ede1a22011-10-05 13:34:52 -0500324
Andrey Andreev04535c72014-01-06 10:57:05 +0200325 Returns an array of HTTP request headers.
326 Useful if running in a non-Apache environment where
327 `apache_request_headers() <http://php.net/apache_request_headers>`_
328 will not be supported.
329 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500330
Andrey Andreev04535c72014-01-06 10:57:05 +0200331 $headers = $this->input->request_headers();
Derek Jones8ede1a22011-10-05 13:34:52 -0500332
Andrey Andreev04535c72014-01-06 10:57:05 +0200333 .. method:: get_request_header($index[, $xss_clean = FALSE])
Derek Jones8ede1a22011-10-05 13:34:52 -0500334
Andrey Andreev04535c72014-01-06 10:57:05 +0200335 :param string $index: HTTP request header name
336 :param bool $xss_clean: Whether to apply XSS filtering
337 :returns: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500338
Andrey Andreev04535c72014-01-06 10:57:05 +0200339 Returns a single member of the request headers array or NULL
340 if the searched header is not found.
341 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500342
Andrey Andreev04535c72014-01-06 10:57:05 +0200343 $this->input->get_request_header('some-header', TRUE);
Derek Jones8ede1a22011-10-05 13:34:52 -0500344
Andrey Andreev04535c72014-01-06 10:57:05 +0200345 .. method:: is_ajax_request()
Derek Jones8ede1a22011-10-05 13:34:52 -0500346
Andrey Andreev04535c72014-01-06 10:57:05 +0200347 :returns: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500348
Andrey Andreev04535c72014-01-06 10:57:05 +0200349 Checks to see if the HTTP_X_REQUESTED_WITH server header has been
350 set, and returns boolean TRUE if it is or FALSE if not.
Derek Jones8ede1a22011-10-05 13:34:52 -0500351
Andrey Andreev04535c72014-01-06 10:57:05 +0200352 .. method:: is_cli_request()
Derek Jones8ede1a22011-10-05 13:34:52 -0500353
Andrey Andreev04535c72014-01-06 10:57:05 +0200354 :returns: bool
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100355
Andrey Andreev04535c72014-01-06 10:57:05 +0200356 Checks to see if the application was run from the command-line
357 interface.
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100358
Andrey Andreev04535c72014-01-06 10:57:05 +0200359 .. note:: This method checks both the PHP SAPI name currently in use
360 and if the ``STDIN`` constant is defined, which is usually a
361 failsafe way to see if PHP is being run via the command line.
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100362
Andrey Andreev04535c72014-01-06 10:57:05 +0200363 ::
364
365 $this->input->is_cli_request()
366
367 .. method:: method([$upper = FALSE])
368
369 :param bool $upper: Whether to return the request method name in upper or lower case
370 :returns: string
371
372 Returns the ``$_SERVER['REQUEST_METHOD']``, with the option to set it
373 in uppercase or lowercase.
374 ::
375
376 echo $this->input->method(TRUE); // Outputs: POST
377 echo $this->input->method(FALSE); // Outputs: post
378 echo $this->input->method(); // Outputs: post