blob: fb245d7cd6d40ad03f84b710da4fc12195dfbcc7 [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.
28- Standardizes newline characters to \\n(In Windows \\r\\n)
29
30XSS Filtering
31=============
32
33The Input class has the ability to filter input automatically to prevent
34cross-site scripting attacks. If you want the filter to run
35automatically every time it encounters POST or COOKIE data you can
36enable it by opening your application/config/config.php file and setting
37this::
38
39 $config['global_xss_filtering'] = TRUE;
40
41Please refer to the :doc:`Security class <security>` documentation for
42information on using XSS Filtering in your application.
43
Andrey Andreev9cf12d12012-06-13 10:19:59 +030044Using POST, GET, COOKIE, or SERVER Data
45=======================================
Derek Jones8ede1a22011-10-05 13:34:52 -050046
Andrey Andreev9cf12d12012-06-13 10:19:59 +030047CodeIgniter comes with four helper methods that let you fetch POST, GET,
Derek Jones8ede1a22011-10-05 13:34:52 -050048COOKIE or SERVER items. The main advantage of using the provided
Andrey Andreev303eef02012-11-06 14:55:48 +020049methods rather than fetching an item directly (``$_POST['something']``)
Andrey Andreev9cf12d12012-06-13 10:19:59 +030050is that the methods will check to see if the item is set and return
51NULL if not. This lets you conveniently use data without
Derek Jones8ede1a22011-10-05 13:34:52 -050052having to test whether an item exists first. In other words, normally
53you might do something like this::
54
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +010055 $something = isset($_POST['something']) ? $_POST['something'] : NULL;
Derek Jones8ede1a22011-10-05 13:34:52 -050056
Andrey Andreev303eef02012-11-06 14:55:48 +020057With CodeIgniter's built in methods you can simply do this::
Derek Jones8ede1a22011-10-05 13:34:52 -050058
59 $something = $this->input->post('something');
60
Andrey Andreev22c3e732012-06-13 10:21:36 +030061The four methods are:
Derek Jones8ede1a22011-10-05 13:34:52 -050062
63- $this->input->post()
Andrey Andreev22c3e732012-06-13 10:21:36 +030064- $this->input->get()
Derek Jones8ede1a22011-10-05 13:34:52 -050065- $this->input->cookie()
66- $this->input->server()
67
68$this->input->post()
69====================
70
71The first parameter will contain the name of the POST item you are
72looking for::
73
74 $this->input->post('some_data');
75
Andrey Andreev303eef02012-11-06 14:55:48 +020076The method returns NULL if the item you are attempting to retrieve
Andrey Andreev9cf12d12012-06-13 10:19:59 +030077does not exist.
Derek Jones8ede1a22011-10-05 13:34:52 -050078
79The second optional parameter lets you run the data through the XSS
80filter. It's enabled by setting the second parameter to boolean TRUE;
81
82::
83
84 $this->input->post('some_data', TRUE);
85
86To return an array of all POST items call without any parameters.
87
88To return all POST items and pass them through the XSS filter set the
89first parameter NULL while setting the second parameter to boolean;
90
Andrey Andreev303eef02012-11-06 14:55:48 +020091The method returns NULL if there are no items in the POST.
Derek Jones8ede1a22011-10-05 13:34:52 -050092
93::
94
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +010095 $this->input->post(NULL, TRUE); // returns all POST items with XSS filter
Derek Jones8831d112011-10-05 15:57:02 -050096 $this->input->post(); // returns all POST items without XSS filter
Derek Jones8ede1a22011-10-05 13:34:52 -050097
98$this->input->get()
99===================
100
vlakoff441fd262013-08-11 20:36:41 +0200101This method is identical to the POST method, only it fetches GET data
Andrey Andreev303eef02012-11-06 14:55:48 +0200102::
Derek Jones8ede1a22011-10-05 13:34:52 -0500103
104 $this->input->get('some_data', TRUE);
105
106To return an array of all GET items call without any parameters.
107
108To return all GET items and pass them through the XSS filter set the
109first parameter NULL while setting the second parameter to boolean;
110
Andrey Andreev303eef02012-11-06 14:55:48 +0200111The method returns NULL if there are no items in the GET.
Derek Jones8ede1a22011-10-05 13:34:52 -0500112
113::
114
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100115 $this->input->get(NULL, TRUE); // returns all GET items with XSS filter
Derek Jones8831d112011-10-05 15:57:02 -0500116 $this->input->get(); // returns all GET items without XSS filtering
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100117
Derek Jones8ede1a22011-10-05 13:34:52 -0500118
vlakoff441fd262013-08-11 20:36:41 +0200119$this->input->post_get()
120========================
121
122This method will search through both the POST and GET streams for
123data, looking first in POST, and then in GET::
124
125 $this->input->post_get('some_data', TRUE);
126
Derek Jones8ede1a22011-10-05 13:34:52 -0500127$this->input->get_post()
Andrey Andreev303eef02012-11-06 14:55:48 +0200128========================
Derek Jones8ede1a22011-10-05 13:34:52 -0500129
vlakoff441fd262013-08-11 20:36:41 +0200130This method will search through both the POST and GET streams for
131data, looking first in GET, and then in POST::
Derek Jones8ede1a22011-10-05 13:34:52 -0500132
133 $this->input->get_post('some_data', TRUE);
134
135$this->input->cookie()
136======================
137
vlakoff441fd262013-08-11 20:36:41 +0200138This method is identical to the POST method, only it fetches cookie data
Andrey Andreev303eef02012-11-06 14:55:48 +0200139::
Derek Jones8ede1a22011-10-05 13:34:52 -0500140
Andrey Andreev9cf12d12012-06-13 10:19:59 +0300141 $this->input->cookie('some_cookie');
142 $this->input->cookie('some_cookie, TRUE); // with XSS filter
143
Derek Jones8ede1a22011-10-05 13:34:52 -0500144
145$this->input->server()
146======================
147
Andrey Andreev303eef02012-11-06 14:55:48 +0200148This method is identical to the above methods, only it fetches server
Derek Jones8ede1a22011-10-05 13:34:52 -0500149server data::
150
151 $this->input->server('some_data');
152
Andrey Andreev303eef02012-11-06 14:55:48 +0200153Using the php://input stream
154============================
155
156If you want to utilize the PUT, DELETE, PATCH or other exotic request
157methods, they can only be accessed via a special input stream, that
158can only be read once. This isn't as easy as just reading from e.g.
159the ``$_POST`` array, because it will always exist and you can try
160and access multiple variables without caring that you might only have
161one shot at all of the POST data.
162
163CodeIgniter will take care of that for you, and you can access data
164from the **php://input** stream at any time, just by calling the
165``input_stream()`` method::
166
167 $this->input->input_stream('key');
168
169Similar to the methods above, if the requested data is not found, it
170will return NULL and you can also decide whether to run the data
171through ``xss_clean()`` by passing a boolean value as the second
172parameter::
173
174 $this->input->input_stream('key', TRUE); // XSS Clean
175 $this->input->input_stream('key', FALSE); // No XSS filter
176
177.. note:: You can utilize method() in order to know if you're reading
178 PUT, DELETE or PATCH data.
179
Derek Jones8ede1a22011-10-05 13:34:52 -0500180$this->input->set_cookie()
Andrey Andreev303eef02012-11-06 14:55:48 +0200181==========================
Derek Jones8ede1a22011-10-05 13:34:52 -0500182
183Sets a cookie containing the values you specify. There are two ways to
Andrey Andreev303eef02012-11-06 14:55:48 +0200184pass information to this method so that a cookie can be set: Array
Derek Jones8ede1a22011-10-05 13:34:52 -0500185Method, and Discrete Parameters:
186
187Array Method
188^^^^^^^^^^^^
189
190Using this method, an associative array is passed to the first
191parameter::
192
Derek Jones8831d112011-10-05 15:57:02 -0500193 $cookie = array(
194 'name' => 'The Cookie Name',
195 'value' => 'The Value',
196 'expire' => '86500',
197 'domain' => '.some-domain.com',
198 'path' => '/',
199 'prefix' => 'myprefix_',
200 'secure' => TRUE
201 );
202
203 $this->input->set_cookie($cookie);
Derek Jones8ede1a22011-10-05 13:34:52 -0500204
205**Notes:**
206
207Only the name and value are required. To delete a cookie set it with the
208expiration blank.
209
210The expiration is set in **seconds**, which will be added to the current
211time. Do not include the time, but rather only the number of seconds
212from *now* that you wish the cookie to be valid. If the expiration is
213set to zero the cookie will only last as long as the browser is open.
214
215For site-wide cookies regardless of how your site is requested, add your
216URL to the **domain** starting with a period, like this:
217.your-domain.com
218
Andrey Andreev303eef02012-11-06 14:55:48 +0200219The path is usually not needed since the method sets a root path.
Derek Jones8ede1a22011-10-05 13:34:52 -0500220
221The prefix is only needed if you need to avoid name collisions with
222other identically named cookies for your server.
223
224The secure boolean is only needed if you want to make it a secure cookie
225by setting it to TRUE.
226
227Discrete Parameters
228^^^^^^^^^^^^^^^^^^^
229
230If you prefer, you can set the cookie by passing data using individual
231parameters::
232
233 $this->input->set_cookie($name, $value, $expire, $domain, $path, $prefix, $secure);
234
Andrey Andreev303eef02012-11-06 14:55:48 +0200235
Derek Jones8ede1a22011-10-05 13:34:52 -0500236$this->input->ip_address()
Andrey Andreev303eef02012-11-06 14:55:48 +0200237==========================
Derek Jones8ede1a22011-10-05 13:34:52 -0500238
239Returns the IP address for the current user. If the IP address is not
Andrey Andreev303eef02012-11-06 14:55:48 +0200240valid, the method will return an IP of: 0.0.0.0
Derek Jones8ede1a22011-10-05 13:34:52 -0500241
242::
243
244 echo $this->input->ip_address();
245
246$this->input->valid_ip($ip)
Andrey Andreev303eef02012-11-06 14:55:48 +0200247===========================
Derek Jones8ede1a22011-10-05 13:34:52 -0500248
249Takes an IP address as input and returns TRUE or FALSE (boolean) if it
Andrey Andreev303eef02012-11-06 14:55:48 +0200250is valid or not.
251
252.. note:: The $this->input->ip_address() method above automatically
253 validates the IP address.
Derek Jones8ede1a22011-10-05 13:34:52 -0500254
255::
256
Derek Jones8831d112011-10-05 15:57:02 -0500257 if ( ! $this->input->valid_ip($ip))
258 {
259 echo 'Not Valid';
260 }
261 else
262 {
263 echo 'Valid';
264 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500265
Andrey Andreev5a257182012-06-10 06:18:14 +0300266Accepts an optional second string parameter of 'ipv4' or 'ipv6' to specify
267an IP format. The default checks for both formats.
268
Derek Jones8ede1a22011-10-05 13:34:52 -0500269$this->input->user_agent()
Andrey Andreev303eef02012-11-06 14:55:48 +0200270==========================
Derek Jones8ede1a22011-10-05 13:34:52 -0500271
272Returns the user agent (web browser) being used by the current user.
273Returns FALSE if it's not available.
274
275::
276
277 echo $this->input->user_agent();
278
279See the :doc:`User Agent Class <user_agent>` for methods which extract
280information from the user agent string.
281
282$this->input->request_headers()
Andrey Andreev303eef02012-11-06 14:55:48 +0200283===============================
Derek Jones8ede1a22011-10-05 13:34:52 -0500284
285Useful if running in a non-Apache environment where
286`apache_request_headers() <http://php.net/apache_request_headers>`_
287will not be supported. Returns an array of headers.
288
289::
290
291 $headers = $this->input->request_headers();
292
Andrey Andreev303eef02012-11-06 14:55:48 +0200293$this->input->get_request_header()
294==================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500295
296Returns a single member of the request headers array.
297
298::
299
300 $this->input->get_request_header('some-header', TRUE);
301
302$this->input->is_ajax_request()
Andrey Andreev303eef02012-11-06 14:55:48 +0200303===============================
Derek Jones8ede1a22011-10-05 13:34:52 -0500304
305Checks to see if the HTTP_X_REQUESTED_WITH server header has been
306set, and returns a boolean response.
307
308$this->input->is_cli_request()
Andrey Andreev303eef02012-11-06 14:55:48 +0200309==============================
Derek Jones8ede1a22011-10-05 13:34:52 -0500310
311Checks to see if the STDIN constant is set, which is a failsafe way to
312see if PHP is being run on the command line.
313
314::
315
316 $this->input->is_cli_request()
317
Andrey Andreev303eef02012-11-06 14:55:48 +0200318$this->input->method()
319======================
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100320
Michiel Vugteveen1e9fb492012-03-07 20:51:25 +0100321Returns the $_SERVER['REQUEST_METHOD'], optional set uppercase or lowercase (default lowercase).
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100322
323::
324
Michiel Vugteveendc900df2012-03-07 20:41:37 +0100325 echo $this->input->method(TRUE); // Outputs: POST
326 echo $this->input->method(FALSE); // Outputs: post
Andrey Andreev303eef02012-11-06 14:55:48 +0200327 echo $this->input->method(); // Outputs: post