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