blob: 8a83207af2a71d2a0951375beb54c6810872f4fd [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.
Andrey Andreevbfb635b2014-01-08 18:32:05 +020035- Standardizes newline characters to ``PHP_EOL`` (\\n in UNIX-based OSes,
36 \\r\\n under Windows). This is configurable.
Derek Jones8ede1a22011-10-05 13:34:52 -050037
38XSS Filtering
39=============
40
41The Input class has the ability to filter input automatically to prevent
42cross-site scripting attacks. If you want the filter to run
43automatically every time it encounters POST or COOKIE data you can
Andrey Andreev04535c72014-01-06 10:57:05 +020044enable it by opening your *application/config/config.php* file and setting
Derek Jones8ede1a22011-10-05 13:34:52 -050045this::
46
47 $config['global_xss_filtering'] = TRUE;
48
49Please refer to the :doc:`Security class <security>` documentation for
50information on using XSS Filtering in your application.
51
Andrey Andreev9cf12d12012-06-13 10:19:59 +030052Using POST, GET, COOKIE, or SERVER Data
53=======================================
Derek Jones8ede1a22011-10-05 13:34:52 -050054
Andrey Andreev04535c72014-01-06 10:57:05 +020055CodeIgniter comes with helper methods that let you fetch POST, GET,
Derek Jones8ede1a22011-10-05 13:34:52 -050056COOKIE or SERVER items. The main advantage of using the provided
Andrey Andreev303eef02012-11-06 14:55:48 +020057methods rather than fetching an item directly (``$_POST['something']``)
Andrey Andreev9cf12d12012-06-13 10:19:59 +030058is that the methods will check to see if the item is set and return
59NULL if not. This lets you conveniently use data without
Derek Jones8ede1a22011-10-05 13:34:52 -050060having to test whether an item exists first. In other words, normally
61you might do something like this::
62
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +010063 $something = isset($_POST['something']) ? $_POST['something'] : NULL;
Derek Jones8ede1a22011-10-05 13:34:52 -050064
Andrey Andreev303eef02012-11-06 14:55:48 +020065With CodeIgniter's built in methods you can simply do this::
Derek Jones8ede1a22011-10-05 13:34:52 -050066
67 $something = $this->input->post('something');
68
Andrey Andreev04535c72014-01-06 10:57:05 +020069The main methods are:
Derek Jones8ede1a22011-10-05 13:34:52 -050070
71- $this->input->post()
Andrey Andreev22c3e732012-06-13 10:21:36 +030072- $this->input->get()
Derek Jones8ede1a22011-10-05 13:34:52 -050073- $this->input->cookie()
74- $this->input->server()
75
Andrey Andreev303eef02012-11-06 14:55:48 +020076Using the php://input stream
77============================
78
79If you want to utilize the PUT, DELETE, PATCH or other exotic request
80methods, they can only be accessed via a special input stream, that
81can only be read once. This isn't as easy as just reading from e.g.
82the ``$_POST`` array, because it will always exist and you can try
83and access multiple variables without caring that you might only have
84one shot at all of the POST data.
85
86CodeIgniter will take care of that for you, and you can access data
87from the **php://input** stream at any time, just by calling the
88``input_stream()`` method::
89
90 $this->input->input_stream('key');
91
Andrey Andreev04535c72014-01-06 10:57:05 +020092Similar to other methods such as ``get()`` and ``post()``, if the
93requested data is not found, it will return NULL and you can also
94decide whether to run the data through ``xss_clean()`` by passing
95a boolean value as the second parameter::
Andrey Andreev303eef02012-11-06 14:55:48 +020096
97 $this->input->input_stream('key', TRUE); // XSS Clean
98 $this->input->input_stream('key', FALSE); // No XSS filter
99
Andrey Andreev04535c72014-01-06 10:57:05 +0200100.. note:: You can utilize ``method()`` in order to know if you're reading
Andrey Andreev303eef02012-11-06 14:55:48 +0200101 PUT, DELETE or PATCH data.
102
Andrey Andreev04535c72014-01-06 10:57:05 +0200103***************
104Class Reference
105***************
Derek Jones8ede1a22011-10-05 13:34:52 -0500106
Andrey Andreev04535c72014-01-06 10:57:05 +0200107.. class:: CI_Input
Derek Jones8ede1a22011-10-05 13:34:52 -0500108
Andrey Andreev88ebdf72014-01-08 17:28:02 +0200109 .. method:: post([$index = NULL[, $xss_clean = NULL]])
Derek Jones8ede1a22011-10-05 13:34:52 -0500110
Andrey Andreev04535c72014-01-06 10:57:05 +0200111 :param string $index: POST parameter name
112 :param bool $xss_clean: Whether to apply XSS filtering
113 :returns: mixed
Derek Jones8ede1a22011-10-05 13:34:52 -0500114
Andrey Andreev04535c72014-01-06 10:57:05 +0200115 The first parameter will contain the name of the POST item you are
116 looking for::
Derek Jones8831d112011-10-05 15:57:02 -0500117
Andrey Andreev04535c72014-01-06 10:57:05 +0200118 $this->input->post('some_data');
Derek Jones8ede1a22011-10-05 13:34:52 -0500119
Andrey Andreev04535c72014-01-06 10:57:05 +0200120 The method returns NULL if the item you are attempting to retrieve
121 does not exist.
Derek Jones8ede1a22011-10-05 13:34:52 -0500122
Andrey Andreev04535c72014-01-06 10:57:05 +0200123 The second optional parameter lets you run the data through the XSS
Andrey Andreev88ebdf72014-01-08 17:28:02 +0200124 filter. It's enabled by setting the second parameter to boolean TRUE
125 or by setting your ``$config['global_xss_filtering']`` to TRUE.
Andrey Andreev04535c72014-01-06 10:57:05 +0200126 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500127
Andrey Andreev04535c72014-01-06 10:57:05 +0200128 $this->input->post('some_data', TRUE);
Derek Jones8ede1a22011-10-05 13:34:52 -0500129
Andrey Andreev04535c72014-01-06 10:57:05 +0200130 To return an array of all POST items call without any parameters.
Derek Jones8ede1a22011-10-05 13:34:52 -0500131
Andrey Andreev04535c72014-01-06 10:57:05 +0200132 To return all POST items and pass them through the XSS filter set the
133 first parameter NULL while setting the second parameter to boolean TRUE.
134 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500135
Andrey Andreev04535c72014-01-06 10:57:05 +0200136 $this->input->post(NULL, TRUE); // returns all POST items with XSS filter
Andrey Andreev88ebdf72014-01-08 17:28:02 +0200137 $this->input->post(NULL, FALSE); // returns all POST items without XSS filter
Derek Jones8ede1a22011-10-05 13:34:52 -0500138
Andrey Andreev88ebdf72014-01-08 17:28:02 +0200139 .. method:: get([$index = NULL[, $xss_clean = NULL]])
Derek Jones8ede1a22011-10-05 13:34:52 -0500140
Andrey Andreev04535c72014-01-06 10:57:05 +0200141 :param string $index: GET parameter name
142 :param bool $xss_clean: Whether to apply XSS filtering
143 :returns: mixed
Derek Jones8ede1a22011-10-05 13:34:52 -0500144
Andrey Andreev04535c72014-01-06 10:57:05 +0200145 This method is identical to ``post()``, only it fetches GET data.
146 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500147
Andrey Andreev04535c72014-01-06 10:57:05 +0200148 $this->input->get('some_data', TRUE);
149
150 To return an array of all GET items call without any parameters.
151
152 To return all GET items and pass them through the XSS filter set the
153 first parameter NULL while setting the second parameter to boolean TRUE.
154 ::
155
156 $this->input->get(NULL, TRUE); // returns all GET items with XSS filter
Andrey Andreev88ebdf72014-01-08 17:28:02 +0200157 $this->input->get(NULL, FALSE); // returns all GET items without XSS filtering
Andrey Andreev04535c72014-01-06 10:57:05 +0200158
Andrey Andreevea801ab2014-01-20 15:03:43 +0200159 .. method:: post_get([$index = ''[, $xss_clean = NULL]])
160
161 :param string $index: POST/GET parameter name
162 :param bool $xss_clean: Whether to apply XSS filtering
163 :returns: mixed
164
165 This method works the same way as ``post()`` and ``get()``, only combined.
166 It will search through both POST and GET streams for data, looking in POST
167 first, and then in GET::
168
169 $this->input->post_get('some_data', TRUE);
170
Andrey Andreev88ebdf72014-01-08 17:28:02 +0200171 .. method:: get_post([$index = ''[, $xss_clean = NULL]])
Andrey Andreev04535c72014-01-06 10:57:05 +0200172
173 :param string $index: GET/POST parameter name
174 :param bool $xss_clean: Whether to apply XSS filtering
175 :returns: mixed
176
Andrey Andreevea801ab2014-01-20 15:03:43 +0200177 This method works the same way as ``post_get()`` only it looks for GET
178 data first.
Andrey Andreev04535c72014-01-06 10:57:05 +0200179
180 $this->input->get_post('some_data', TRUE);
181
Andrey Andreevea801ab2014-01-20 15:03:43 +0200182 .. note:: This method used to act EXACTLY like ``post_get()``, but it's
183 behavior has changed in CodeIgniter 3.0.
184
Andrey Andreev88ebdf72014-01-08 17:28:02 +0200185 .. method:: cookie([$index = ''[, $xss_clean = NULL]])
Andrey Andreev04535c72014-01-06 10:57:05 +0200186
187 :param string $index: COOKIE parameter name
188 :param bool $xss_clean: Whether to apply XSS filtering
189 :returns: mixed
190
191 This method is identical to ``post()`` and ``get()``, only it fetches cookie
192 data::
193
194 $this->input->cookie('some_cookie');
195 $this->input->cookie('some_cookie, TRUE); // with XSS filter
196
Andrey Andreev88ebdf72014-01-08 17:28:02 +0200197 .. method:: server([$index = ''[, $xss_clean = NULL]])
Andrey Andreev04535c72014-01-06 10:57:05 +0200198
199 :param string $index: Value name
200 :param bool $xss_clean: Whether to apply XSS filtering
201 :returns: mixed
202
203 This method is identical to the ``post()``, ``get()`` and ``cookie()`` methods,
204 only it fetches server data (``$_SERVER``)::
205
206 $this->input->server('some_data');
207
Andrey Andreev88ebdf72014-01-08 17:28:02 +0200208 .. method:: input_stream([$index = ''[, $xss_clean = NULL]])
Andrey Andreev04535c72014-01-06 10:57:05 +0200209
210 :param string $index: Key name
211 :param bool $xss_clean: Whether to apply XSS filtering
212 :returns: mixed
213
214 This method is identical to ``get()``, ``post()`` and ``cookie()``,
215 only it fetches the *php://input* stream data.
216
217 .. method:: set_cookie($name = ''[, $value = ''[, $expire = ''[, $domain = ''[, $path = '/'[, $prefix = ''[, $secure = FALSE[, $httponly = FALSE]]]]]]])
218
219 :param mixed $name: Cookie name or an array of parameters
220 :param string $value: Cookie value
221 :param int $expire: Cookie expiration time in seconds
222 :param string $domain: Cookie domain
223 :param string $path: Cookie path
224 :param string $prefix: Cookie name prefix
225 :param bool $secure: Whether to only transfer the cookie through HTTPS
226 :param bool $httponly: Whether to only make the cookie accessible for HTTP requests (no JavaScript)
227 :returns: void
228
229 Sets a cookie containing the values you specify. There are two ways to
230 pass information to this method so that a cookie can be set: Array
231 Method, and Discrete Parameters:
232
Connor Tumbleson75b3fb22014-01-11 06:58:43 -0600233 **Array Method**
Andrey Andreev04535c72014-01-06 10:57:05 +0200234
235 Using this method, an associative array is passed to the first
236 parameter::
237
238 $cookie = array(
239 'name' => 'The Cookie Name',
240 'value' => 'The Value',
241 'expire' => '86500',
242 'domain' => '.some-domain.com',
243 'path' => '/',
244 'prefix' => 'myprefix_',
245 'secure' => TRUE
246 );
247
248 $this->input->set_cookie($cookie);
249
250 **Notes:**
251
252 Only the name and value are required. To delete a cookie set it with the
253 expiration blank.
254
255 The expiration is set in **seconds**, which will be added to the current
256 time. Do not include the time, but rather only the number of seconds
257 from *now* that you wish the cookie to be valid. If the expiration is
258 set to zero the cookie will only last as long as the browser is open.
259
260 For site-wide cookies regardless of how your site is requested, add your
261 URL to the **domain** starting with a period, like this:
262 .your-domain.com
263
264 The path is usually not needed since the method sets a root path.
265
266 The prefix is only needed if you need to avoid name collisions with
267 other identically named cookies for your server.
268
269 The secure boolean is only needed if you want to make it a secure cookie
270 by setting it to TRUE.
271
Connor Tumbleson75b3fb22014-01-11 06:58:43 -0600272 **Discrete Parameters**
Andrey Andreev04535c72014-01-06 10:57:05 +0200273
274 If you prefer, you can set the cookie by passing data using individual
275 parameters::
276
277 $this->input->set_cookie($name, $value, $expire, $domain, $path, $prefix, $secure);
Derek Jones8ede1a22011-10-05 13:34:52 -0500278
Andrey Andreev303eef02012-11-06 14:55:48 +0200279
Andrey Andreev04535c72014-01-06 10:57:05 +0200280 .. method:: ip_address()
Derek Jones8ede1a22011-10-05 13:34:52 -0500281
Andrey Andreev04535c72014-01-06 10:57:05 +0200282 :returns: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500283
Andrey Andreev04535c72014-01-06 10:57:05 +0200284 Returns the IP address for the current user. If the IP address is not
285 valid, the method will return '0.0.0.0'::
Derek Jones8ede1a22011-10-05 13:34:52 -0500286
Andrey Andreev04535c72014-01-06 10:57:05 +0200287 echo $this->input->ip_address();
Derek Jones8ede1a22011-10-05 13:34:52 -0500288
Andrey Andreev04535c72014-01-06 10:57:05 +0200289 .. important:: This method takes into account the ``$config['proxy_ips']``
290 setting and will return the reported HTTP_X_FORWARDED_FOR,
291 HTTP_CLIENT_IP, HTTP_X_CLIENT_IP or HTTP_X_CLUSTER_CLIENT_IP
292 address for the allowed IP addresses.
Derek Jones8ede1a22011-10-05 13:34:52 -0500293
Andrey Andreev04535c72014-01-06 10:57:05 +0200294 .. method:: valid_ip($ip[, $which = ''])
Andrey Andreev303eef02012-11-06 14:55:48 +0200295
Andrey Andreev04535c72014-01-06 10:57:05 +0200296 :param string $ip: IP address
297 :param string $which: IP protocol ('ipv4' or 'ipv6')
298 :returns: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500299
Andrey Andreev04535c72014-01-06 10:57:05 +0200300 Takes an IP address as input and returns TRUE or FALSE (boolean) depending
301 on whether it is valid or not.
Derek Jones8ede1a22011-10-05 13:34:52 -0500302
Andrey Andreev04535c72014-01-06 10:57:05 +0200303 .. note:: The $this->input->ip_address() method above automatically
304 validates the IP address.
Derek Jones8ede1a22011-10-05 13:34:52 -0500305
Andrey Andreev04535c72014-01-06 10:57:05 +0200306 ::
Andrey Andreev5a257182012-06-10 06:18:14 +0300307
Andrey Andreev04535c72014-01-06 10:57:05 +0200308 if ( ! $this->input->valid_ip($ip))
309 {
310 echo 'Not Valid';
311 }
312 else
313 {
314 echo 'Valid';
315 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500316
Andrey Andreev04535c72014-01-06 10:57:05 +0200317 Accepts an optional second string parameter of 'ipv4' or 'ipv6' to specify
318 an IP format. The default checks for both formats.
Derek Jones8ede1a22011-10-05 13:34:52 -0500319
Andrey Andreev04535c72014-01-06 10:57:05 +0200320 .. method:: user_agent()
Derek Jones8ede1a22011-10-05 13:34:52 -0500321
Andrey Andreev04535c72014-01-06 10:57:05 +0200322 :returns: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500323
Andrey Andreev04535c72014-01-06 10:57:05 +0200324 Returns the user agent string (web browser) being used by the current user,
325 or NULL if it's not available.
326 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500327
Andrey Andreev04535c72014-01-06 10:57:05 +0200328 echo $this->input->user_agent();
Derek Jones8ede1a22011-10-05 13:34:52 -0500329
Andrey Andreev04535c72014-01-06 10:57:05 +0200330 See the :doc:`User Agent Class <user_agent>` for methods which extract
331 information from the user agent string.
Derek Jones8ede1a22011-10-05 13:34:52 -0500332
Andrey Andreev04535c72014-01-06 10:57:05 +0200333 .. method:: request_headers([$xss_clean = FALSE])
Derek Jones8ede1a22011-10-05 13:34:52 -0500334
Andrey Andreev04535c72014-01-06 10:57:05 +0200335 :param bool $xss_clean: Whether to apply XSS filtering
336 :returns: array
Derek Jones8ede1a22011-10-05 13:34:52 -0500337
Andrey Andreev04535c72014-01-06 10:57:05 +0200338 Returns an array of HTTP request headers.
339 Useful if running in a non-Apache environment where
340 `apache_request_headers() <http://php.net/apache_request_headers>`_
341 will not be supported.
342 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500343
Andrey Andreev04535c72014-01-06 10:57:05 +0200344 $headers = $this->input->request_headers();
Derek Jones8ede1a22011-10-05 13:34:52 -0500345
Andrey Andreev04535c72014-01-06 10:57:05 +0200346 .. method:: get_request_header($index[, $xss_clean = FALSE])
Derek Jones8ede1a22011-10-05 13:34:52 -0500347
Andrey Andreev04535c72014-01-06 10:57:05 +0200348 :param string $index: HTTP request header name
349 :param bool $xss_clean: Whether to apply XSS filtering
350 :returns: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500351
Andrey Andreev04535c72014-01-06 10:57:05 +0200352 Returns a single member of the request headers array or NULL
353 if the searched header is not found.
354 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500355
Andrey Andreev04535c72014-01-06 10:57:05 +0200356 $this->input->get_request_header('some-header', TRUE);
Derek Jones8ede1a22011-10-05 13:34:52 -0500357
Andrey Andreev04535c72014-01-06 10:57:05 +0200358 .. method:: is_ajax_request()
Derek Jones8ede1a22011-10-05 13:34:52 -0500359
Andrey Andreev04535c72014-01-06 10:57:05 +0200360 :returns: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500361
Andrey Andreev04535c72014-01-06 10:57:05 +0200362 Checks to see if the HTTP_X_REQUESTED_WITH server header has been
363 set, and returns boolean TRUE if it is or FALSE if not.
Derek Jones8ede1a22011-10-05 13:34:52 -0500364
Andrey Andreev04535c72014-01-06 10:57:05 +0200365 .. method:: is_cli_request()
Derek Jones8ede1a22011-10-05 13:34:52 -0500366
Andrey Andreev04535c72014-01-06 10:57:05 +0200367 :returns: bool
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100368
Andrey Andreev04535c72014-01-06 10:57:05 +0200369 Checks to see if the application was run from the command-line
370 interface.
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100371
Andrey Andreev04535c72014-01-06 10:57:05 +0200372 .. note:: This method checks both the PHP SAPI name currently in use
373 and if the ``STDIN`` constant is defined, which is usually a
374 failsafe way to see if PHP is being run via the command line.
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100375
Andrey Andreev04535c72014-01-06 10:57:05 +0200376 ::
377
378 $this->input->is_cli_request()
379
Andrey Andreevea801ab2014-01-20 15:03:43 +0200380 .. note:: This method is DEPRECATED and is now just an alias for the
381 :func:`is_cli()` function.
382
Andrey Andreev04535c72014-01-06 10:57:05 +0200383 .. method:: method([$upper = FALSE])
384
385 :param bool $upper: Whether to return the request method name in upper or lower case
386 :returns: string
387
388 Returns the ``$_SERVER['REQUEST_METHOD']``, with the option to set it
389 in uppercase or lowercase.
390 ::
391
392 echo $this->input->method(TRUE); // Outputs: POST
393 echo $this->input->method(FALSE); // Outputs: post
394 echo $this->input->method(); // Outputs: post