blob: f5ab048834a28f3b0d8a3eb88e21b99c0385a748 [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
Connor Tumbleson75b3fb22014-01-11 06:58:43 -0600218 **Array Method**
Andrey Andreev04535c72014-01-06 10:57:05 +0200219
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
Connor Tumbleson75b3fb22014-01-11 06:58:43 -0600257 **Discrete Parameters**
Andrey Andreev04535c72014-01-06 10:57:05 +0200258
259 If you prefer, you can set the cookie by passing data using individual
260 parameters::
261
262 $this->input->set_cookie($name, $value, $expire, $domain, $path, $prefix, $secure);
Derek Jones8ede1a22011-10-05 13:34:52 -0500263
Andrey Andreev303eef02012-11-06 14:55:48 +0200264
Andrey Andreev04535c72014-01-06 10:57:05 +0200265 .. method:: ip_address()
Derek Jones8ede1a22011-10-05 13:34:52 -0500266
Andrey Andreev04535c72014-01-06 10:57:05 +0200267 :returns: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500268
Andrey Andreev04535c72014-01-06 10:57:05 +0200269 Returns the IP address for the current user. If the IP address is not
270 valid, the method will return '0.0.0.0'::
Derek Jones8ede1a22011-10-05 13:34:52 -0500271
Andrey Andreev04535c72014-01-06 10:57:05 +0200272 echo $this->input->ip_address();
Derek Jones8ede1a22011-10-05 13:34:52 -0500273
Andrey Andreev04535c72014-01-06 10:57:05 +0200274 .. important:: This method takes into account the ``$config['proxy_ips']``
275 setting and will return the reported HTTP_X_FORWARDED_FOR,
276 HTTP_CLIENT_IP, HTTP_X_CLIENT_IP or HTTP_X_CLUSTER_CLIENT_IP
277 address for the allowed IP addresses.
Derek Jones8ede1a22011-10-05 13:34:52 -0500278
Andrey Andreev04535c72014-01-06 10:57:05 +0200279 .. method:: valid_ip($ip[, $which = ''])
Andrey Andreev303eef02012-11-06 14:55:48 +0200280
Andrey Andreev04535c72014-01-06 10:57:05 +0200281 :param string $ip: IP address
282 :param string $which: IP protocol ('ipv4' or 'ipv6')
283 :returns: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500284
Andrey Andreev04535c72014-01-06 10:57:05 +0200285 Takes an IP address as input and returns TRUE or FALSE (boolean) depending
286 on whether it is valid or not.
Derek Jones8ede1a22011-10-05 13:34:52 -0500287
Andrey Andreev04535c72014-01-06 10:57:05 +0200288 .. note:: The $this->input->ip_address() method above automatically
289 validates the IP address.
Derek Jones8ede1a22011-10-05 13:34:52 -0500290
Andrey Andreev04535c72014-01-06 10:57:05 +0200291 ::
Andrey Andreev5a257182012-06-10 06:18:14 +0300292
Andrey Andreev04535c72014-01-06 10:57:05 +0200293 if ( ! $this->input->valid_ip($ip))
294 {
295 echo 'Not Valid';
296 }
297 else
298 {
299 echo 'Valid';
300 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500301
Andrey Andreev04535c72014-01-06 10:57:05 +0200302 Accepts an optional second string parameter of 'ipv4' or 'ipv6' to specify
303 an IP format. The default checks for both formats.
Derek Jones8ede1a22011-10-05 13:34:52 -0500304
Andrey Andreev04535c72014-01-06 10:57:05 +0200305 .. method:: user_agent()
Derek Jones8ede1a22011-10-05 13:34:52 -0500306
Andrey Andreev04535c72014-01-06 10:57:05 +0200307 :returns: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500308
Andrey Andreev04535c72014-01-06 10:57:05 +0200309 Returns the user agent string (web browser) being used by the current user,
310 or NULL if it's not available.
311 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500312
Andrey Andreev04535c72014-01-06 10:57:05 +0200313 echo $this->input->user_agent();
Derek Jones8ede1a22011-10-05 13:34:52 -0500314
Andrey Andreev04535c72014-01-06 10:57:05 +0200315 See the :doc:`User Agent Class <user_agent>` for methods which extract
316 information from the user agent string.
Derek Jones8ede1a22011-10-05 13:34:52 -0500317
Andrey Andreev04535c72014-01-06 10:57:05 +0200318 .. method:: request_headers([$xss_clean = FALSE])
Derek Jones8ede1a22011-10-05 13:34:52 -0500319
Andrey Andreev04535c72014-01-06 10:57:05 +0200320 :param bool $xss_clean: Whether to apply XSS filtering
321 :returns: array
Derek Jones8ede1a22011-10-05 13:34:52 -0500322
Andrey Andreev04535c72014-01-06 10:57:05 +0200323 Returns an array of HTTP request headers.
324 Useful if running in a non-Apache environment where
325 `apache_request_headers() <http://php.net/apache_request_headers>`_
326 will not be supported.
327 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500328
Andrey Andreev04535c72014-01-06 10:57:05 +0200329 $headers = $this->input->request_headers();
Derek Jones8ede1a22011-10-05 13:34:52 -0500330
Andrey Andreev04535c72014-01-06 10:57:05 +0200331 .. method:: get_request_header($index[, $xss_clean = FALSE])
Derek Jones8ede1a22011-10-05 13:34:52 -0500332
Andrey Andreev04535c72014-01-06 10:57:05 +0200333 :param string $index: HTTP request header name
334 :param bool $xss_clean: Whether to apply XSS filtering
335 :returns: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500336
Andrey Andreev04535c72014-01-06 10:57:05 +0200337 Returns a single member of the request headers array or NULL
338 if the searched header is not found.
339 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500340
Andrey Andreev04535c72014-01-06 10:57:05 +0200341 $this->input->get_request_header('some-header', TRUE);
Derek Jones8ede1a22011-10-05 13:34:52 -0500342
Andrey Andreev04535c72014-01-06 10:57:05 +0200343 .. method:: is_ajax_request()
Derek Jones8ede1a22011-10-05 13:34:52 -0500344
Andrey Andreev04535c72014-01-06 10:57:05 +0200345 :returns: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500346
Andrey Andreev04535c72014-01-06 10:57:05 +0200347 Checks to see if the HTTP_X_REQUESTED_WITH server header has been
348 set, and returns boolean TRUE if it is or FALSE if not.
Derek Jones8ede1a22011-10-05 13:34:52 -0500349
Andrey Andreev04535c72014-01-06 10:57:05 +0200350 .. method:: is_cli_request()
Derek Jones8ede1a22011-10-05 13:34:52 -0500351
Andrey Andreev04535c72014-01-06 10:57:05 +0200352 :returns: bool
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100353
Andrey Andreev04535c72014-01-06 10:57:05 +0200354 Checks to see if the application was run from the command-line
355 interface.
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100356
Andrey Andreev04535c72014-01-06 10:57:05 +0200357 .. note:: This method checks both the PHP SAPI name currently in use
358 and if the ``STDIN`` constant is defined, which is usually a
359 failsafe way to see if PHP is being run via the command line.
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100360
Andrey Andreev04535c72014-01-06 10:57:05 +0200361 ::
362
363 $this->input->is_cli_request()
364
365 .. method:: method([$upper = FALSE])
366
367 :param bool $upper: Whether to return the request method name in upper or lower case
368 :returns: string
369
370 Returns the ``$_SERVER['REQUEST_METHOD']``, with the option to set it
371 in uppercase or lowercase.
372 ::
373
374 echo $this->input->method(TRUE); // Outputs: POST
375 echo $this->input->method(FALSE); // Outputs: post
376 echo $this->input->method(); // Outputs: post