Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ########### |
| 2 | Input Class |
| 3 | ########### |
| 4 | |
| 5 | The Input Class serves two purposes: |
| 6 | |
| 7 | #. It pre-processes global input data for security. |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 8 | #. It provides some helper methods for fetching input data and pre-processing it. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 9 | |
| 10 | .. note:: This class is initialized automatically by the system so there |
| 11 | is no need to do it manually. |
| 12 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 13 | .. contents:: |
| 14 | :local: |
| 15 | |
| 16 | .. raw:: html |
| 17 | |
| 18 | <div class="custom-index container"></div> |
| 19 | |
James L Parry | 08191be | 2014-12-20 02:37:13 -0800 | [diff] [blame] | 20 | *************** |
| 21 | Input Filtering |
| 22 | *************** |
| 23 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 24 | Security Filtering |
| 25 | ================== |
| 26 | |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 27 | The security filtering method is called automatically when a new |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 28 | :doc:`controller <../general/controllers>` is invoked. It does the |
| 29 | following: |
| 30 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 31 | - If ``$config['allow_get_array']`` is FALSE (default is TRUE), destroys |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 32 | the global GET array. |
| 33 | - Destroys all global variables in the event register_globals is |
| 34 | turned on. |
| 35 | - Filters the GET/POST/COOKIE array keys, permitting only alpha-numeric |
| 36 | (and a few other) characters. |
| 37 | - Provides XSS (Cross-site Scripting Hacks) filtering. This can be |
| 38 | enabled globally, or upon request. |
Andrey Andreev | bfb635b | 2014-01-08 18:32:05 +0200 | [diff] [blame] | 39 | - Standardizes newline characters to ``PHP_EOL`` (\\n in UNIX-based OSes, |
| 40 | \\r\\n under Windows). This is configurable. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 41 | |
| 42 | XSS Filtering |
| 43 | ============= |
| 44 | |
| 45 | The Input class has the ability to filter input automatically to prevent |
| 46 | cross-site scripting attacks. If you want the filter to run |
| 47 | automatically every time it encounters POST or COOKIE data you can |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 48 | enable it by opening your *application/config/config.php* file and setting |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 49 | this:: |
| 50 | |
| 51 | $config['global_xss_filtering'] = TRUE; |
| 52 | |
| 53 | Please refer to the :doc:`Security class <security>` documentation for |
| 54 | information on using XSS Filtering in your application. |
| 55 | |
Andrey Andreev | 9187ed3 | 2015-02-28 19:54:17 +0200 | [diff] [blame] | 56 | .. important:: The 'global_xss_filtering' setting is DEPRECATED and kept |
| 57 | solely for backwards-compatibility purposes. XSS escaping should |
| 58 | be performed on *output*, not *input*! |
| 59 | |
James L Parry | 08191be | 2014-12-20 02:37:13 -0800 | [diff] [blame] | 60 | ******************* |
| 61 | Accessing form data |
| 62 | ******************* |
| 63 | |
Andrey Andreev | 9cf12d1 | 2012-06-13 10:19:59 +0300 | [diff] [blame] | 64 | Using POST, GET, COOKIE, or SERVER Data |
| 65 | ======================================= |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 66 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 67 | CodeIgniter comes with helper methods that let you fetch POST, GET, |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 68 | COOKIE or SERVER items. The main advantage of using the provided |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 69 | methods rather than fetching an item directly (``$_POST['something']``) |
Andrey Andreev | 9cf12d1 | 2012-06-13 10:19:59 +0300 | [diff] [blame] | 70 | is that the methods will check to see if the item is set and return |
| 71 | NULL if not. This lets you conveniently use data without |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 72 | having to test whether an item exists first. In other words, normally |
| 73 | you might do something like this:: |
| 74 | |
Phil Sturgeon | 55a6ddb | 2012-05-23 18:37:24 +0100 | [diff] [blame] | 75 | $something = isset($_POST['something']) ? $_POST['something'] : NULL; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 76 | |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 77 | With CodeIgniter's built in methods you can simply do this:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 78 | |
| 79 | $something = $this->input->post('something'); |
| 80 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 81 | The main methods are: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 82 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 83 | - ``$this->input->post()`` |
| 84 | - ``$this->input->get()`` |
| 85 | - ``$this->input->cookie()`` |
| 86 | - ``$this->input->server()`` |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 87 | |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 88 | Using the php://input stream |
| 89 | ============================ |
| 90 | |
| 91 | If you want to utilize the PUT, DELETE, PATCH or other exotic request |
| 92 | methods, they can only be accessed via a special input stream, that |
| 93 | can only be read once. This isn't as easy as just reading from e.g. |
| 94 | the ``$_POST`` array, because it will always exist and you can try |
| 95 | and access multiple variables without caring that you might only have |
| 96 | one shot at all of the POST data. |
| 97 | |
Ignasimg | 0b5569f | 2015-02-20 17:56:55 +0100 | [diff] [blame] | 98 | CodeIgniter will take care of that for you, and you can read the data |
| 99 | from the **php://input** stream at any time, just by using the |
Ignasimg | 54b42d6 | 2015-02-26 03:16:12 +0100 | [diff] [blame] | 100 | ``$raw_input_stream`` property:: |
Ignasimg | 0b5569f | 2015-02-20 17:56:55 +0100 | [diff] [blame] | 101 | |
| 102 | $this->input->raw_input_stream; |
| 103 | |
Ignasimg | 54b42d6 | 2015-02-26 03:16:12 +0100 | [diff] [blame] | 104 | Additionally if the input stream is form-encoded like $_POST you can |
| 105 | access its values by calling the |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 106 | ``input_stream()`` method:: |
| 107 | |
| 108 | $this->input->input_stream('key'); |
| 109 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 110 | Similar to other methods such as ``get()`` and ``post()``, if the |
| 111 | requested data is not found, it will return NULL and you can also |
| 112 | decide whether to run the data through ``xss_clean()`` by passing |
| 113 | a boolean value as the second parameter:: |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 114 | |
| 115 | $this->input->input_stream('key', TRUE); // XSS Clean |
| 116 | $this->input->input_stream('key', FALSE); // No XSS filter |
| 117 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 118 | .. note:: You can utilize ``method()`` in order to know if you're reading |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 119 | PUT, DELETE or PATCH data. |
| 120 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 121 | *************** |
| 122 | Class Reference |
| 123 | *************** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 124 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 125 | .. php:class:: CI_Input |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 126 | |
Ignasimg | 54b42d6 | 2015-02-26 03:16:12 +0100 | [diff] [blame] | 127 | .. attribute:: $raw_input_stream |
| 128 | |
| 129 | Read only property that will return php://input data as is. |
| 130 | |
| 131 | The property can be read multiple times. |
| 132 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 133 | .. php:method:: post([$index = NULL[, $xss_clean = NULL]]) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 134 | |
Ahmad Anbar | ff89a4e | 2014-12-02 17:26:30 +0200 | [diff] [blame] | 135 | :param mixed $index: POST parameter name |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 136 | :param bool $xss_clean: Whether to apply XSS filtering |
| 137 | :returns: $_POST if no parameters supplied, otherwise the POST value if found or NULL if not |
| 138 | :rtype: mixed |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 139 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 140 | The first parameter will contain the name of the POST item you are |
| 141 | looking for:: |
Derek Jones | 8831d11 | 2011-10-05 15:57:02 -0500 | [diff] [blame] | 142 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 143 | $this->input->post('some_data'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 144 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 145 | The method returns NULL if the item you are attempting to retrieve |
| 146 | does not exist. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 147 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 148 | The second optional parameter lets you run the data through the XSS |
Andrey Andreev | 88ebdf7 | 2014-01-08 17:28:02 +0200 | [diff] [blame] | 149 | filter. It's enabled by setting the second parameter to boolean TRUE |
| 150 | or by setting your ``$config['global_xss_filtering']`` to TRUE. |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 151 | :: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 152 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 153 | $this->input->post('some_data', TRUE); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 154 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 155 | To return an array of all POST items call without any parameters. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 156 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 157 | To return all POST items and pass them through the XSS filter set the |
| 158 | first parameter NULL while setting the second parameter to boolean TRUE. |
| 159 | :: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 160 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 161 | $this->input->post(NULL, TRUE); // returns all POST items with XSS filter |
Andrey Andreev | 88ebdf7 | 2014-01-08 17:28:02 +0200 | [diff] [blame] | 162 | $this->input->post(NULL, FALSE); // returns all POST items without XSS filter |
David Wosnitza | d31a4e6 | 2014-12-12 16:35:35 +0100 | [diff] [blame] | 163 | |
Ahmad Anbar | ff89a4e | 2014-12-02 17:26:30 +0200 | [diff] [blame] | 164 | To return an array of multiple POST parameters, pass all the required keys |
| 165 | as an array. |
| 166 | :: |
David Wosnitza | d31a4e6 | 2014-12-12 16:35:35 +0100 | [diff] [blame] | 167 | |
Ahmad Anbar | ff89a4e | 2014-12-02 17:26:30 +0200 | [diff] [blame] | 168 | $this->input->post(array('field1', 'field2')); |
David Wosnitza | d31a4e6 | 2014-12-12 16:35:35 +0100 | [diff] [blame] | 169 | |
Andrey Andreev | 71d8f72 | 2017-01-17 12:01:00 +0200 | [diff] [blame] | 170 | Same rule applied here, to retrieve the parameters with XSS filtering enabled, set the |
Ahmad Anbar | ff89a4e | 2014-12-02 17:26:30 +0200 | [diff] [blame] | 171 | second parameter to boolean TRUE. |
| 172 | :: |
David Wosnitza | d31a4e6 | 2014-12-12 16:35:35 +0100 | [diff] [blame] | 173 | |
Ahmad Anbar | ff89a4e | 2014-12-02 17:26:30 +0200 | [diff] [blame] | 174 | $this->input->post(array('field1', 'field2'), TRUE); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 175 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 176 | .. php:method:: get([$index = NULL[, $xss_clean = NULL]]) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 177 | |
Ahmad Anbar | ff89a4e | 2014-12-02 17:26:30 +0200 | [diff] [blame] | 178 | :param mixed $index: GET parameter name |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 179 | :param bool $xss_clean: Whether to apply XSS filtering |
| 180 | :returns: $_GET if no parameters supplied, otherwise the GET value if found or NULL if not |
| 181 | :rtype: mixed |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 182 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 183 | This method is identical to ``post()``, only it fetches GET data. |
| 184 | :: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 185 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 186 | $this->input->get('some_data', TRUE); |
| 187 | |
| 188 | To return an array of all GET items call without any parameters. |
| 189 | |
| 190 | To return all GET items and pass them through the XSS filter set the |
| 191 | first parameter NULL while setting the second parameter to boolean TRUE. |
| 192 | :: |
| 193 | |
| 194 | $this->input->get(NULL, TRUE); // returns all GET items with XSS filter |
Andrey Andreev | 88ebdf7 | 2014-01-08 17:28:02 +0200 | [diff] [blame] | 195 | $this->input->get(NULL, FALSE); // returns all GET items without XSS filtering |
David Wosnitza | d31a4e6 | 2014-12-12 16:35:35 +0100 | [diff] [blame] | 196 | |
Ahmad Anbar | ff89a4e | 2014-12-02 17:26:30 +0200 | [diff] [blame] | 197 | To return an array of multiple GET parameters, pass all the required keys |
| 198 | as an array. |
| 199 | :: |
David Wosnitza | d31a4e6 | 2014-12-12 16:35:35 +0100 | [diff] [blame] | 200 | |
Ahmad Anbar | ff89a4e | 2014-12-02 17:26:30 +0200 | [diff] [blame] | 201 | $this->input->get(array('field1', 'field2')); |
David Wosnitza | d31a4e6 | 2014-12-12 16:35:35 +0100 | [diff] [blame] | 202 | |
Andrey Andreev | 71d8f72 | 2017-01-17 12:01:00 +0200 | [diff] [blame] | 203 | Same rule applied here, to retrieve the parameters with XSS filtering enabled, set the |
Ahmad Anbar | ff89a4e | 2014-12-02 17:26:30 +0200 | [diff] [blame] | 204 | second parameter to boolean TRUE. |
| 205 | :: |
David Wosnitza | d31a4e6 | 2014-12-12 16:35:35 +0100 | [diff] [blame] | 206 | |
Ahmad Anbar | ff89a4e | 2014-12-02 17:26:30 +0200 | [diff] [blame] | 207 | $this->input->get(array('field1', 'field2'), TRUE); |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 208 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 209 | .. php:method:: post_get($index[, $xss_clean = NULL]) |
Andrey Andreev | ea801ab | 2014-01-20 15:03:43 +0200 | [diff] [blame] | 210 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 211 | :param string $index: POST/GET parameter name |
| 212 | :param bool $xss_clean: Whether to apply XSS filtering |
| 213 | :returns: POST/GET value if found, NULL if not |
| 214 | :rtype: mixed |
Andrey Andreev | ea801ab | 2014-01-20 15:03:43 +0200 | [diff] [blame] | 215 | |
Andrey Andreev | 7c60b12 | 2014-02-08 18:47:19 +0200 | [diff] [blame] | 216 | This method works pretty much the same way as ``post()`` and ``get()``, |
| 217 | only combined. It will search through both POST and GET streams for data, |
| 218 | looking in POST first, and then in GET:: |
Andrey Andreev | ea801ab | 2014-01-20 15:03:43 +0200 | [diff] [blame] | 219 | |
| 220 | $this->input->post_get('some_data', TRUE); |
| 221 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 222 | .. php:method:: get_post($index[, $xss_clean = NULL]) |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 223 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 224 | :param string $index: GET/POST parameter name |
| 225 | :param bool $xss_clean: Whether to apply XSS filtering |
| 226 | :returns: GET/POST value if found, NULL if not |
| 227 | :rtype: mixed |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 228 | |
Andrey Andreev | ea801ab | 2014-01-20 15:03:43 +0200 | [diff] [blame] | 229 | This method works the same way as ``post_get()`` only it looks for GET |
| 230 | data first. |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 231 | |
| 232 | $this->input->get_post('some_data', TRUE); |
| 233 | |
Andrey Andreev | ea801ab | 2014-01-20 15:03:43 +0200 | [diff] [blame] | 234 | .. note:: This method used to act EXACTLY like ``post_get()``, but it's |
| 235 | behavior has changed in CodeIgniter 3.0. |
| 236 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 237 | .. php:method:: cookie([$index = NULL[, $xss_clean = NULL]]) |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 238 | |
Andrey Andreev | ef29f83 | 2014-12-02 18:03:47 +0200 | [diff] [blame] | 239 | :param mixed $index: COOKIE name |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 240 | :param bool $xss_clean: Whether to apply XSS filtering |
| 241 | :returns: $_COOKIE if no parameters supplied, otherwise the COOKIE value if found or NULL if not |
| 242 | :rtype: mixed |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 243 | |
| 244 | This method is identical to ``post()`` and ``get()``, only it fetches cookie |
| 245 | data:: |
| 246 | |
| 247 | $this->input->cookie('some_cookie'); |
Andrey Andreev | 954c4aa | 2017-07-21 11:51:37 +0300 | [diff] [blame] | 248 | $this->input->cookie('some_cookie', TRUE); // with XSS filter |
David Wosnitza | d31a4e6 | 2014-12-12 16:35:35 +0100 | [diff] [blame] | 249 | |
Andrey Andreev | ef29f83 | 2014-12-02 18:03:47 +0200 | [diff] [blame] | 250 | To return an array of multiple cookie values, pass all the required keys |
Ahmad Anbar | ff89a4e | 2014-12-02 17:26:30 +0200 | [diff] [blame] | 251 | as an array. |
| 252 | :: |
David Wosnitza | d31a4e6 | 2014-12-12 16:35:35 +0100 | [diff] [blame] | 253 | |
Ahmad Anbar | ff89a4e | 2014-12-02 17:26:30 +0200 | [diff] [blame] | 254 | $this->input->cookie(array('some_cookie', 'some_cookie2')); |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 255 | |
Andrey Andreev | b4834cc | 2015-01-29 13:52:44 +0200 | [diff] [blame] | 256 | .. note:: Unlike the :doc:`Cookie Helper <../helpers/cookie_helper>` |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 257 | function :php:func:`get_cookie()`, this method does NOT prepend |
Andrey Andreev | b4834cc | 2015-01-29 13:52:44 +0200 | [diff] [blame] | 258 | your configured ``$config['cookie_prefix']`` value. |
| 259 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 260 | .. php:method:: server($index[, $xss_clean = NULL]) |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 261 | |
Ahmad Anbar | ff89a4e | 2014-12-02 17:26:30 +0200 | [diff] [blame] | 262 | :param mixed $index: Value name |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 263 | :param bool $xss_clean: Whether to apply XSS filtering |
| 264 | :returns: $_SERVER item value if found, NULL if not |
| 265 | :rtype: mixed |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 266 | |
Andrey Andreev | 7c60b12 | 2014-02-08 18:47:19 +0200 | [diff] [blame] | 267 | This method is identical to the ``post()``, ``get()`` and ``cookie()`` |
| 268 | methods, only it fetches server data (``$_SERVER``):: |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 269 | |
| 270 | $this->input->server('some_data'); |
| 271 | |
Andrey Andreev | ef29f83 | 2014-12-02 18:03:47 +0200 | [diff] [blame] | 272 | To return an array of multiple ``$_SERVER`` values, pass all the required keys |
Ahmad Anbar | ff89a4e | 2014-12-02 17:26:30 +0200 | [diff] [blame] | 273 | as an array. |
| 274 | :: |
David Wosnitza | d31a4e6 | 2014-12-12 16:35:35 +0100 | [diff] [blame] | 275 | |
| 276 | $this->input->server(array('SERVER_PROTOCOL', 'REQUEST_URI')); |
Ahmad Anbar | ff89a4e | 2014-12-02 17:26:30 +0200 | [diff] [blame] | 277 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 278 | .. php:method:: input_stream([$index = NULL[, $xss_clean = NULL]]) |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 279 | |
Ahmad Anbar | ff89a4e | 2014-12-02 17:26:30 +0200 | [diff] [blame] | 280 | :param mixed $index: Key name |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 281 | :param bool $xss_clean: Whether to apply XSS filtering |
| 282 | :returns: Input stream array if no parameters supplied, otherwise the specified value if found or NULL if not |
| 283 | :rtype: mixed |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 284 | |
| 285 | This method is identical to ``get()``, ``post()`` and ``cookie()``, |
| 286 | only it fetches the *php://input* stream data. |
| 287 | |
Andrey Andreev | 422b889 | 2017-02-01 14:36:49 +0200 | [diff] [blame] | 288 | .. php:method:: set_cookie($name = ''[, $value = ''[, $expire = ''[, $domain = ''[, $path = '/'[, $prefix = ''[, $secure = NULL[, $httponly = NULL]]]]]]]) |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 289 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 290 | :param mixed $name: Cookie name or an array of parameters |
| 291 | :param string $value: Cookie value |
| 292 | :param int $expire: Cookie expiration time in seconds |
| 293 | :param string $domain: Cookie domain |
| 294 | :param string $path: Cookie path |
| 295 | :param string $prefix: Cookie name prefix |
| 296 | :param bool $secure: Whether to only transfer the cookie through HTTPS |
| 297 | :param bool $httponly: Whether to only make the cookie accessible for HTTP requests (no JavaScript) |
| 298 | :rtype: void |
| 299 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 300 | |
| 301 | Sets a cookie containing the values you specify. There are two ways to |
| 302 | pass information to this method so that a cookie can be set: Array |
| 303 | Method, and Discrete Parameters: |
| 304 | |
Connor Tumbleson | 75b3fb2 | 2014-01-11 06:58:43 -0600 | [diff] [blame] | 305 | **Array Method** |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 306 | |
| 307 | Using this method, an associative array is passed to the first |
| 308 | parameter:: |
| 309 | |
| 310 | $cookie = array( |
| 311 | 'name' => 'The Cookie Name', |
| 312 | 'value' => 'The Value', |
| 313 | 'expire' => '86500', |
| 314 | 'domain' => '.some-domain.com', |
| 315 | 'path' => '/', |
| 316 | 'prefix' => 'myprefix_', |
| 317 | 'secure' => TRUE |
| 318 | ); |
| 319 | |
| 320 | $this->input->set_cookie($cookie); |
| 321 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 322 | **Notes** |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 323 | |
| 324 | Only the name and value are required. To delete a cookie set it with the |
| 325 | expiration blank. |
| 326 | |
| 327 | The expiration is set in **seconds**, which will be added to the current |
| 328 | time. Do not include the time, but rather only the number of seconds |
| 329 | from *now* that you wish the cookie to be valid. If the expiration is |
| 330 | set to zero the cookie will only last as long as the browser is open. |
| 331 | |
| 332 | For site-wide cookies regardless of how your site is requested, add your |
| 333 | URL to the **domain** starting with a period, like this: |
| 334 | .your-domain.com |
| 335 | |
| 336 | The path is usually not needed since the method sets a root path. |
| 337 | |
| 338 | The prefix is only needed if you need to avoid name collisions with |
| 339 | other identically named cookies for your server. |
| 340 | |
Andrey Andreev | 422b889 | 2017-02-01 14:36:49 +0200 | [diff] [blame] | 341 | The *httponly* and *secure* flags, when omitted, will default to your |
| 342 | ``$config['cookie_httponly']`` and ``$config['cookie_secure']`` settings. |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 343 | |
Connor Tumbleson | 75b3fb2 | 2014-01-11 06:58:43 -0600 | [diff] [blame] | 344 | **Discrete Parameters** |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 345 | |
| 346 | If you prefer, you can set the cookie by passing data using individual |
| 347 | parameters:: |
| 348 | |
| 349 | $this->input->set_cookie($name, $value, $expire, $domain, $path, $prefix, $secure); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 350 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 351 | .. php:method:: ip_address() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 352 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 353 | :returns: Visitor's IP address or '0.0.0.0' if not valid |
| 354 | :rtype: string |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 355 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 356 | Returns the IP address for the current user. If the IP address is not |
| 357 | valid, the method will return '0.0.0.0':: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 358 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 359 | echo $this->input->ip_address(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 360 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 361 | .. important:: This method takes into account the ``$config['proxy_ips']`` |
| 362 | setting and will return the reported HTTP_X_FORWARDED_FOR, |
| 363 | HTTP_CLIENT_IP, HTTP_X_CLIENT_IP or HTTP_X_CLUSTER_CLIENT_IP |
| 364 | address for the allowed IP addresses. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 365 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 366 | .. php:method:: valid_ip($ip[, $which = '']) |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 367 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 368 | :param string $ip: IP address |
| 369 | :param string $which: IP protocol ('ipv4' or 'ipv6') |
| 370 | :returns: TRUE if the address is valid, FALSE if not |
| 371 | :rtype: bool |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 372 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 373 | Takes an IP address as input and returns TRUE or FALSE (boolean) depending |
| 374 | on whether it is valid or not. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 375 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 376 | .. note:: The $this->input->ip_address() method above automatically |
| 377 | validates the IP address. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 378 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 379 | :: |
Andrey Andreev | 5a25718 | 2012-06-10 06:18:14 +0300 | [diff] [blame] | 380 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 381 | if ( ! $this->input->valid_ip($ip)) |
| 382 | { |
| 383 | echo 'Not Valid'; |
| 384 | } |
| 385 | else |
| 386 | { |
| 387 | echo 'Valid'; |
| 388 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 389 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 390 | Accepts an optional second string parameter of 'ipv4' or 'ipv6' to specify |
| 391 | an IP format. The default checks for both formats. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 392 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 393 | .. php:method:: user_agent([$xss_clean = NULL]) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 394 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 395 | :returns: User agent string or NULL if not set |
Andrey Andreev | 8850e37 | 2014-02-27 21:56:06 +0200 | [diff] [blame] | 396 | :param bool $xss_clean: Whether to apply XSS filtering |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 397 | :rtype: mixed |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 398 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 399 | Returns the user agent string (web browser) being used by the current user, |
| 400 | or NULL if it's not available. |
| 401 | :: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 402 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 403 | echo $this->input->user_agent(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 404 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 405 | See the :doc:`User Agent Class <user_agent>` for methods which extract |
| 406 | information from the user agent string. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 407 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 408 | .. php:method:: request_headers([$xss_clean = FALSE]) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 409 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 410 | :param bool $xss_clean: Whether to apply XSS filtering |
| 411 | :returns: An array of HTTP request headers |
| 412 | :rtype: array |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 413 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 414 | Returns an array of HTTP request headers. |
| 415 | Useful if running in a non-Apache environment where |
| 416 | `apache_request_headers() <http://php.net/apache_request_headers>`_ |
| 417 | will not be supported. |
| 418 | :: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 419 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 420 | $headers = $this->input->request_headers(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 421 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 422 | .. php:method:: get_request_header($index[, $xss_clean = FALSE]) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 423 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 424 | :param string $index: HTTP request header name |
| 425 | :param bool $xss_clean: Whether to apply XSS filtering |
| 426 | :returns: An HTTP request header or NULL if not found |
| 427 | :rtype: string |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 428 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 429 | Returns a single member of the request headers array or NULL |
| 430 | if the searched header is not found. |
| 431 | :: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 432 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 433 | $this->input->get_request_header('some-header', TRUE); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 434 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 435 | .. php:method:: is_ajax_request() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 436 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 437 | :returns: TRUE if it is an Ajax request, FALSE if not |
| 438 | :rtype: bool |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 439 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 440 | Checks to see if the HTTP_X_REQUESTED_WITH server header has been |
| 441 | set, and returns boolean TRUE if it is or FALSE if not. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 442 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 443 | .. php:method:: is_cli_request() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 444 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 445 | :returns: TRUE if it is a CLI request, FALSE if not |
| 446 | :rtype: bool |
Michiel Vugteveen | be0ca26 | 2012-03-07 19:09:51 +0100 | [diff] [blame] | 447 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 448 | Checks to see if the application was run from the command-line |
| 449 | interface. |
Michiel Vugteveen | be0ca26 | 2012-03-07 19:09:51 +0100 | [diff] [blame] | 450 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 451 | .. note:: This method checks both the PHP SAPI name currently in use |
| 452 | and if the ``STDIN`` constant is defined, which is usually a |
| 453 | failsafe way to see if PHP is being run via the command line. |
Michiel Vugteveen | be0ca26 | 2012-03-07 19:09:51 +0100 | [diff] [blame] | 454 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 455 | :: |
| 456 | |
| 457 | $this->input->is_cli_request() |
| 458 | |
Andrey Andreev | ea801ab | 2014-01-20 15:03:43 +0200 | [diff] [blame] | 459 | .. note:: This method is DEPRECATED and is now just an alias for the |
| 460 | :func:`is_cli()` function. |
| 461 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 462 | .. php:method:: method([$upper = FALSE]) |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 463 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 464 | :param bool $upper: Whether to return the request method name in upper or lower case |
| 465 | :returns: HTTP request method |
| 466 | :rtype: string |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 467 | |
| 468 | Returns the ``$_SERVER['REQUEST_METHOD']``, with the option to set it |
| 469 | in uppercase or lowercase. |
| 470 | :: |
| 471 | |
| 472 | echo $this->input->method(TRUE); // Outputs: POST |
| 473 | echo $this->input->method(FALSE); // Outputs: post |
David Wosnitza | d31a4e6 | 2014-12-12 16:35:35 +0100 | [diff] [blame] | 474 | echo $this->input->method(); // Outputs: post |