blob: bcf117358093c4cd2eb20bca1d05387ca0ad1aa5 [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
21- If $config['allow_get_array'] is FALSE(default is TRUE), destroys
22 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
Derek Jones8831d112011-10-05 15:57:02 -050056 if ( ! isset($_POST['something']))
57 {
58 $something = FALSE;
59 }
60 else
61 {
62 $something = $_POST['something'];
63 }
Derek Jones8ede1a22011-10-05 13:34:52 -050064
65With CodeIgniter's built in functions you can simply do this::
66
67 $something = $this->input->post('something');
68
69The three functions are:
70
71- $this->input->post()
72- $this->input->cookie()
73- $this->input->server()
74
75$this->input->post()
76====================
77
78The first parameter will contain the name of the POST item you are
79looking for::
80
81 $this->input->post('some_data');
82
83The function returns FALSE (boolean) if the item you are attempting to
84retrieve does not exist.
85
86The second optional parameter lets you run the data through the XSS
87filter. It's enabled by setting the second parameter to boolean TRUE;
88
89::
90
91 $this->input->post('some_data', TRUE);
92
93To return an array of all POST items call without any parameters.
94
95To return all POST items and pass them through the XSS filter set the
96first parameter NULL while setting the second parameter to boolean;
97
98The function returns FALSE (boolean) if there are no items in the POST.
99
100::
101
Derek Jones8831d112011-10-05 15:57:02 -0500102 $this->input->post(NULL, TRUE); // returns all POST items with XSS filter
103 $this->input->post(); // returns all POST items without XSS filter
Derek Jones8ede1a22011-10-05 13:34:52 -0500104
105$this->input->get()
106===================
107
108This function is identical to the post function, only it fetches get
109data::
110
111 $this->input->get('some_data', TRUE);
112
113To return an array of all GET items call without any parameters.
114
115To return all GET items and pass them through the XSS filter set the
116first parameter NULL while setting the second parameter to boolean;
117
118The function returns FALSE (boolean) if there are no items in the GET.
119
120::
121
Derek Jones8831d112011-10-05 15:57:02 -0500122 $this->input->get(NULL, TRUE); // returns all GET items with XSS filter
123 $this->input->get(); // returns all GET items without XSS filtering
124
Derek Jones8ede1a22011-10-05 13:34:52 -0500125
126$this->input->get_post()
127=========================
128
129This function will search through both the post and get streams for
130data, looking first in post, and then in get::
131
132 $this->input->get_post('some_data', TRUE);
133
134$this->input->cookie()
135======================
136
137This function is identical to the post function, only it fetches cookie
138data::
139
140 $this->input->cookie('some_data', TRUE);
141
142$this->input->server()
143======================
144
145This function is identical to the above functions, only it fetches
146server data::
147
148 $this->input->server('some_data');
149
150$this->input->set_cookie()
151===========================
152
153Sets a cookie containing the values you specify. There are two ways to
154pass information to this function so that a cookie can be set: Array
155Method, and Discrete Parameters:
156
157Array Method
158^^^^^^^^^^^^
159
160Using this method, an associative array is passed to the first
161parameter::
162
Derek Jones8831d112011-10-05 15:57:02 -0500163 $cookie = array(
164 'name' => 'The Cookie Name',
165 'value' => 'The Value',
166 'expire' => '86500',
167 'domain' => '.some-domain.com',
168 'path' => '/',
169 'prefix' => 'myprefix_',
170 'secure' => TRUE
171 );
172
173 $this->input->set_cookie($cookie);
Derek Jones8ede1a22011-10-05 13:34:52 -0500174
175**Notes:**
176
177Only the name and value are required. To delete a cookie set it with the
178expiration blank.
179
180The expiration is set in **seconds**, which will be added to the current
181time. Do not include the time, but rather only the number of seconds
182from *now* that you wish the cookie to be valid. If the expiration is
183set to zero the cookie will only last as long as the browser is open.
184
185For site-wide cookies regardless of how your site is requested, add your
186URL to the **domain** starting with a period, like this:
187.your-domain.com
188
189The path is usually not needed since the function sets a root path.
190
191The prefix is only needed if you need to avoid name collisions with
192other identically named cookies for your server.
193
194The secure boolean is only needed if you want to make it a secure cookie
195by setting it to TRUE.
196
197Discrete Parameters
198^^^^^^^^^^^^^^^^^^^
199
200If you prefer, you can set the cookie by passing data using individual
201parameters::
202
203 $this->input->set_cookie($name, $value, $expire, $domain, $path, $prefix, $secure);
204
205$this->input->cookie()
206======================
207
208Lets you fetch a cookie. The first parameter will contain the name of
209the cookie you are looking for (including any prefixes)::
210
211 cookie('some_cookie');
212
213The function returns FALSE (boolean) if the item you are attempting to
214retrieve does not exist.
215
216The second optional parameter lets you run the data through the XSS
217filter. It's enabled by setting the second parameter to boolean TRUE;
218
219::
220
221 cookie('some_cookie', TRUE);
222
223
224$this->input->ip_address()
225===========================
226
227Returns the IP address for the current user. If the IP address is not
228valid, the function will return an IP of: 0.0.0.0
229
230::
231
232 echo $this->input->ip_address();
233
234$this->input->valid_ip($ip)
235============================
236
237Takes an IP address as input and returns TRUE or FALSE (boolean) if it
238is valid or not. Note: The $this->input->ip_address() function above
239validates the IP automatically.
240
241::
242
Derek Jones8831d112011-10-05 15:57:02 -0500243 if ( ! $this->input->valid_ip($ip))
244 {
245 echo 'Not Valid';
246 }
247 else
248 {
249 echo 'Valid';
250 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500251
252$this->input->user_agent()
253===========================
254
255Returns the user agent (web browser) being used by the current user.
256Returns FALSE if it's not available.
257
258::
259
260 echo $this->input->user_agent();
261
262See the :doc:`User Agent Class <user_agent>` for methods which extract
263information from the user agent string.
264
265$this->input->request_headers()
266================================
267
268Useful if running in a non-Apache environment where
269`apache_request_headers() <http://php.net/apache_request_headers>`_
270will not be supported. Returns an array of headers.
271
272::
273
274 $headers = $this->input->request_headers();
275
276$this->input->get_request_header();
277=====================================
278
279Returns a single member of the request headers array.
280
281::
282
283 $this->input->get_request_header('some-header', TRUE);
284
285$this->input->is_ajax_request()
286=================================
287
288Checks to see if the HTTP_X_REQUESTED_WITH server header has been
289set, and returns a boolean response.
290
291$this->input->is_cli_request()
292================================
293
294Checks to see if the STDIN constant is set, which is a failsafe way to
295see if PHP is being run on the command line.
296
297::
298
299 $this->input->is_cli_request()
300