blob: 39a0d0628f9881e8b2f4bdaa0865ed5e63eff09f [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 Andreev04535c72014-01-06 10:57:05 +0200108 .. method:: post([$index = NULL[, $xss_clean = FALSE]])
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
123 filter. It's enabled by setting the second parameter to boolean TRUE.
124 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500125
Andrey Andreev04535c72014-01-06 10:57:05 +0200126 $this->input->post('some_data', TRUE);
Derek Jones8ede1a22011-10-05 13:34:52 -0500127
Andrey Andreev04535c72014-01-06 10:57:05 +0200128 To return an array of all POST items call without any parameters.
Derek Jones8ede1a22011-10-05 13:34:52 -0500129
Andrey Andreev04535c72014-01-06 10:57:05 +0200130 To return all POST items and pass them through the XSS filter set the
131 first parameter NULL while setting the second parameter to boolean TRUE.
132 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500133
Andrey Andreev04535c72014-01-06 10:57:05 +0200134 $this->input->post(NULL, TRUE); // returns all POST items with XSS filter
135 $this->input->post(); // returns all POST items without XSS filter
Derek Jones8ede1a22011-10-05 13:34:52 -0500136
Andrey Andreev04535c72014-01-06 10:57:05 +0200137 .. method:: get([$index = NULL[, $xss_clean = FALSE]])
Derek Jones8ede1a22011-10-05 13:34:52 -0500138
Andrey Andreev04535c72014-01-06 10:57:05 +0200139 :param string $index: GET parameter name
140 :param bool $xss_clean: Whether to apply XSS filtering
141 :returns: mixed
Derek Jones8ede1a22011-10-05 13:34:52 -0500142
Andrey Andreev04535c72014-01-06 10:57:05 +0200143 This method is identical to ``post()``, only it fetches GET data.
144 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500145
Andrey Andreev04535c72014-01-06 10:57:05 +0200146 $this->input->get('some_data', TRUE);
147
148 To return an array of all GET items call without any parameters.
149
150 To return all GET items and pass them through the XSS filter set the
151 first parameter NULL while setting the second parameter to boolean TRUE.
152 ::
153
154 $this->input->get(NULL, TRUE); // returns all GET items with XSS filter
155 $this->input->get(); // returns all GET items without XSS filtering
156
157 .. method:: get_post([$index = ''[, $xss_clean = FALSE]])
158
159 :param string $index: GET/POST parameter name
160 :param bool $xss_clean: Whether to apply XSS filtering
161 :returns: mixed
162
163 This method works the same way as ``post()`` and ``get()``, only combined.
164 It will search through both POST and GET streams for data, looking first
165 in POST, and then in GET::
166
167 $this->input->get_post('some_data', TRUE);
168
169 .. method:: cookie([$index = ''[, $xss_clean = FALSE]])
170
171 :param string $index: COOKIE parameter name
172 :param bool $xss_clean: Whether to apply XSS filtering
173 :returns: mixed
174
175 This method is identical to ``post()`` and ``get()``, only it fetches cookie
176 data::
177
178 $this->input->cookie('some_cookie');
179 $this->input->cookie('some_cookie, TRUE); // with XSS filter
180
181 .. method:: server([$index = ''[, $xss_clean = FALSE]])
182
183 :param string $index: Value name
184 :param bool $xss_clean: Whether to apply XSS filtering
185 :returns: mixed
186
187 This method is identical to the ``post()``, ``get()`` and ``cookie()`` methods,
188 only it fetches server data (``$_SERVER``)::
189
190 $this->input->server('some_data');
191
192 .. method:: input_stream([$index = ''[, $xss_clean = FALSE]])
193
194 :param string $index: Key name
195 :param bool $xss_clean: Whether to apply XSS filtering
196 :returns: mixed
197
198 This method is identical to ``get()``, ``post()`` and ``cookie()``,
199 only it fetches the *php://input* stream data.
200
201 .. method:: set_cookie($name = ''[, $value = ''[, $expire = ''[, $domain = ''[, $path = '/'[, $prefix = ''[, $secure = FALSE[, $httponly = FALSE]]]]]]])
202
203 :param mixed $name: Cookie name or an array of parameters
204 :param string $value: Cookie value
205 :param int $expire: Cookie expiration time in seconds
206 :param string $domain: Cookie domain
207 :param string $path: Cookie path
208 :param string $prefix: Cookie name prefix
209 :param bool $secure: Whether to only transfer the cookie through HTTPS
210 :param bool $httponly: Whether to only make the cookie accessible for HTTP requests (no JavaScript)
211 :returns: void
212
213 Sets a cookie containing the values you specify. There are two ways to
214 pass information to this method so that a cookie can be set: Array
215 Method, and Discrete Parameters:
216
217 Array Method
218 ^^^^^^^^^^^^
219
220 Using this method, an associative array is passed to the first
221 parameter::
222
223 $cookie = array(
224 'name' => 'The Cookie Name',
225 'value' => 'The Value',
226 'expire' => '86500',
227 'domain' => '.some-domain.com',
228 'path' => '/',
229 'prefix' => 'myprefix_',
230 'secure' => TRUE
231 );
232
233 $this->input->set_cookie($cookie);
234
235 **Notes:**
236
237 Only the name and value are required. To delete a cookie set it with the
238 expiration blank.
239
240 The expiration is set in **seconds**, which will be added to the current
241 time. Do not include the time, but rather only the number of seconds
242 from *now* that you wish the cookie to be valid. If the expiration is
243 set to zero the cookie will only last as long as the browser is open.
244
245 For site-wide cookies regardless of how your site is requested, add your
246 URL to the **domain** starting with a period, like this:
247 .your-domain.com
248
249 The path is usually not needed since the method sets a root path.
250
251 The prefix is only needed if you need to avoid name collisions with
252 other identically named cookies for your server.
253
254 The secure boolean is only needed if you want to make it a secure cookie
255 by setting it to TRUE.
256
257 Discrete Parameters
258 ^^^^^^^^^^^^^^^^^^^
259
260 If you prefer, you can set the cookie by passing data using individual
261 parameters::
262
263 $this->input->set_cookie($name, $value, $expire, $domain, $path, $prefix, $secure);
Derek Jones8ede1a22011-10-05 13:34:52 -0500264
Andrey Andreev303eef02012-11-06 14:55:48 +0200265
Andrey Andreev04535c72014-01-06 10:57:05 +0200266 .. method:: ip_address()
Derek Jones8ede1a22011-10-05 13:34:52 -0500267
Andrey Andreev04535c72014-01-06 10:57:05 +0200268 :returns: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500269
Andrey Andreev04535c72014-01-06 10:57:05 +0200270 Returns the IP address for the current user. If the IP address is not
271 valid, the method will return '0.0.0.0'::
Derek Jones8ede1a22011-10-05 13:34:52 -0500272
Andrey Andreev04535c72014-01-06 10:57:05 +0200273 echo $this->input->ip_address();
Derek Jones8ede1a22011-10-05 13:34:52 -0500274
Andrey Andreev04535c72014-01-06 10:57:05 +0200275 .. important:: This method takes into account the ``$config['proxy_ips']``
276 setting and will return the reported HTTP_X_FORWARDED_FOR,
277 HTTP_CLIENT_IP, HTTP_X_CLIENT_IP or HTTP_X_CLUSTER_CLIENT_IP
278 address for the allowed IP addresses.
Derek Jones8ede1a22011-10-05 13:34:52 -0500279
Andrey Andreev04535c72014-01-06 10:57:05 +0200280 .. method:: valid_ip($ip[, $which = ''])
Andrey Andreev303eef02012-11-06 14:55:48 +0200281
Andrey Andreev04535c72014-01-06 10:57:05 +0200282 :param string $ip: IP address
283 :param string $which: IP protocol ('ipv4' or 'ipv6')
284 :returns: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500285
Andrey Andreev04535c72014-01-06 10:57:05 +0200286 Takes an IP address as input and returns TRUE or FALSE (boolean) depending
287 on whether it is valid or not.
Derek Jones8ede1a22011-10-05 13:34:52 -0500288
Andrey Andreev04535c72014-01-06 10:57:05 +0200289 .. note:: The $this->input->ip_address() method above automatically
290 validates the IP address.
Derek Jones8ede1a22011-10-05 13:34:52 -0500291
Andrey Andreev04535c72014-01-06 10:57:05 +0200292 ::
Andrey Andreev5a257182012-06-10 06:18:14 +0300293
Andrey Andreev04535c72014-01-06 10:57:05 +0200294 if ( ! $this->input->valid_ip($ip))
295 {
296 echo 'Not Valid';
297 }
298 else
299 {
300 echo 'Valid';
301 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500302
Andrey Andreev04535c72014-01-06 10:57:05 +0200303 Accepts an optional second string parameter of 'ipv4' or 'ipv6' to specify
304 an IP format. The default checks for both formats.
Derek Jones8ede1a22011-10-05 13:34:52 -0500305
Andrey Andreev04535c72014-01-06 10:57:05 +0200306 .. method:: user_agent()
Derek Jones8ede1a22011-10-05 13:34:52 -0500307
Andrey Andreev04535c72014-01-06 10:57:05 +0200308 :returns: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500309
Andrey Andreev04535c72014-01-06 10:57:05 +0200310 Returns the user agent string (web browser) being used by the current user,
311 or NULL if it's not available.
312 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500313
Andrey Andreev04535c72014-01-06 10:57:05 +0200314 echo $this->input->user_agent();
Derek Jones8ede1a22011-10-05 13:34:52 -0500315
Andrey Andreev04535c72014-01-06 10:57:05 +0200316 See the :doc:`User Agent Class <user_agent>` for methods which extract
317 information from the user agent string.
Derek Jones8ede1a22011-10-05 13:34:52 -0500318
Andrey Andreev04535c72014-01-06 10:57:05 +0200319 .. method:: request_headers([$xss_clean = FALSE])
Derek Jones8ede1a22011-10-05 13:34:52 -0500320
Andrey Andreev04535c72014-01-06 10:57:05 +0200321 :param bool $xss_clean: Whether to apply XSS filtering
322 :returns: array
Derek Jones8ede1a22011-10-05 13:34:52 -0500323
Andrey Andreev04535c72014-01-06 10:57:05 +0200324 Returns an array of HTTP request headers.
325 Useful if running in a non-Apache environment where
326 `apache_request_headers() <http://php.net/apache_request_headers>`_
327 will not be supported.
328 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500329
Andrey Andreev04535c72014-01-06 10:57:05 +0200330 $headers = $this->input->request_headers();
Derek Jones8ede1a22011-10-05 13:34:52 -0500331
Andrey Andreev04535c72014-01-06 10:57:05 +0200332 .. method:: get_request_header($index[, $xss_clean = FALSE])
Derek Jones8ede1a22011-10-05 13:34:52 -0500333
Andrey Andreev04535c72014-01-06 10:57:05 +0200334 :param string $index: HTTP request header name
335 :param bool $xss_clean: Whether to apply XSS filtering
336 :returns: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500337
Andrey Andreev04535c72014-01-06 10:57:05 +0200338 Returns a single member of the request headers array or NULL
339 if the searched header is not found.
340 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500341
Andrey Andreev04535c72014-01-06 10:57:05 +0200342 $this->input->get_request_header('some-header', TRUE);
Derek Jones8ede1a22011-10-05 13:34:52 -0500343
Andrey Andreev04535c72014-01-06 10:57:05 +0200344 .. method:: is_ajax_request()
Derek Jones8ede1a22011-10-05 13:34:52 -0500345
Andrey Andreev04535c72014-01-06 10:57:05 +0200346 :returns: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500347
Andrey Andreev04535c72014-01-06 10:57:05 +0200348 Checks to see if the HTTP_X_REQUESTED_WITH server header has been
349 set, and returns boolean TRUE if it is or FALSE if not.
Derek Jones8ede1a22011-10-05 13:34:52 -0500350
Andrey Andreev04535c72014-01-06 10:57:05 +0200351 .. method:: is_cli_request()
Derek Jones8ede1a22011-10-05 13:34:52 -0500352
Andrey Andreev04535c72014-01-06 10:57:05 +0200353 :returns: bool
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100354
Andrey Andreev04535c72014-01-06 10:57:05 +0200355 Checks to see if the application was run from the command-line
356 interface.
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100357
Andrey Andreev04535c72014-01-06 10:57:05 +0200358 .. note:: This method checks both the PHP SAPI name currently in use
359 and if the ``STDIN`` constant is defined, which is usually a
360 failsafe way to see if PHP is being run via the command line.
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100361
Andrey Andreev04535c72014-01-06 10:57:05 +0200362 ::
363
364 $this->input->is_cli_request()
365
366 .. method:: method([$upper = FALSE])
367
368 :param bool $upper: Whether to return the request method name in upper or lower case
369 :returns: string
370
371 Returns the ``$_SERVER['REQUEST_METHOD']``, with the option to set it
372 in uppercase or lowercase.
373 ::
374
375 echo $this->input->method(TRUE); // Outputs: POST
376 echo $this->input->method(FALSE); // Outputs: post
377 echo $this->input->method(); // Outputs: post