blob: 967f69d13345d7570a03456cdc0a4088dcdcc8d5 [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
James L Parry08191be2014-12-20 02:37:13 -080020***************
21Input Filtering
22***************
23
Derek Jones8ede1a22011-10-05 13:34:52 -050024Security Filtering
25==================
26
Andrey Andreev303eef02012-11-06 14:55:48 +020027The security filtering method is called automatically when a new
Derek Jones8ede1a22011-10-05 13:34:52 -050028:doc:`controller <../general/controllers>` is invoked. It does the
29following:
30
Andrey Andreev04535c72014-01-06 10:57:05 +020031- If ``$config['allow_get_array']`` is FALSE (default is TRUE), destroys
Derek Jones8ede1a22011-10-05 13:34:52 -050032 the global GET array.
33- Destroys all global variables in the event register_globals is
34 turned on.
35- Filters the GET/POST/COOKIE array keys, permitting only alpha-numeric
36 (and a few other) characters.
37- Provides XSS (Cross-site Scripting Hacks) filtering. This can be
38 enabled globally, or upon request.
Andrey Andreevbfb635b2014-01-08 18:32:05 +020039- Standardizes newline characters to ``PHP_EOL`` (\\n in UNIX-based OSes,
40 \\r\\n under Windows). This is configurable.
Derek Jones8ede1a22011-10-05 13:34:52 -050041
42XSS Filtering
43=============
44
45The Input class has the ability to filter input automatically to prevent
46cross-site scripting attacks. If you want the filter to run
47automatically every time it encounters POST or COOKIE data you can
Andrey Andreev04535c72014-01-06 10:57:05 +020048enable it by opening your *application/config/config.php* file and setting
Derek Jones8ede1a22011-10-05 13:34:52 -050049this::
50
51 $config['global_xss_filtering'] = TRUE;
52
53Please refer to the :doc:`Security class <security>` documentation for
54information on using XSS Filtering in your application.
55
James L Parry08191be2014-12-20 02:37:13 -080056*******************
57Accessing form data
58*******************
59
Andrey Andreev9cf12d12012-06-13 10:19:59 +030060Using POST, GET, COOKIE, or SERVER Data
61=======================================
Derek Jones8ede1a22011-10-05 13:34:52 -050062
Andrey Andreev04535c72014-01-06 10:57:05 +020063CodeIgniter comes with helper methods that let you fetch POST, GET,
Derek Jones8ede1a22011-10-05 13:34:52 -050064COOKIE or SERVER items. The main advantage of using the provided
Andrey Andreev303eef02012-11-06 14:55:48 +020065methods rather than fetching an item directly (``$_POST['something']``)
Andrey Andreev9cf12d12012-06-13 10:19:59 +030066is that the methods will check to see if the item is set and return
67NULL if not. This lets you conveniently use data without
Derek Jones8ede1a22011-10-05 13:34:52 -050068having to test whether an item exists first. In other words, normally
69you might do something like this::
70
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +010071 $something = isset($_POST['something']) ? $_POST['something'] : NULL;
Derek Jones8ede1a22011-10-05 13:34:52 -050072
Andrey Andreev303eef02012-11-06 14:55:48 +020073With CodeIgniter's built in methods you can simply do this::
Derek Jones8ede1a22011-10-05 13:34:52 -050074
75 $something = $this->input->post('something');
76
Andrey Andreev04535c72014-01-06 10:57:05 +020077The main methods are:
Derek Jones8ede1a22011-10-05 13:34:52 -050078
Andrey Andreev28c2c972014-02-08 04:27:48 +020079- ``$this->input->post()``
80- ``$this->input->get()``
81- ``$this->input->cookie()``
82- ``$this->input->server()``
Derek Jones8ede1a22011-10-05 13:34:52 -050083
Andrey Andreev303eef02012-11-06 14:55:48 +020084Using the php://input stream
85============================
86
87If you want to utilize the PUT, DELETE, PATCH or other exotic request
88methods, they can only be accessed via a special input stream, that
89can only be read once. This isn't as easy as just reading from e.g.
90the ``$_POST`` array, because it will always exist and you can try
91and access multiple variables without caring that you might only have
92one shot at all of the POST data.
93
94CodeIgniter will take care of that for you, and you can access data
95from the **php://input** stream at any time, just by calling the
96``input_stream()`` method::
97
98 $this->input->input_stream('key');
99
Andrey Andreev04535c72014-01-06 10:57:05 +0200100Similar to other methods such as ``get()`` and ``post()``, if the
101requested data is not found, it will return NULL and you can also
102decide whether to run the data through ``xss_clean()`` by passing
103a boolean value as the second parameter::
Andrey Andreev303eef02012-11-06 14:55:48 +0200104
105 $this->input->input_stream('key', TRUE); // XSS Clean
106 $this->input->input_stream('key', FALSE); // No XSS filter
107
Andrey Andreev04535c72014-01-06 10:57:05 +0200108.. note:: You can utilize ``method()`` in order to know if you're reading
Andrey Andreev303eef02012-11-06 14:55:48 +0200109 PUT, DELETE or PATCH data.
110
Andrey Andreev04535c72014-01-06 10:57:05 +0200111***************
112Class Reference
113***************
Derek Jones8ede1a22011-10-05 13:34:52 -0500114
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200115.. php:class:: CI_Input
Derek Jones8ede1a22011-10-05 13:34:52 -0500116
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200117 .. php:method:: post([$index = NULL[, $xss_clean = NULL]])
Derek Jones8ede1a22011-10-05 13:34:52 -0500118
Ahmad Anbarff89a4e2014-12-02 17:26:30 +0200119 :param mixed $index: POST parameter name
Andrey Andreev28c2c972014-02-08 04:27:48 +0200120 :param bool $xss_clean: Whether to apply XSS filtering
121 :returns: $_POST if no parameters supplied, otherwise the POST value if found or NULL if not
122 :rtype: mixed
Derek Jones8ede1a22011-10-05 13:34:52 -0500123
Andrey Andreev04535c72014-01-06 10:57:05 +0200124 The first parameter will contain the name of the POST item you are
125 looking for::
Derek Jones8831d112011-10-05 15:57:02 -0500126
Andrey Andreev04535c72014-01-06 10:57:05 +0200127 $this->input->post('some_data');
Derek Jones8ede1a22011-10-05 13:34:52 -0500128
Andrey Andreev04535c72014-01-06 10:57:05 +0200129 The method returns NULL if the item you are attempting to retrieve
130 does not exist.
Derek Jones8ede1a22011-10-05 13:34:52 -0500131
Andrey Andreev04535c72014-01-06 10:57:05 +0200132 The second optional parameter lets you run the data through the XSS
Andrey Andreev88ebdf72014-01-08 17:28:02 +0200133 filter. It's enabled by setting the second parameter to boolean TRUE
134 or by setting your ``$config['global_xss_filtering']`` to TRUE.
Andrey Andreev04535c72014-01-06 10:57:05 +0200135 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500136
Andrey Andreev04535c72014-01-06 10:57:05 +0200137 $this->input->post('some_data', TRUE);
Derek Jones8ede1a22011-10-05 13:34:52 -0500138
Andrey Andreev04535c72014-01-06 10:57:05 +0200139 To return an array of all POST items call without any parameters.
Derek Jones8ede1a22011-10-05 13:34:52 -0500140
Andrey Andreev04535c72014-01-06 10:57:05 +0200141 To return all POST items and pass them through the XSS filter set the
142 first parameter NULL while setting the second parameter to boolean TRUE.
143 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500144
Andrey Andreev04535c72014-01-06 10:57:05 +0200145 $this->input->post(NULL, TRUE); // returns all POST items with XSS filter
Andrey Andreev88ebdf72014-01-08 17:28:02 +0200146 $this->input->post(NULL, FALSE); // returns all POST items without XSS filter
David Wosnitzad31a4e62014-12-12 16:35:35 +0100147
Ahmad Anbarff89a4e2014-12-02 17:26:30 +0200148 To return an array of multiple POST parameters, pass all the required keys
149 as an array.
150 ::
David Wosnitzad31a4e62014-12-12 16:35:35 +0100151
Ahmad Anbarff89a4e2014-12-02 17:26:30 +0200152 $this->input->post(array('field1', 'field2'));
David Wosnitzad31a4e62014-12-12 16:35:35 +0100153
Ahmad Anbarff89a4e2014-12-02 17:26:30 +0200154 Same rule applied here, to retrive the parameters with XSS filtering enabled, set the
155 second parameter to boolean TRUE.
156 ::
David Wosnitzad31a4e62014-12-12 16:35:35 +0100157
Ahmad Anbarff89a4e2014-12-02 17:26:30 +0200158 $this->input->post(array('field1', 'field2'), TRUE);
Derek Jones8ede1a22011-10-05 13:34:52 -0500159
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200160 .. php:method:: get([$index = NULL[, $xss_clean = NULL]])
Derek Jones8ede1a22011-10-05 13:34:52 -0500161
Ahmad Anbarff89a4e2014-12-02 17:26:30 +0200162 :param mixed $index: GET parameter name
Andrey Andreev28c2c972014-02-08 04:27:48 +0200163 :param bool $xss_clean: Whether to apply XSS filtering
164 :returns: $_GET if no parameters supplied, otherwise the GET value if found or NULL if not
165 :rtype: mixed
Derek Jones8ede1a22011-10-05 13:34:52 -0500166
Andrey Andreev04535c72014-01-06 10:57:05 +0200167 This method is identical to ``post()``, only it fetches GET data.
168 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500169
Andrey Andreev04535c72014-01-06 10:57:05 +0200170 $this->input->get('some_data', TRUE);
171
172 To return an array of all GET items call without any parameters.
173
174 To return all GET items and pass them through the XSS filter set the
175 first parameter NULL while setting the second parameter to boolean TRUE.
176 ::
177
178 $this->input->get(NULL, TRUE); // returns all GET items with XSS filter
Andrey Andreev88ebdf72014-01-08 17:28:02 +0200179 $this->input->get(NULL, FALSE); // returns all GET items without XSS filtering
David Wosnitzad31a4e62014-12-12 16:35:35 +0100180
Ahmad Anbarff89a4e2014-12-02 17:26:30 +0200181 To return an array of multiple GET parameters, pass all the required keys
182 as an array.
183 ::
David Wosnitzad31a4e62014-12-12 16:35:35 +0100184
Ahmad Anbarff89a4e2014-12-02 17:26:30 +0200185 $this->input->get(array('field1', 'field2'));
David Wosnitzad31a4e62014-12-12 16:35:35 +0100186
Ahmad Anbarff89a4e2014-12-02 17:26:30 +0200187 Same rule applied here, to retrive the parameters with XSS filtering enabled, set the
188 second parameter to boolean TRUE.
189 ::
David Wosnitzad31a4e62014-12-12 16:35:35 +0100190
Ahmad Anbarff89a4e2014-12-02 17:26:30 +0200191 $this->input->get(array('field1', 'field2'), TRUE);
Andrey Andreev04535c72014-01-06 10:57:05 +0200192
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200193 .. php:method:: post_get($index[, $xss_clean = NULL])
Andrey Andreevea801ab2014-01-20 15:03:43 +0200194
Andrey Andreev28c2c972014-02-08 04:27:48 +0200195 :param string $index: POST/GET parameter name
196 :param bool $xss_clean: Whether to apply XSS filtering
197 :returns: POST/GET value if found, NULL if not
198 :rtype: mixed
Andrey Andreevea801ab2014-01-20 15:03:43 +0200199
Andrey Andreev7c60b122014-02-08 18:47:19 +0200200 This method works pretty much the same way as ``post()`` and ``get()``,
201 only combined. It will search through both POST and GET streams for data,
202 looking in POST first, and then in GET::
Andrey Andreevea801ab2014-01-20 15:03:43 +0200203
204 $this->input->post_get('some_data', TRUE);
205
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200206 .. php:method:: get_post($index[, $xss_clean = NULL])
Andrey Andreev04535c72014-01-06 10:57:05 +0200207
Andrey Andreev28c2c972014-02-08 04:27:48 +0200208 :param string $index: GET/POST parameter name
209 :param bool $xss_clean: Whether to apply XSS filtering
210 :returns: GET/POST value if found, NULL if not
211 :rtype: mixed
Andrey Andreev04535c72014-01-06 10:57:05 +0200212
Andrey Andreevea801ab2014-01-20 15:03:43 +0200213 This method works the same way as ``post_get()`` only it looks for GET
214 data first.
Andrey Andreev04535c72014-01-06 10:57:05 +0200215
216 $this->input->get_post('some_data', TRUE);
217
Andrey Andreevea801ab2014-01-20 15:03:43 +0200218 .. note:: This method used to act EXACTLY like ``post_get()``, but it's
219 behavior has changed in CodeIgniter 3.0.
220
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200221 .. php:method:: cookie([$index = NULL[, $xss_clean = NULL]])
Andrey Andreev04535c72014-01-06 10:57:05 +0200222
Andrey Andreevef29f832014-12-02 18:03:47 +0200223 :param mixed $index: COOKIE name
Andrey Andreev28c2c972014-02-08 04:27:48 +0200224 :param bool $xss_clean: Whether to apply XSS filtering
225 :returns: $_COOKIE if no parameters supplied, otherwise the COOKIE value if found or NULL if not
226 :rtype: mixed
Andrey Andreev04535c72014-01-06 10:57:05 +0200227
228 This method is identical to ``post()`` and ``get()``, only it fetches cookie
229 data::
230
231 $this->input->cookie('some_cookie');
232 $this->input->cookie('some_cookie, TRUE); // with XSS filter
David Wosnitzad31a4e62014-12-12 16:35:35 +0100233
Andrey Andreevef29f832014-12-02 18:03:47 +0200234 To return an array of multiple cookie values, pass all the required keys
Ahmad Anbarff89a4e2014-12-02 17:26:30 +0200235 as an array.
236 ::
David Wosnitzad31a4e62014-12-12 16:35:35 +0100237
Ahmad Anbarff89a4e2014-12-02 17:26:30 +0200238 $this->input->cookie(array('some_cookie', 'some_cookie2'));
Andrey Andreev04535c72014-01-06 10:57:05 +0200239
Andrey Andreevb4834cc2015-01-29 13:52:44 +0200240 .. note:: Unlike the :doc:`Cookie Helper <../helpers/cookie_helper>`
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200241 function :php:func:`get_cookie()`, this method does NOT prepend
Andrey Andreevb4834cc2015-01-29 13:52:44 +0200242 your configured ``$config['cookie_prefix']`` value.
243
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200244 .. php:method:: server($index[, $xss_clean = NULL])
Andrey Andreev04535c72014-01-06 10:57:05 +0200245
Ahmad Anbarff89a4e2014-12-02 17:26:30 +0200246 :param mixed $index: Value name
Andrey Andreev28c2c972014-02-08 04:27:48 +0200247 :param bool $xss_clean: Whether to apply XSS filtering
248 :returns: $_SERVER item value if found, NULL if not
249 :rtype: mixed
Andrey Andreev04535c72014-01-06 10:57:05 +0200250
Andrey Andreev7c60b122014-02-08 18:47:19 +0200251 This method is identical to the ``post()``, ``get()`` and ``cookie()``
252 methods, only it fetches server data (``$_SERVER``)::
Andrey Andreev04535c72014-01-06 10:57:05 +0200253
254 $this->input->server('some_data');
255
Andrey Andreevef29f832014-12-02 18:03:47 +0200256 To return an array of multiple ``$_SERVER`` values, pass all the required keys
Ahmad Anbarff89a4e2014-12-02 17:26:30 +0200257 as an array.
258 ::
David Wosnitzad31a4e62014-12-12 16:35:35 +0100259
260 $this->input->server(array('SERVER_PROTOCOL', 'REQUEST_URI'));
Ahmad Anbarff89a4e2014-12-02 17:26:30 +0200261
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200262 .. php:method:: input_stream([$index = NULL[, $xss_clean = NULL]])
Andrey Andreev04535c72014-01-06 10:57:05 +0200263
Ahmad Anbarff89a4e2014-12-02 17:26:30 +0200264 :param mixed $index: Key name
Andrey Andreev28c2c972014-02-08 04:27:48 +0200265 :param bool $xss_clean: Whether to apply XSS filtering
266 :returns: Input stream array if no parameters supplied, otherwise the specified value if found or NULL if not
267 :rtype: mixed
Andrey Andreev04535c72014-01-06 10:57:05 +0200268
269 This method is identical to ``get()``, ``post()`` and ``cookie()``,
270 only it fetches the *php://input* stream data.
271
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200272 .. php:method:: set_cookie($name = ''[, $value = ''[, $expire = ''[, $domain = ''[, $path = '/'[, $prefix = ''[, $secure = FALSE[, $httponly = FALSE]]]]]]])
Andrey Andreev04535c72014-01-06 10:57:05 +0200273
Andrey Andreev28c2c972014-02-08 04:27:48 +0200274 :param mixed $name: Cookie name or an array of parameters
275 :param string $value: Cookie value
276 :param int $expire: Cookie expiration time in seconds
277 :param string $domain: Cookie domain
278 :param string $path: Cookie path
279 :param string $prefix: Cookie name prefix
280 :param bool $secure: Whether to only transfer the cookie through HTTPS
281 :param bool $httponly: Whether to only make the cookie accessible for HTTP requests (no JavaScript)
282 :rtype: void
283
Andrey Andreev04535c72014-01-06 10:57:05 +0200284
285 Sets a cookie containing the values you specify. There are two ways to
286 pass information to this method so that a cookie can be set: Array
287 Method, and Discrete Parameters:
288
Connor Tumbleson75b3fb22014-01-11 06:58:43 -0600289 **Array Method**
Andrey Andreev04535c72014-01-06 10:57:05 +0200290
291 Using this method, an associative array is passed to the first
292 parameter::
293
294 $cookie = array(
295 'name' => 'The Cookie Name',
296 'value' => 'The Value',
297 'expire' => '86500',
298 'domain' => '.some-domain.com',
299 'path' => '/',
300 'prefix' => 'myprefix_',
301 'secure' => TRUE
302 );
303
304 $this->input->set_cookie($cookie);
305
Andrey Andreev28c2c972014-02-08 04:27:48 +0200306 **Notes**
Andrey Andreev04535c72014-01-06 10:57:05 +0200307
308 Only the name and value are required. To delete a cookie set it with the
309 expiration blank.
310
311 The expiration is set in **seconds**, which will be added to the current
312 time. Do not include the time, but rather only the number of seconds
313 from *now* that you wish the cookie to be valid. If the expiration is
314 set to zero the cookie will only last as long as the browser is open.
315
316 For site-wide cookies regardless of how your site is requested, add your
317 URL to the **domain** starting with a period, like this:
318 .your-domain.com
319
320 The path is usually not needed since the method sets a root path.
321
322 The prefix is only needed if you need to avoid name collisions with
323 other identically named cookies for your server.
324
325 The secure boolean is only needed if you want to make it a secure cookie
326 by setting it to TRUE.
327
Connor Tumbleson75b3fb22014-01-11 06:58:43 -0600328 **Discrete Parameters**
Andrey Andreev04535c72014-01-06 10:57:05 +0200329
330 If you prefer, you can set the cookie by passing data using individual
331 parameters::
332
333 $this->input->set_cookie($name, $value, $expire, $domain, $path, $prefix, $secure);
Derek Jones8ede1a22011-10-05 13:34:52 -0500334
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200335 .. php:method:: ip_address()
Derek Jones8ede1a22011-10-05 13:34:52 -0500336
Andrey Andreev28c2c972014-02-08 04:27:48 +0200337 :returns: Visitor's IP address or '0.0.0.0' if not valid
338 :rtype: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500339
Andrey Andreev04535c72014-01-06 10:57:05 +0200340 Returns the IP address for the current user. If the IP address is not
341 valid, the method will return '0.0.0.0'::
Derek Jones8ede1a22011-10-05 13:34:52 -0500342
Andrey Andreev04535c72014-01-06 10:57:05 +0200343 echo $this->input->ip_address();
Derek Jones8ede1a22011-10-05 13:34:52 -0500344
Andrey Andreev04535c72014-01-06 10:57:05 +0200345 .. important:: This method takes into account the ``$config['proxy_ips']``
346 setting and will return the reported HTTP_X_FORWARDED_FOR,
347 HTTP_CLIENT_IP, HTTP_X_CLIENT_IP or HTTP_X_CLUSTER_CLIENT_IP
348 address for the allowed IP addresses.
Derek Jones8ede1a22011-10-05 13:34:52 -0500349
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200350 .. php:method:: valid_ip($ip[, $which = ''])
Andrey Andreev303eef02012-11-06 14:55:48 +0200351
Andrey Andreev28c2c972014-02-08 04:27:48 +0200352 :param string $ip: IP address
353 :param string $which: IP protocol ('ipv4' or 'ipv6')
354 :returns: TRUE if the address is valid, FALSE if not
355 :rtype: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500356
Andrey Andreev04535c72014-01-06 10:57:05 +0200357 Takes an IP address as input and returns TRUE or FALSE (boolean) depending
358 on whether it is valid or not.
Derek Jones8ede1a22011-10-05 13:34:52 -0500359
Andrey Andreev04535c72014-01-06 10:57:05 +0200360 .. note:: The $this->input->ip_address() method above automatically
361 validates the IP address.
Derek Jones8ede1a22011-10-05 13:34:52 -0500362
Andrey Andreev04535c72014-01-06 10:57:05 +0200363 ::
Andrey Andreev5a257182012-06-10 06:18:14 +0300364
Andrey Andreev04535c72014-01-06 10:57:05 +0200365 if ( ! $this->input->valid_ip($ip))
366 {
367 echo 'Not Valid';
368 }
369 else
370 {
371 echo 'Valid';
372 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500373
Andrey Andreev04535c72014-01-06 10:57:05 +0200374 Accepts an optional second string parameter of 'ipv4' or 'ipv6' to specify
375 an IP format. The default checks for both formats.
Derek Jones8ede1a22011-10-05 13:34:52 -0500376
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200377 .. php:method:: user_agent([$xss_clean = NULL])
Derek Jones8ede1a22011-10-05 13:34:52 -0500378
Andrey Andreev28c2c972014-02-08 04:27:48 +0200379 :returns: User agent string or NULL if not set
Andrey Andreev8850e372014-02-27 21:56:06 +0200380 :param bool $xss_clean: Whether to apply XSS filtering
Andrey Andreev28c2c972014-02-08 04:27:48 +0200381 :rtype: mixed
Derek Jones8ede1a22011-10-05 13:34:52 -0500382
Andrey Andreev04535c72014-01-06 10:57:05 +0200383 Returns the user agent string (web browser) being used by the current user,
384 or NULL if it's not available.
385 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500386
Andrey Andreev04535c72014-01-06 10:57:05 +0200387 echo $this->input->user_agent();
Derek Jones8ede1a22011-10-05 13:34:52 -0500388
Andrey Andreev04535c72014-01-06 10:57:05 +0200389 See the :doc:`User Agent Class <user_agent>` for methods which extract
390 information from the user agent string.
Derek Jones8ede1a22011-10-05 13:34:52 -0500391
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200392 .. php:method:: request_headers([$xss_clean = FALSE])
Derek Jones8ede1a22011-10-05 13:34:52 -0500393
Andrey Andreev28c2c972014-02-08 04:27:48 +0200394 :param bool $xss_clean: Whether to apply XSS filtering
395 :returns: An array of HTTP request headers
396 :rtype: array
Derek Jones8ede1a22011-10-05 13:34:52 -0500397
Andrey Andreev04535c72014-01-06 10:57:05 +0200398 Returns an array of HTTP request headers.
399 Useful if running in a non-Apache environment where
400 `apache_request_headers() <http://php.net/apache_request_headers>`_
401 will not be supported.
402 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500403
Andrey Andreev04535c72014-01-06 10:57:05 +0200404 $headers = $this->input->request_headers();
Derek Jones8ede1a22011-10-05 13:34:52 -0500405
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200406 .. php:method:: get_request_header($index[, $xss_clean = FALSE])
Derek Jones8ede1a22011-10-05 13:34:52 -0500407
Andrey Andreev28c2c972014-02-08 04:27:48 +0200408 :param string $index: HTTP request header name
409 :param bool $xss_clean: Whether to apply XSS filtering
410 :returns: An HTTP request header or NULL if not found
411 :rtype: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500412
Andrey Andreev04535c72014-01-06 10:57:05 +0200413 Returns a single member of the request headers array or NULL
414 if the searched header is not found.
415 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500416
Andrey Andreev04535c72014-01-06 10:57:05 +0200417 $this->input->get_request_header('some-header', TRUE);
Derek Jones8ede1a22011-10-05 13:34:52 -0500418
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200419 .. php:method:: is_ajax_request()
Derek Jones8ede1a22011-10-05 13:34:52 -0500420
Andrey Andreev28c2c972014-02-08 04:27:48 +0200421 :returns: TRUE if it is an Ajax request, FALSE if not
422 :rtype: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500423
Andrey Andreev04535c72014-01-06 10:57:05 +0200424 Checks to see if the HTTP_X_REQUESTED_WITH server header has been
425 set, and returns boolean TRUE if it is or FALSE if not.
Derek Jones8ede1a22011-10-05 13:34:52 -0500426
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200427 .. php:method:: is_cli_request()
Derek Jones8ede1a22011-10-05 13:34:52 -0500428
Andrey Andreev28c2c972014-02-08 04:27:48 +0200429 :returns: TRUE if it is a CLI request, FALSE if not
430 :rtype: bool
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100431
Andrey Andreev04535c72014-01-06 10:57:05 +0200432 Checks to see if the application was run from the command-line
433 interface.
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100434
Andrey Andreev04535c72014-01-06 10:57:05 +0200435 .. note:: This method checks both the PHP SAPI name currently in use
436 and if the ``STDIN`` constant is defined, which is usually a
437 failsafe way to see if PHP is being run via the command line.
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100438
Andrey Andreev04535c72014-01-06 10:57:05 +0200439 ::
440
441 $this->input->is_cli_request()
442
Andrey Andreevea801ab2014-01-20 15:03:43 +0200443 .. note:: This method is DEPRECATED and is now just an alias for the
444 :func:`is_cli()` function.
445
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200446 .. php:method:: method([$upper = FALSE])
Andrey Andreev04535c72014-01-06 10:57:05 +0200447
Andrey Andreev28c2c972014-02-08 04:27:48 +0200448 :param bool $upper: Whether to return the request method name in upper or lower case
449 :returns: HTTP request method
450 :rtype: string
Andrey Andreev04535c72014-01-06 10:57:05 +0200451
452 Returns the ``$_SERVER['REQUEST_METHOD']``, with the option to set it
453 in uppercase or lowercase.
454 ::
455
456 echo $this->input->method(TRUE); // Outputs: POST
457 echo $this->input->method(FALSE); // Outputs: post
David Wosnitzad31a4e62014-12-12 16:35:35 +0100458 echo $this->input->method(); // Outputs: post