blob: 7f995f0507b6cb1d536f0368a86649fa2622d069 [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
45Using POST, COOKIE, or SERVER Data
46==================================
47
48CodeIgniter comes with three helper functions that let you fetch POST,
49COOKIE or SERVER items. The main advantage of using the provided
50functions rather than fetching an item directly ($_POST['something'])
51is that the functions will check to see if the item is set and return
52false (boolean) if not. This lets you conveniently use data without
53having 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
62The three functions are:
63
64- $this->input->post()
65- $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
76The function returns FALSE (boolean) if the item you are attempting to
77retrieve does not exist.
78
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
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +010091The function 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
101This function is identical to the post function, only it fetches get
102data::
103
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
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100111The function 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()
120=========================
121
122This function will search through both the post and get streams for
123data, looking first in post, and then in get::
124
125 $this->input->get_post('some_data', TRUE);
126
127$this->input->cookie()
128======================
129
130This function is identical to the post function, only it fetches cookie
131data::
132
133 $this->input->cookie('some_data', TRUE);
134
135$this->input->server()
136======================
137
138This function is identical to the above functions, only it fetches
139server data::
140
141 $this->input->server('some_data');
142
143$this->input->set_cookie()
144===========================
145
146Sets a cookie containing the values you specify. There are two ways to
147pass information to this function so that a cookie can be set: Array
148Method, and Discrete Parameters:
149
150Array Method
151^^^^^^^^^^^^
152
153Using this method, an associative array is passed to the first
154parameter::
155
Derek Jones8831d112011-10-05 15:57:02 -0500156 $cookie = array(
157 'name' => 'The Cookie Name',
158 'value' => 'The Value',
159 'expire' => '86500',
160 'domain' => '.some-domain.com',
161 'path' => '/',
162 'prefix' => 'myprefix_',
163 'secure' => TRUE
164 );
165
166 $this->input->set_cookie($cookie);
Derek Jones8ede1a22011-10-05 13:34:52 -0500167
168**Notes:**
169
170Only the name and value are required. To delete a cookie set it with the
171expiration blank.
172
173The expiration is set in **seconds**, which will be added to the current
174time. Do not include the time, but rather only the number of seconds
175from *now* that you wish the cookie to be valid. If the expiration is
176set to zero the cookie will only last as long as the browser is open.
177
178For site-wide cookies regardless of how your site is requested, add your
179URL to the **domain** starting with a period, like this:
180.your-domain.com
181
182The path is usually not needed since the function sets a root path.
183
184The prefix is only needed if you need to avoid name collisions with
185other identically named cookies for your server.
186
187The secure boolean is only needed if you want to make it a secure cookie
188by setting it to TRUE.
189
190Discrete Parameters
191^^^^^^^^^^^^^^^^^^^
192
193If you prefer, you can set the cookie by passing data using individual
194parameters::
195
196 $this->input->set_cookie($name, $value, $expire, $domain, $path, $prefix, $secure);
197
198$this->input->cookie()
199======================
200
201Lets you fetch a cookie. The first parameter will contain the name of
202the cookie you are looking for (including any prefixes)::
203
204 cookie('some_cookie');
205
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100206The function returns NULL if the item you are attempting to
Derek Jones8ede1a22011-10-05 13:34:52 -0500207retrieve does not exist.
208
209The second optional parameter lets you run the data through the XSS
210filter. It's enabled by setting the second parameter to boolean TRUE;
211
212::
213
214 cookie('some_cookie', TRUE);
215
216
217$this->input->ip_address()
218===========================
219
220Returns the IP address for the current user. If the IP address is not
221valid, the function will return an IP of: 0.0.0.0
222
223::
224
225 echo $this->input->ip_address();
226
227$this->input->valid_ip($ip)
228============================
229
230Takes an IP address as input and returns TRUE or FALSE (boolean) if it
231is valid or not. Note: The $this->input->ip_address() function above
232validates the IP automatically.
233
234::
235
Derek Jones8831d112011-10-05 15:57:02 -0500236 if ( ! $this->input->valid_ip($ip))
237 {
238 echo 'Not Valid';
239 }
240 else
241 {
242 echo 'Valid';
243 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500244
Andrey Andreev5a257182012-06-10 06:18:14 +0300245Accepts an optional second string parameter of 'ipv4' or 'ipv6' to specify
246an IP format. The default checks for both formats.
247
Derek Jones8ede1a22011-10-05 13:34:52 -0500248$this->input->user_agent()
249===========================
250
251Returns the user agent (web browser) being used by the current user.
252Returns FALSE if it's not available.
253
254::
255
256 echo $this->input->user_agent();
257
258See the :doc:`User Agent Class <user_agent>` for methods which extract
259information from the user agent string.
260
261$this->input->request_headers()
262================================
263
264Useful if running in a non-Apache environment where
265`apache_request_headers() <http://php.net/apache_request_headers>`_
266will not be supported. Returns an array of headers.
267
268::
269
270 $headers = $this->input->request_headers();
271
272$this->input->get_request_header();
273=====================================
274
275Returns a single member of the request headers array.
276
277::
278
279 $this->input->get_request_header('some-header', TRUE);
280
281$this->input->is_ajax_request()
282=================================
283
284Checks to see if the HTTP_X_REQUESTED_WITH server header has been
285set, and returns a boolean response.
286
287$this->input->is_cli_request()
288================================
289
290Checks to see if the STDIN constant is set, which is a failsafe way to
291see if PHP is being run on the command line.
292
293::
294
295 $this->input->is_cli_request()
296
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100297$this->input->method();
298=====================================
299
Michiel Vugteveen1e9fb492012-03-07 20:51:25 +0100300Returns the $_SERVER['REQUEST_METHOD'], optional set uppercase or lowercase (default lowercase).
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100301
302::
303
Michiel Vugteveendc900df2012-03-07 20:41:37 +0100304 echo $this->input->method(TRUE); // Outputs: POST
305 echo $this->input->method(FALSE); // Outputs: post
306 echo $this->input->method(); // Outputs: post