blob: c0b9c65897e9bff571df2cb4152b952d0c96f953 [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.
8#. It provides some helper functions for fetching input data and
9 pre-processing it.
10
11.. note:: This class is initialized automatically by the system so there
12 is no need to do it manually.
13
14Security Filtering
15==================
16
17The security filtering function is called automatically when a new
18:doc:`controller <../general/controllers>` is invoked. It does the
19following:
20
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +010021- If $config['allow_get_array'] is FALSE (default is TRUE), destroys
Derek Jones8ede1a22011-10-05 13:34:52 -050022 the global GET array.
23- Destroys all global variables in the event register_globals is
24 turned on.
25- Filters the GET/POST/COOKIE array keys, permitting only alpha-numeric
26 (and a few other) characters.
27- Provides XSS (Cross-site Scripting Hacks) filtering. This can be
28 enabled globally, or upon request.
29- Standardizes newline characters to \\n(In Windows \\r\\n)
30
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
50functions 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
58With CodeIgniter's built in functions you can simply do this::
59
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 Andreev9cf12d12012-06-13 10:19:59 +030077The function returns NULL if the item you are attempting to retrieve
78does 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
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +010092The function 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
102This function is identical to the post function, only it fetches get
103data::
104
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
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100112The function 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
120$this->input->get_post()
121=========================
122
123This function will search through both the post and get streams for
124data, looking first in post, and then in get::
125
126 $this->input->get_post('some_data', TRUE);
127
128$this->input->cookie()
129======================
130
131This function is identical to the post function, only it fetches cookie
132data::
133
Andrey Andreev9cf12d12012-06-13 10:19:59 +0300134 $this->input->cookie('some_cookie');
135 $this->input->cookie('some_cookie, TRUE); // with XSS filter
136
Derek Jones8ede1a22011-10-05 13:34:52 -0500137
138$this->input->server()
139======================
140
141This function is identical to the above functions, only it fetches
142server data::
143
144 $this->input->server('some_data');
145
146$this->input->set_cookie()
147===========================
148
149Sets a cookie containing the values you specify. There are two ways to
150pass information to this function so that a cookie can be set: Array
151Method, and Discrete Parameters:
152
153Array Method
154^^^^^^^^^^^^
155
156Using this method, an associative array is passed to the first
157parameter::
158
Derek Jones8831d112011-10-05 15:57:02 -0500159 $cookie = array(
160 'name' => 'The Cookie Name',
161 'value' => 'The Value',
162 'expire' => '86500',
163 'domain' => '.some-domain.com',
164 'path' => '/',
165 'prefix' => 'myprefix_',
166 'secure' => TRUE
167 );
168
169 $this->input->set_cookie($cookie);
Derek Jones8ede1a22011-10-05 13:34:52 -0500170
171**Notes:**
172
173Only the name and value are required. To delete a cookie set it with the
174expiration blank.
175
176The expiration is set in **seconds**, which will be added to the current
177time. Do not include the time, but rather only the number of seconds
178from *now* that you wish the cookie to be valid. If the expiration is
179set to zero the cookie will only last as long as the browser is open.
180
181For site-wide cookies regardless of how your site is requested, add your
182URL to the **domain** starting with a period, like this:
183.your-domain.com
184
185The path is usually not needed since the function sets a root path.
186
187The prefix is only needed if you need to avoid name collisions with
188other identically named cookies for your server.
189
190The secure boolean is only needed if you want to make it a secure cookie
191by setting it to TRUE.
192
193Discrete Parameters
194^^^^^^^^^^^^^^^^^^^
195
196If you prefer, you can set the cookie by passing data using individual
197parameters::
198
199 $this->input->set_cookie($name, $value, $expire, $domain, $path, $prefix, $secure);
200
Derek Jones8ede1a22011-10-05 13:34:52 -0500201$this->input->ip_address()
202===========================
203
204Returns the IP address for the current user. If the IP address is not
205valid, the function will return an IP of: 0.0.0.0
206
207::
208
209 echo $this->input->ip_address();
210
211$this->input->valid_ip($ip)
212============================
213
214Takes an IP address as input and returns TRUE or FALSE (boolean) if it
215is valid or not. Note: The $this->input->ip_address() function above
216validates the IP automatically.
217
218::
219
Derek Jones8831d112011-10-05 15:57:02 -0500220 if ( ! $this->input->valid_ip($ip))
221 {
222 echo 'Not Valid';
223 }
224 else
225 {
226 echo 'Valid';
227 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500228
Andrey Andreev5a257182012-06-10 06:18:14 +0300229Accepts an optional second string parameter of 'ipv4' or 'ipv6' to specify
230an IP format. The default checks for both formats.
231
Derek Jones8ede1a22011-10-05 13:34:52 -0500232$this->input->user_agent()
233===========================
234
235Returns the user agent (web browser) being used by the current user.
236Returns FALSE if it's not available.
237
238::
239
240 echo $this->input->user_agent();
241
242See the :doc:`User Agent Class <user_agent>` for methods which extract
243information from the user agent string.
244
245$this->input->request_headers()
246================================
247
248Useful if running in a non-Apache environment where
249`apache_request_headers() <http://php.net/apache_request_headers>`_
250will not be supported. Returns an array of headers.
251
252::
253
254 $headers = $this->input->request_headers();
255
256$this->input->get_request_header();
257=====================================
258
259Returns a single member of the request headers array.
260
261::
262
263 $this->input->get_request_header('some-header', TRUE);
264
265$this->input->is_ajax_request()
266=================================
267
268Checks to see if the HTTP_X_REQUESTED_WITH server header has been
269set, and returns a boolean response.
270
271$this->input->is_cli_request()
272================================
273
274Checks to see if the STDIN constant is set, which is a failsafe way to
275see if PHP is being run on the command line.
276
277::
278
279 $this->input->is_cli_request()
280
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100281$this->input->method();
282=====================================
283
Michiel Vugteveen1e9fb492012-03-07 20:51:25 +0100284Returns the $_SERVER['REQUEST_METHOD'], optional set uppercase or lowercase (default lowercase).
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100285
286::
287
Michiel Vugteveendc900df2012-03-07 20:41:37 +0100288 echo $this->input->method(TRUE); // Outputs: POST
289 echo $this->input->method(FALSE); // Outputs: post
290 echo $this->input->method(); // Outputs: post