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