blob: 72746c147f55444e01866afbdabbb8e7d6b40cfd [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
13Security Filtering
14==================
15
Andrey Andreev303eef02012-11-06 14:55:48 +020016The security filtering method is called automatically when a new
Derek Jones8ede1a22011-10-05 13:34:52 -050017:doc:`controller <../general/controllers>` is invoked. It does the
18following:
19
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +010020- If $config['allow_get_array'] is FALSE (default is TRUE), destroys
Derek Jones8ede1a22011-10-05 13:34:52 -050021 the global GET array.
22- Destroys all global variables in the event register_globals is
23 turned on.
24- Filters the GET/POST/COOKIE array keys, permitting only alpha-numeric
25 (and a few other) characters.
26- Provides XSS (Cross-site Scripting Hacks) filtering. This can be
27 enabled globally, or upon request.
Andrey Andreevbfb635b2014-01-08 18:32:05 +020028- Standardizes newline characters to ``PHP_EOL`` (\\n in UNIX-based OSes,
29 \\r\\n under Windows). This is configurable.
Derek Jones8ede1a22011-10-05 13:34:52 -050030
31XSS Filtering
32=============
33
34The Input class has the ability to filter input automatically to prevent
35cross-site scripting attacks. If you want the filter to run
36automatically every time it encounters POST or COOKIE data you can
37enable it by opening your application/config/config.php file and setting
38this::
39
40 $config['global_xss_filtering'] = TRUE;
41
42Please refer to the :doc:`Security class <security>` documentation for
43information on using XSS Filtering in your application.
44
Andrey Andreev9cf12d12012-06-13 10:19:59 +030045Using POST, GET, COOKIE, or SERVER Data
46=======================================
Derek Jones8ede1a22011-10-05 13:34:52 -050047
Andrey Andreev9cf12d12012-06-13 10:19:59 +030048CodeIgniter comes with four helper methods that let you fetch POST, GET,
Derek Jones8ede1a22011-10-05 13:34:52 -050049COOKIE or SERVER items. The main advantage of using the provided
Andrey Andreev303eef02012-11-06 14:55:48 +020050methods rather than fetching an item directly (``$_POST['something']``)
Andrey Andreev9cf12d12012-06-13 10:19:59 +030051is that the methods will check to see if the item is set and return
52NULL if not. This lets you conveniently use data without
Derek Jones8ede1a22011-10-05 13:34:52 -050053having to test whether an item exists first. In other words, normally
54you might do something like this::
55
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +010056 $something = isset($_POST['something']) ? $_POST['something'] : NULL;
Derek Jones8ede1a22011-10-05 13:34:52 -050057
Andrey Andreev303eef02012-11-06 14:55:48 +020058With CodeIgniter's built in methods you can simply do this::
Derek Jones8ede1a22011-10-05 13:34:52 -050059
60 $something = $this->input->post('something');
61
Andrey Andreev22c3e732012-06-13 10:21:36 +030062The four methods are:
Derek Jones8ede1a22011-10-05 13:34:52 -050063
64- $this->input->post()
Andrey Andreev22c3e732012-06-13 10:21:36 +030065- $this->input->get()
Derek Jones8ede1a22011-10-05 13:34:52 -050066- $this->input->cookie()
67- $this->input->server()
68
69$this->input->post()
70====================
71
72The first parameter will contain the name of the POST item you are
73looking for::
74
75 $this->input->post('some_data');
76
Andrey Andreev303eef02012-11-06 14:55:48 +020077The method returns NULL if the item you are attempting to retrieve
Andrey Andreev9cf12d12012-06-13 10:19:59 +030078does not exist.
Derek Jones8ede1a22011-10-05 13:34:52 -050079
80The second optional parameter lets you run the data through the XSS
81filter. It's enabled by setting the second parameter to boolean TRUE;
82
83::
84
85 $this->input->post('some_data', TRUE);
86
87To return an array of all POST items call without any parameters.
88
89To return all POST items and pass them through the XSS filter set the
90first parameter NULL while setting the second parameter to boolean;
91
Andrey Andreev303eef02012-11-06 14:55:48 +020092The method returns NULL if there are no items in the POST.
Derek Jones8ede1a22011-10-05 13:34:52 -050093
94::
95
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +010096 $this->input->post(NULL, TRUE); // returns all POST items with XSS filter
Derek Jones8831d112011-10-05 15:57:02 -050097 $this->input->post(); // returns all POST items without XSS filter
Derek Jones8ede1a22011-10-05 13:34:52 -050098
99$this->input->get()
100===================
101
vlakoff441fd262013-08-11 20:36:41 +0200102This method is identical to the POST method, only it fetches GET data
Andrey Andreev303eef02012-11-06 14:55:48 +0200103::
Derek Jones8ede1a22011-10-05 13:34:52 -0500104
105 $this->input->get('some_data', TRUE);
106
107To return an array of all GET items call without any parameters.
108
109To return all GET items and pass them through the XSS filter set the
110first parameter NULL while setting the second parameter to boolean;
111
Andrey Andreev303eef02012-11-06 14:55:48 +0200112The method returns NULL if there are no items in the GET.
Derek Jones8ede1a22011-10-05 13:34:52 -0500113
114::
115
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100116 $this->input->get(NULL, TRUE); // returns all GET items with XSS filter
Derek Jones8831d112011-10-05 15:57:02 -0500117 $this->input->get(); // returns all GET items without XSS filtering
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100118
Derek Jones8ede1a22011-10-05 13:34:52 -0500119
vlakoff441fd262013-08-11 20:36:41 +0200120$this->input->post_get()
121========================
122
123This method will search through both the POST and GET streams for
124data, looking first in POST, and then in GET::
125
126 $this->input->post_get('some_data', TRUE);
127
Derek Jones8ede1a22011-10-05 13:34:52 -0500128$this->input->get_post()
Andrey Andreev303eef02012-11-06 14:55:48 +0200129========================
Derek Jones8ede1a22011-10-05 13:34:52 -0500130
vlakoff441fd262013-08-11 20:36:41 +0200131This method will search through both the POST and GET streams for
132data, looking first in GET, and then in POST::
Derek Jones8ede1a22011-10-05 13:34:52 -0500133
134 $this->input->get_post('some_data', TRUE);
135
136$this->input->cookie()
137======================
138
vlakoff441fd262013-08-11 20:36:41 +0200139This method is identical to the POST method, only it fetches cookie data
Andrey Andreev303eef02012-11-06 14:55:48 +0200140::
Derek Jones8ede1a22011-10-05 13:34:52 -0500141
Andrey Andreev9cf12d12012-06-13 10:19:59 +0300142 $this->input->cookie('some_cookie');
143 $this->input->cookie('some_cookie, TRUE); // with XSS filter
144
Derek Jones8ede1a22011-10-05 13:34:52 -0500145
146$this->input->server()
147======================
148
Andrey Andreev303eef02012-11-06 14:55:48 +0200149This method is identical to the above methods, only it fetches server
Derek Jones8ede1a22011-10-05 13:34:52 -0500150server data::
151
152 $this->input->server('some_data');
153
Andrey Andreev303eef02012-11-06 14:55:48 +0200154Using the php://input stream
155============================
156
157If you want to utilize the PUT, DELETE, PATCH or other exotic request
158methods, they can only be accessed via a special input stream, that
159can only be read once. This isn't as easy as just reading from e.g.
160the ``$_POST`` array, because it will always exist and you can try
161and access multiple variables without caring that you might only have
162one shot at all of the POST data.
163
164CodeIgniter will take care of that for you, and you can access data
165from the **php://input** stream at any time, just by calling the
166``input_stream()`` method::
167
168 $this->input->input_stream('key');
169
170Similar to the methods above, if the requested data is not found, it
171will return NULL and you can also decide whether to run the data
172through ``xss_clean()`` by passing a boolean value as the second
173parameter::
174
175 $this->input->input_stream('key', TRUE); // XSS Clean
176 $this->input->input_stream('key', FALSE); // No XSS filter
177
178.. note:: You can utilize method() in order to know if you're reading
179 PUT, DELETE or PATCH data.
180
Derek Jones8ede1a22011-10-05 13:34:52 -0500181$this->input->set_cookie()
Andrey Andreev303eef02012-11-06 14:55:48 +0200182==========================
Derek Jones8ede1a22011-10-05 13:34:52 -0500183
184Sets a cookie containing the values you specify. There are two ways to
Andrey Andreev303eef02012-11-06 14:55:48 +0200185pass information to this method so that a cookie can be set: Array
Derek Jones8ede1a22011-10-05 13:34:52 -0500186Method, and Discrete Parameters:
187
188Array Method
189^^^^^^^^^^^^
190
191Using this method, an associative array is passed to the first
192parameter::
193
Derek Jones8831d112011-10-05 15:57:02 -0500194 $cookie = array(
195 'name' => 'The Cookie Name',
196 'value' => 'The Value',
197 'expire' => '86500',
198 'domain' => '.some-domain.com',
199 'path' => '/',
200 'prefix' => 'myprefix_',
201 'secure' => TRUE
202 );
203
204 $this->input->set_cookie($cookie);
Derek Jones8ede1a22011-10-05 13:34:52 -0500205
206**Notes:**
207
208Only the name and value are required. To delete a cookie set it with the
209expiration blank.
210
211The expiration is set in **seconds**, which will be added to the current
212time. Do not include the time, but rather only the number of seconds
213from *now* that you wish the cookie to be valid. If the expiration is
214set to zero the cookie will only last as long as the browser is open.
215
216For site-wide cookies regardless of how your site is requested, add your
217URL to the **domain** starting with a period, like this:
218.your-domain.com
219
Andrey Andreev303eef02012-11-06 14:55:48 +0200220The path is usually not needed since the method sets a root path.
Derek Jones8ede1a22011-10-05 13:34:52 -0500221
222The prefix is only needed if you need to avoid name collisions with
223other identically named cookies for your server.
224
225The secure boolean is only needed if you want to make it a secure cookie
226by setting it to TRUE.
227
228Discrete Parameters
229^^^^^^^^^^^^^^^^^^^
230
231If you prefer, you can set the cookie by passing data using individual
232parameters::
233
234 $this->input->set_cookie($name, $value, $expire, $domain, $path, $prefix, $secure);
235
Andrey Andreev303eef02012-11-06 14:55:48 +0200236
Derek Jones8ede1a22011-10-05 13:34:52 -0500237$this->input->ip_address()
Andrey Andreev303eef02012-11-06 14:55:48 +0200238==========================
Derek Jones8ede1a22011-10-05 13:34:52 -0500239
240Returns the IP address for the current user. If the IP address is not
Andrey Andreev303eef02012-11-06 14:55:48 +0200241valid, the method will return an IP of: 0.0.0.0
Derek Jones8ede1a22011-10-05 13:34:52 -0500242
243::
244
245 echo $this->input->ip_address();
246
247$this->input->valid_ip($ip)
Andrey Andreev303eef02012-11-06 14:55:48 +0200248===========================
Derek Jones8ede1a22011-10-05 13:34:52 -0500249
250Takes an IP address as input and returns TRUE or FALSE (boolean) if it
Andrey Andreev303eef02012-11-06 14:55:48 +0200251is valid or not.
252
253.. note:: The $this->input->ip_address() method above automatically
254 validates the IP address.
Derek Jones8ede1a22011-10-05 13:34:52 -0500255
256::
257
Derek Jones8831d112011-10-05 15:57:02 -0500258 if ( ! $this->input->valid_ip($ip))
259 {
260 echo 'Not Valid';
261 }
262 else
263 {
264 echo 'Valid';
265 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500266
Andrey Andreev5a257182012-06-10 06:18:14 +0300267Accepts an optional second string parameter of 'ipv4' or 'ipv6' to specify
268an IP format. The default checks for both formats.
269
Derek Jones8ede1a22011-10-05 13:34:52 -0500270$this->input->user_agent()
Andrey Andreev303eef02012-11-06 14:55:48 +0200271==========================
Derek Jones8ede1a22011-10-05 13:34:52 -0500272
273Returns the user agent (web browser) being used by the current user.
274Returns FALSE if it's not available.
275
276::
277
278 echo $this->input->user_agent();
279
280See the :doc:`User Agent Class <user_agent>` for methods which extract
281information from the user agent string.
282
283$this->input->request_headers()
Andrey Andreev303eef02012-11-06 14:55:48 +0200284===============================
Derek Jones8ede1a22011-10-05 13:34:52 -0500285
286Useful if running in a non-Apache environment where
287`apache_request_headers() <http://php.net/apache_request_headers>`_
288will not be supported. Returns an array of headers.
289
290::
291
292 $headers = $this->input->request_headers();
293
Andrey Andreev303eef02012-11-06 14:55:48 +0200294$this->input->get_request_header()
295==================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500296
297Returns a single member of the request headers array.
298
299::
300
301 $this->input->get_request_header('some-header', TRUE);
302
303$this->input->is_ajax_request()
Andrey Andreev303eef02012-11-06 14:55:48 +0200304===============================
Derek Jones8ede1a22011-10-05 13:34:52 -0500305
306Checks to see if the HTTP_X_REQUESTED_WITH server header has been
307set, and returns a boolean response.
308
309$this->input->is_cli_request()
Andrey Andreev303eef02012-11-06 14:55:48 +0200310==============================
Derek Jones8ede1a22011-10-05 13:34:52 -0500311
312Checks to see if the STDIN constant is set, which is a failsafe way to
313see if PHP is being run on the command line.
314
315::
316
317 $this->input->is_cli_request()
318
Andrey Andreevf964b162013-11-12 17:04:55 +0200319.. note:: This method is DEPRECATED and is now just an alias for the
320 :php:func:`is_cli()` function.
321
Andrey Andreev303eef02012-11-06 14:55:48 +0200322$this->input->method()
323======================
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100324
Michiel Vugteveen1e9fb492012-03-07 20:51:25 +0100325Returns the $_SERVER['REQUEST_METHOD'], optional set uppercase or lowercase (default lowercase).
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100326
327::
328
Michiel Vugteveendc900df2012-03-07 20:41:37 +0100329 echo $this->input->method(TRUE); // Outputs: POST
330 echo $this->input->method(FALSE); // Outputs: post
Andrey Andreev303eef02012-11-06 14:55:48 +0200331 echo $this->input->method(); // Outputs: post