blob: 2b71b348ac5646eac61e95730a9fc5dbed4304cd [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
Ignasimg0b5569f2015-02-20 17:56:55 +010094CodeIgniter will take care of that for you, and you can read the data
95from the **php://input** stream at any time, just by using the
96``raw_input_stream`` property::
97
98 $this->input->raw_input_stream;
99
100Additionally if the input stream is formated in a query string fashion
101you can access it's values, just by calling the
Andrey Andreev303eef02012-11-06 14:55:48 +0200102``input_stream()`` method::
103
104 $this->input->input_stream('key');
105
Andrey Andreev04535c72014-01-06 10:57:05 +0200106Similar to other methods such as ``get()`` and ``post()``, if the
107requested data is not found, it will return NULL and you can also
108decide whether to run the data through ``xss_clean()`` by passing
109a boolean value as the second parameter::
Andrey Andreev303eef02012-11-06 14:55:48 +0200110
111 $this->input->input_stream('key', TRUE); // XSS Clean
112 $this->input->input_stream('key', FALSE); // No XSS filter
113
Andrey Andreev04535c72014-01-06 10:57:05 +0200114.. note:: You can utilize ``method()`` in order to know if you're reading
Andrey Andreev303eef02012-11-06 14:55:48 +0200115 PUT, DELETE or PATCH data.
116
Andrey Andreev04535c72014-01-06 10:57:05 +0200117***************
118Class Reference
119***************
Derek Jones8ede1a22011-10-05 13:34:52 -0500120
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200121.. php:class:: CI_Input
Derek Jones8ede1a22011-10-05 13:34:52 -0500122
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200123 .. php:method:: post([$index = NULL[, $xss_clean = NULL]])
Derek Jones8ede1a22011-10-05 13:34:52 -0500124
Ahmad Anbarff89a4e2014-12-02 17:26:30 +0200125 :param mixed $index: POST parameter name
Andrey Andreev28c2c972014-02-08 04:27:48 +0200126 :param bool $xss_clean: Whether to apply XSS filtering
127 :returns: $_POST if no parameters supplied, otherwise the POST value if found or NULL if not
128 :rtype: mixed
Derek Jones8ede1a22011-10-05 13:34:52 -0500129
Andrey Andreev04535c72014-01-06 10:57:05 +0200130 The first parameter will contain the name of the POST item you are
131 looking for::
Derek Jones8831d112011-10-05 15:57:02 -0500132
Andrey Andreev04535c72014-01-06 10:57:05 +0200133 $this->input->post('some_data');
Derek Jones8ede1a22011-10-05 13:34:52 -0500134
Andrey Andreev04535c72014-01-06 10:57:05 +0200135 The method returns NULL if the item you are attempting to retrieve
136 does not exist.
Derek Jones8ede1a22011-10-05 13:34:52 -0500137
Andrey Andreev04535c72014-01-06 10:57:05 +0200138 The second optional parameter lets you run the data through the XSS
Andrey Andreev88ebdf72014-01-08 17:28:02 +0200139 filter. It's enabled by setting the second parameter to boolean TRUE
140 or by setting your ``$config['global_xss_filtering']`` to TRUE.
Andrey Andreev04535c72014-01-06 10:57:05 +0200141 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500142
Andrey Andreev04535c72014-01-06 10:57:05 +0200143 $this->input->post('some_data', TRUE);
Derek Jones8ede1a22011-10-05 13:34:52 -0500144
Andrey Andreev04535c72014-01-06 10:57:05 +0200145 To return an array of all POST items call without any parameters.
Derek Jones8ede1a22011-10-05 13:34:52 -0500146
Andrey Andreev04535c72014-01-06 10:57:05 +0200147 To return all POST items and pass them through the XSS filter set the
148 first parameter NULL while setting the second parameter to boolean TRUE.
149 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500150
Andrey Andreev04535c72014-01-06 10:57:05 +0200151 $this->input->post(NULL, TRUE); // returns all POST items with XSS filter
Andrey Andreev88ebdf72014-01-08 17:28:02 +0200152 $this->input->post(NULL, FALSE); // returns all POST items without XSS filter
David Wosnitzad31a4e62014-12-12 16:35:35 +0100153
Ahmad Anbarff89a4e2014-12-02 17:26:30 +0200154 To return an array of multiple POST parameters, pass all the required keys
155 as an array.
156 ::
David Wosnitzad31a4e62014-12-12 16:35:35 +0100157
Ahmad Anbarff89a4e2014-12-02 17:26:30 +0200158 $this->input->post(array('field1', 'field2'));
David Wosnitzad31a4e62014-12-12 16:35:35 +0100159
Ahmad Anbarff89a4e2014-12-02 17:26:30 +0200160 Same rule applied here, to retrive the parameters with XSS filtering enabled, set the
161 second parameter to boolean TRUE.
162 ::
David Wosnitzad31a4e62014-12-12 16:35:35 +0100163
Ahmad Anbarff89a4e2014-12-02 17:26:30 +0200164 $this->input->post(array('field1', 'field2'), TRUE);
Derek Jones8ede1a22011-10-05 13:34:52 -0500165
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200166 .. php:method:: get([$index = NULL[, $xss_clean = NULL]])
Derek Jones8ede1a22011-10-05 13:34:52 -0500167
Ahmad Anbarff89a4e2014-12-02 17:26:30 +0200168 :param mixed $index: GET parameter name
Andrey Andreev28c2c972014-02-08 04:27:48 +0200169 :param bool $xss_clean: Whether to apply XSS filtering
170 :returns: $_GET if no parameters supplied, otherwise the GET value if found or NULL if not
171 :rtype: mixed
Derek Jones8ede1a22011-10-05 13:34:52 -0500172
Andrey Andreev04535c72014-01-06 10:57:05 +0200173 This method is identical to ``post()``, only it fetches GET data.
174 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500175
Andrey Andreev04535c72014-01-06 10:57:05 +0200176 $this->input->get('some_data', TRUE);
177
178 To return an array of all GET items call without any parameters.
179
180 To return all GET items and pass them through the XSS filter set the
181 first parameter NULL while setting the second parameter to boolean TRUE.
182 ::
183
184 $this->input->get(NULL, TRUE); // returns all GET items with XSS filter
Andrey Andreev88ebdf72014-01-08 17:28:02 +0200185 $this->input->get(NULL, FALSE); // returns all GET items without XSS filtering
David Wosnitzad31a4e62014-12-12 16:35:35 +0100186
Ahmad Anbarff89a4e2014-12-02 17:26:30 +0200187 To return an array of multiple GET parameters, pass all the required keys
188 as an array.
189 ::
David Wosnitzad31a4e62014-12-12 16:35:35 +0100190
Ahmad Anbarff89a4e2014-12-02 17:26:30 +0200191 $this->input->get(array('field1', 'field2'));
David Wosnitzad31a4e62014-12-12 16:35:35 +0100192
Ahmad Anbarff89a4e2014-12-02 17:26:30 +0200193 Same rule applied here, to retrive the parameters with XSS filtering enabled, set the
194 second parameter to boolean TRUE.
195 ::
David Wosnitzad31a4e62014-12-12 16:35:35 +0100196
Ahmad Anbarff89a4e2014-12-02 17:26:30 +0200197 $this->input->get(array('field1', 'field2'), TRUE);
Andrey Andreev04535c72014-01-06 10:57:05 +0200198
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200199 .. php:method:: post_get($index[, $xss_clean = NULL])
Andrey Andreevea801ab2014-01-20 15:03:43 +0200200
Andrey Andreev28c2c972014-02-08 04:27:48 +0200201 :param string $index: POST/GET parameter name
202 :param bool $xss_clean: Whether to apply XSS filtering
203 :returns: POST/GET value if found, NULL if not
204 :rtype: mixed
Andrey Andreevea801ab2014-01-20 15:03:43 +0200205
Andrey Andreev7c60b122014-02-08 18:47:19 +0200206 This method works pretty much the same way as ``post()`` and ``get()``,
207 only combined. It will search through both POST and GET streams for data,
208 looking in POST first, and then in GET::
Andrey Andreevea801ab2014-01-20 15:03:43 +0200209
210 $this->input->post_get('some_data', TRUE);
211
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200212 .. php:method:: get_post($index[, $xss_clean = NULL])
Andrey Andreev04535c72014-01-06 10:57:05 +0200213
Andrey Andreev28c2c972014-02-08 04:27:48 +0200214 :param string $index: GET/POST parameter name
215 :param bool $xss_clean: Whether to apply XSS filtering
216 :returns: GET/POST value if found, NULL if not
217 :rtype: mixed
Andrey Andreev04535c72014-01-06 10:57:05 +0200218
Andrey Andreevea801ab2014-01-20 15:03:43 +0200219 This method works the same way as ``post_get()`` only it looks for GET
220 data first.
Andrey Andreev04535c72014-01-06 10:57:05 +0200221
222 $this->input->get_post('some_data', TRUE);
223
Andrey Andreevea801ab2014-01-20 15:03:43 +0200224 .. note:: This method used to act EXACTLY like ``post_get()``, but it's
225 behavior has changed in CodeIgniter 3.0.
226
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200227 .. php:method:: cookie([$index = NULL[, $xss_clean = NULL]])
Andrey Andreev04535c72014-01-06 10:57:05 +0200228
Andrey Andreevef29f832014-12-02 18:03:47 +0200229 :param mixed $index: COOKIE name
Andrey Andreev28c2c972014-02-08 04:27:48 +0200230 :param bool $xss_clean: Whether to apply XSS filtering
231 :returns: $_COOKIE if no parameters supplied, otherwise the COOKIE value if found or NULL if not
232 :rtype: mixed
Andrey Andreev04535c72014-01-06 10:57:05 +0200233
234 This method is identical to ``post()`` and ``get()``, only it fetches cookie
235 data::
236
237 $this->input->cookie('some_cookie');
238 $this->input->cookie('some_cookie, TRUE); // with XSS filter
David Wosnitzad31a4e62014-12-12 16:35:35 +0100239
Andrey Andreevef29f832014-12-02 18:03:47 +0200240 To return an array of multiple cookie values, pass all the required keys
Ahmad Anbarff89a4e2014-12-02 17:26:30 +0200241 as an array.
242 ::
David Wosnitzad31a4e62014-12-12 16:35:35 +0100243
Ahmad Anbarff89a4e2014-12-02 17:26:30 +0200244 $this->input->cookie(array('some_cookie', 'some_cookie2'));
Andrey Andreev04535c72014-01-06 10:57:05 +0200245
Andrey Andreevb4834cc2015-01-29 13:52:44 +0200246 .. note:: Unlike the :doc:`Cookie Helper <../helpers/cookie_helper>`
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200247 function :php:func:`get_cookie()`, this method does NOT prepend
Andrey Andreevb4834cc2015-01-29 13:52:44 +0200248 your configured ``$config['cookie_prefix']`` value.
249
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200250 .. php:method:: server($index[, $xss_clean = NULL])
Andrey Andreev04535c72014-01-06 10:57:05 +0200251
Ahmad Anbarff89a4e2014-12-02 17:26:30 +0200252 :param mixed $index: Value name
Andrey Andreev28c2c972014-02-08 04:27:48 +0200253 :param bool $xss_clean: Whether to apply XSS filtering
254 :returns: $_SERVER item value if found, NULL if not
255 :rtype: mixed
Andrey Andreev04535c72014-01-06 10:57:05 +0200256
Andrey Andreev7c60b122014-02-08 18:47:19 +0200257 This method is identical to the ``post()``, ``get()`` and ``cookie()``
258 methods, only it fetches server data (``$_SERVER``)::
Andrey Andreev04535c72014-01-06 10:57:05 +0200259
260 $this->input->server('some_data');
261
Andrey Andreevef29f832014-12-02 18:03:47 +0200262 To return an array of multiple ``$_SERVER`` values, pass all the required keys
Ahmad Anbarff89a4e2014-12-02 17:26:30 +0200263 as an array.
264 ::
David Wosnitzad31a4e62014-12-12 16:35:35 +0100265
266 $this->input->server(array('SERVER_PROTOCOL', 'REQUEST_URI'));
Ahmad Anbarff89a4e2014-12-02 17:26:30 +0200267
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200268 .. php:method:: input_stream([$index = NULL[, $xss_clean = NULL]])
Andrey Andreev04535c72014-01-06 10:57:05 +0200269
Ahmad Anbarff89a4e2014-12-02 17:26:30 +0200270 :param mixed $index: Key name
Andrey Andreev28c2c972014-02-08 04:27:48 +0200271 :param bool $xss_clean: Whether to apply XSS filtering
272 :returns: Input stream array if no parameters supplied, otherwise the specified value if found or NULL if not
273 :rtype: mixed
Andrey Andreev04535c72014-01-06 10:57:05 +0200274
275 This method is identical to ``get()``, ``post()`` and ``cookie()``,
276 only it fetches the *php://input* stream data.
277
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200278 .. php:method:: set_cookie($name = ''[, $value = ''[, $expire = ''[, $domain = ''[, $path = '/'[, $prefix = ''[, $secure = FALSE[, $httponly = FALSE]]]]]]])
Andrey Andreev04535c72014-01-06 10:57:05 +0200279
Andrey Andreev28c2c972014-02-08 04:27:48 +0200280 :param mixed $name: Cookie name or an array of parameters
281 :param string $value: Cookie value
282 :param int $expire: Cookie expiration time in seconds
283 :param string $domain: Cookie domain
284 :param string $path: Cookie path
285 :param string $prefix: Cookie name prefix
286 :param bool $secure: Whether to only transfer the cookie through HTTPS
287 :param bool $httponly: Whether to only make the cookie accessible for HTTP requests (no JavaScript)
288 :rtype: void
289
Andrey Andreev04535c72014-01-06 10:57:05 +0200290
291 Sets a cookie containing the values you specify. There are two ways to
292 pass information to this method so that a cookie can be set: Array
293 Method, and Discrete Parameters:
294
Connor Tumbleson75b3fb22014-01-11 06:58:43 -0600295 **Array Method**
Andrey Andreev04535c72014-01-06 10:57:05 +0200296
297 Using this method, an associative array is passed to the first
298 parameter::
299
300 $cookie = array(
301 'name' => 'The Cookie Name',
302 'value' => 'The Value',
303 'expire' => '86500',
304 'domain' => '.some-domain.com',
305 'path' => '/',
306 'prefix' => 'myprefix_',
307 'secure' => TRUE
308 );
309
310 $this->input->set_cookie($cookie);
311
Andrey Andreev28c2c972014-02-08 04:27:48 +0200312 **Notes**
Andrey Andreev04535c72014-01-06 10:57:05 +0200313
314 Only the name and value are required. To delete a cookie set it with the
315 expiration blank.
316
317 The expiration is set in **seconds**, which will be added to the current
318 time. Do not include the time, but rather only the number of seconds
319 from *now* that you wish the cookie to be valid. If the expiration is
320 set to zero the cookie will only last as long as the browser is open.
321
322 For site-wide cookies regardless of how your site is requested, add your
323 URL to the **domain** starting with a period, like this:
324 .your-domain.com
325
326 The path is usually not needed since the method sets a root path.
327
328 The prefix is only needed if you need to avoid name collisions with
329 other identically named cookies for your server.
330
331 The secure boolean is only needed if you want to make it a secure cookie
332 by setting it to TRUE.
333
Connor Tumbleson75b3fb22014-01-11 06:58:43 -0600334 **Discrete Parameters**
Andrey Andreev04535c72014-01-06 10:57:05 +0200335
336 If you prefer, you can set the cookie by passing data using individual
337 parameters::
338
339 $this->input->set_cookie($name, $value, $expire, $domain, $path, $prefix, $secure);
Derek Jones8ede1a22011-10-05 13:34:52 -0500340
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200341 .. php:method:: ip_address()
Derek Jones8ede1a22011-10-05 13:34:52 -0500342
Andrey Andreev28c2c972014-02-08 04:27:48 +0200343 :returns: Visitor's IP address or '0.0.0.0' if not valid
344 :rtype: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500345
Andrey Andreev04535c72014-01-06 10:57:05 +0200346 Returns the IP address for the current user. If the IP address is not
347 valid, the method will return '0.0.0.0'::
Derek Jones8ede1a22011-10-05 13:34:52 -0500348
Andrey Andreev04535c72014-01-06 10:57:05 +0200349 echo $this->input->ip_address();
Derek Jones8ede1a22011-10-05 13:34:52 -0500350
Andrey Andreev04535c72014-01-06 10:57:05 +0200351 .. important:: This method takes into account the ``$config['proxy_ips']``
352 setting and will return the reported HTTP_X_FORWARDED_FOR,
353 HTTP_CLIENT_IP, HTTP_X_CLIENT_IP or HTTP_X_CLUSTER_CLIENT_IP
354 address for the allowed IP addresses.
Derek Jones8ede1a22011-10-05 13:34:52 -0500355
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200356 .. php:method:: valid_ip($ip[, $which = ''])
Andrey Andreev303eef02012-11-06 14:55:48 +0200357
Andrey Andreev28c2c972014-02-08 04:27:48 +0200358 :param string $ip: IP address
359 :param string $which: IP protocol ('ipv4' or 'ipv6')
360 :returns: TRUE if the address is valid, FALSE if not
361 :rtype: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500362
Andrey Andreev04535c72014-01-06 10:57:05 +0200363 Takes an IP address as input and returns TRUE or FALSE (boolean) depending
364 on whether it is valid or not.
Derek Jones8ede1a22011-10-05 13:34:52 -0500365
Andrey Andreev04535c72014-01-06 10:57:05 +0200366 .. note:: The $this->input->ip_address() method above automatically
367 validates the IP address.
Derek Jones8ede1a22011-10-05 13:34:52 -0500368
Andrey Andreev04535c72014-01-06 10:57:05 +0200369 ::
Andrey Andreev5a257182012-06-10 06:18:14 +0300370
Andrey Andreev04535c72014-01-06 10:57:05 +0200371 if ( ! $this->input->valid_ip($ip))
372 {
373 echo 'Not Valid';
374 }
375 else
376 {
377 echo 'Valid';
378 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500379
Andrey Andreev04535c72014-01-06 10:57:05 +0200380 Accepts an optional second string parameter of 'ipv4' or 'ipv6' to specify
381 an IP format. The default checks for both formats.
Derek Jones8ede1a22011-10-05 13:34:52 -0500382
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200383 .. php:method:: user_agent([$xss_clean = NULL])
Derek Jones8ede1a22011-10-05 13:34:52 -0500384
Andrey Andreev28c2c972014-02-08 04:27:48 +0200385 :returns: User agent string or NULL if not set
Andrey Andreev8850e372014-02-27 21:56:06 +0200386 :param bool $xss_clean: Whether to apply XSS filtering
Andrey Andreev28c2c972014-02-08 04:27:48 +0200387 :rtype: mixed
Derek Jones8ede1a22011-10-05 13:34:52 -0500388
Andrey Andreev04535c72014-01-06 10:57:05 +0200389 Returns the user agent string (web browser) being used by the current user,
390 or NULL if it's not available.
391 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500392
Andrey Andreev04535c72014-01-06 10:57:05 +0200393 echo $this->input->user_agent();
Derek Jones8ede1a22011-10-05 13:34:52 -0500394
Andrey Andreev04535c72014-01-06 10:57:05 +0200395 See the :doc:`User Agent Class <user_agent>` for methods which extract
396 information from the user agent string.
Derek Jones8ede1a22011-10-05 13:34:52 -0500397
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200398 .. php:method:: request_headers([$xss_clean = FALSE])
Derek Jones8ede1a22011-10-05 13:34:52 -0500399
Andrey Andreev28c2c972014-02-08 04:27:48 +0200400 :param bool $xss_clean: Whether to apply XSS filtering
401 :returns: An array of HTTP request headers
402 :rtype: array
Derek Jones8ede1a22011-10-05 13:34:52 -0500403
Andrey Andreev04535c72014-01-06 10:57:05 +0200404 Returns an array of HTTP request headers.
405 Useful if running in a non-Apache environment where
406 `apache_request_headers() <http://php.net/apache_request_headers>`_
407 will not be supported.
408 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500409
Andrey Andreev04535c72014-01-06 10:57:05 +0200410 $headers = $this->input->request_headers();
Derek Jones8ede1a22011-10-05 13:34:52 -0500411
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200412 .. php:method:: get_request_header($index[, $xss_clean = FALSE])
Derek Jones8ede1a22011-10-05 13:34:52 -0500413
Andrey Andreev28c2c972014-02-08 04:27:48 +0200414 :param string $index: HTTP request header name
415 :param bool $xss_clean: Whether to apply XSS filtering
416 :returns: An HTTP request header or NULL if not found
417 :rtype: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500418
Andrey Andreev04535c72014-01-06 10:57:05 +0200419 Returns a single member of the request headers array or NULL
420 if the searched header is not found.
421 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500422
Andrey Andreev04535c72014-01-06 10:57:05 +0200423 $this->input->get_request_header('some-header', TRUE);
Derek Jones8ede1a22011-10-05 13:34:52 -0500424
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200425 .. php:method:: is_ajax_request()
Derek Jones8ede1a22011-10-05 13:34:52 -0500426
Andrey Andreev28c2c972014-02-08 04:27:48 +0200427 :returns: TRUE if it is an Ajax request, FALSE if not
428 :rtype: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500429
Andrey Andreev04535c72014-01-06 10:57:05 +0200430 Checks to see if the HTTP_X_REQUESTED_WITH server header has been
431 set, and returns boolean TRUE if it is or FALSE if not.
Derek Jones8ede1a22011-10-05 13:34:52 -0500432
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200433 .. php:method:: is_cli_request()
Derek Jones8ede1a22011-10-05 13:34:52 -0500434
Andrey Andreev28c2c972014-02-08 04:27:48 +0200435 :returns: TRUE if it is a CLI request, FALSE if not
436 :rtype: bool
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100437
Andrey Andreev04535c72014-01-06 10:57:05 +0200438 Checks to see if the application was run from the command-line
439 interface.
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100440
Andrey Andreev04535c72014-01-06 10:57:05 +0200441 .. note:: This method checks both the PHP SAPI name currently in use
442 and if the ``STDIN`` constant is defined, which is usually a
443 failsafe way to see if PHP is being run via the command line.
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100444
Andrey Andreev04535c72014-01-06 10:57:05 +0200445 ::
446
447 $this->input->is_cli_request()
448
Andrey Andreevea801ab2014-01-20 15:03:43 +0200449 .. note:: This method is DEPRECATED and is now just an alias for the
450 :func:`is_cli()` function.
451
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200452 .. php:method:: method([$upper = FALSE])
Andrey Andreev04535c72014-01-06 10:57:05 +0200453
Andrey Andreev28c2c972014-02-08 04:27:48 +0200454 :param bool $upper: Whether to return the request method name in upper or lower case
455 :returns: HTTP request method
456 :rtype: string
Andrey Andreev04535c72014-01-06 10:57:05 +0200457
458 Returns the ``$_SERVER['REQUEST_METHOD']``, with the option to set it
459 in uppercase or lowercase.
460 ::
461
462 echo $this->input->method(TRUE); // Outputs: POST
463 echo $this->input->method(FALSE); // Outputs: post
David Wosnitzad31a4e62014-12-12 16:35:35 +0100464 echo $this->input->method(); // Outputs: post