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