blob: 177f5cb64077872ce1db057872db98fd6c6f42ac [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
Andrey Andreev303eef02012-11-06 14:55:48 +0200101This method is identical to the post method, only it fetches get data
102::
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
119$this->input->get_post()
Andrey Andreev303eef02012-11-06 14:55:48 +0200120========================
Derek Jones8ede1a22011-10-05 13:34:52 -0500121
Andrey Andreev303eef02012-11-06 14:55:48 +0200122This method will search through both the post and get streams for
Derek Jones8ede1a22011-10-05 13:34:52 -0500123data, looking first in post, and then in get::
124
125 $this->input->get_post('some_data', TRUE);
126
127$this->input->cookie()
128======================
129
Andrey Andreev303eef02012-11-06 14:55:48 +0200130This method is identical to the post method, only it fetches cookie data
131::
Derek Jones8ede1a22011-10-05 13:34:52 -0500132
Andrey Andreev9cf12d12012-06-13 10:19:59 +0300133 $this->input->cookie('some_cookie');
134 $this->input->cookie('some_cookie, TRUE); // with XSS filter
135
Derek Jones8ede1a22011-10-05 13:34:52 -0500136
137$this->input->server()
138======================
139
Andrey Andreev303eef02012-11-06 14:55:48 +0200140This method is identical to the above methods, only it fetches server
Derek Jones8ede1a22011-10-05 13:34:52 -0500141server data::
142
143 $this->input->server('some_data');
144
Andrey Andreev303eef02012-11-06 14:55:48 +0200145Using the php://input stream
146============================
147
148If you want to utilize the PUT, DELETE, PATCH or other exotic request
149methods, they can only be accessed via a special input stream, that
150can only be read once. This isn't as easy as just reading from e.g.
151the ``$_POST`` array, because it will always exist and you can try
152and access multiple variables without caring that you might only have
153one shot at all of the POST data.
154
155CodeIgniter will take care of that for you, and you can access data
156from the **php://input** stream at any time, just by calling the
157``input_stream()`` method::
158
159 $this->input->input_stream('key');
160
161Similar to the methods above, if the requested data is not found, it
162will return NULL and you can also decide whether to run the data
163through ``xss_clean()`` by passing a boolean value as the second
164parameter::
165
166 $this->input->input_stream('key', TRUE); // XSS Clean
167 $this->input->input_stream('key', FALSE); // No XSS filter
168
169.. note:: You can utilize method() in order to know if you're reading
170 PUT, DELETE or PATCH data.
171
Derek Jones8ede1a22011-10-05 13:34:52 -0500172$this->input->set_cookie()
Andrey Andreev303eef02012-11-06 14:55:48 +0200173==========================
Derek Jones8ede1a22011-10-05 13:34:52 -0500174
175Sets a cookie containing the values you specify. There are two ways to
Andrey Andreev303eef02012-11-06 14:55:48 +0200176pass information to this method so that a cookie can be set: Array
Derek Jones8ede1a22011-10-05 13:34:52 -0500177Method, and Discrete Parameters:
178
179Array Method
180^^^^^^^^^^^^
181
182Using this method, an associative array is passed to the first
183parameter::
184
Derek Jones8831d112011-10-05 15:57:02 -0500185 $cookie = array(
186 'name' => 'The Cookie Name',
187 'value' => 'The Value',
188 'expire' => '86500',
189 'domain' => '.some-domain.com',
190 'path' => '/',
191 'prefix' => 'myprefix_',
192 'secure' => TRUE
193 );
194
195 $this->input->set_cookie($cookie);
Derek Jones8ede1a22011-10-05 13:34:52 -0500196
197**Notes:**
198
199Only the name and value are required. To delete a cookie set it with the
200expiration blank.
201
202The expiration is set in **seconds**, which will be added to the current
203time. Do not include the time, but rather only the number of seconds
204from *now* that you wish the cookie to be valid. If the expiration is
205set to zero the cookie will only last as long as the browser is open.
206
207For site-wide cookies regardless of how your site is requested, add your
208URL to the **domain** starting with a period, like this:
209.your-domain.com
210
Andrey Andreev303eef02012-11-06 14:55:48 +0200211The path is usually not needed since the method sets a root path.
Derek Jones8ede1a22011-10-05 13:34:52 -0500212
213The prefix is only needed if you need to avoid name collisions with
214other identically named cookies for your server.
215
216The secure boolean is only needed if you want to make it a secure cookie
217by setting it to TRUE.
218
219Discrete Parameters
220^^^^^^^^^^^^^^^^^^^
221
222If you prefer, you can set the cookie by passing data using individual
223parameters::
224
225 $this->input->set_cookie($name, $value, $expire, $domain, $path, $prefix, $secure);
226
Andrey Andreev303eef02012-11-06 14:55:48 +0200227
Derek Jones8ede1a22011-10-05 13:34:52 -0500228$this->input->ip_address()
Andrey Andreev303eef02012-11-06 14:55:48 +0200229==========================
Derek Jones8ede1a22011-10-05 13:34:52 -0500230
231Returns the IP address for the current user. If the IP address is not
Andrey Andreev303eef02012-11-06 14:55:48 +0200232valid, the method will return an IP of: 0.0.0.0
Derek Jones8ede1a22011-10-05 13:34:52 -0500233
234::
235
236 echo $this->input->ip_address();
237
238$this->input->valid_ip($ip)
Andrey Andreev303eef02012-11-06 14:55:48 +0200239===========================
Derek Jones8ede1a22011-10-05 13:34:52 -0500240
241Takes an IP address as input and returns TRUE or FALSE (boolean) if it
Andrey Andreev303eef02012-11-06 14:55:48 +0200242is valid or not.
243
244.. note:: The $this->input->ip_address() method above automatically
245 validates the IP address.
Derek Jones8ede1a22011-10-05 13:34:52 -0500246
247::
248
Derek Jones8831d112011-10-05 15:57:02 -0500249 if ( ! $this->input->valid_ip($ip))
250 {
251 echo 'Not Valid';
252 }
253 else
254 {
255 echo 'Valid';
256 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500257
Andrey Andreev5a257182012-06-10 06:18:14 +0300258Accepts an optional second string parameter of 'ipv4' or 'ipv6' to specify
259an IP format. The default checks for both formats.
260
Derek Jones8ede1a22011-10-05 13:34:52 -0500261$this->input->user_agent()
Andrey Andreev303eef02012-11-06 14:55:48 +0200262==========================
Derek Jones8ede1a22011-10-05 13:34:52 -0500263
264Returns the user agent (web browser) being used by the current user.
265Returns FALSE if it's not available.
266
267::
268
269 echo $this->input->user_agent();
270
271See the :doc:`User Agent Class <user_agent>` for methods which extract
272information from the user agent string.
273
274$this->input->request_headers()
Andrey Andreev303eef02012-11-06 14:55:48 +0200275===============================
Derek Jones8ede1a22011-10-05 13:34:52 -0500276
277Useful if running in a non-Apache environment where
278`apache_request_headers() <http://php.net/apache_request_headers>`_
279will not be supported. Returns an array of headers.
280
281::
282
283 $headers = $this->input->request_headers();
284
Andrey Andreev303eef02012-11-06 14:55:48 +0200285$this->input->get_request_header()
286==================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500287
288Returns a single member of the request headers array.
289
290::
291
292 $this->input->get_request_header('some-header', TRUE);
293
294$this->input->is_ajax_request()
Andrey Andreev303eef02012-11-06 14:55:48 +0200295===============================
Derek Jones8ede1a22011-10-05 13:34:52 -0500296
297Checks to see if the HTTP_X_REQUESTED_WITH server header has been
298set, and returns a boolean response.
299
300$this->input->is_cli_request()
Andrey Andreev303eef02012-11-06 14:55:48 +0200301==============================
Derek Jones8ede1a22011-10-05 13:34:52 -0500302
303Checks to see if the STDIN constant is set, which is a failsafe way to
304see if PHP is being run on the command line.
305
306::
307
308 $this->input->is_cli_request()
309
Andrey Andreev303eef02012-11-06 14:55:48 +0200310$this->input->method()
311======================
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100312
Michiel Vugteveen1e9fb492012-03-07 20:51:25 +0100313Returns the $_SERVER['REQUEST_METHOD'], optional set uppercase or lowercase (default lowercase).
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100314
315::
316
Michiel Vugteveendc900df2012-03-07 20:41:37 +0100317 echo $this->input->method(TRUE); // Outputs: POST
318 echo $this->input->method(FALSE); // Outputs: post
Andrey Andreev303eef02012-11-06 14:55:48 +0200319 echo $this->input->method(); // Outputs: post