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 | |
| 71 | - $this->input->post() |
Andrey Andreev | 22c3e73 | 2012-06-13 10:21:36 +0300 | [diff] [blame] | 72 | - $this->input->get() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 73 | - $this->input->cookie() |
| 74 | - $this->input->server() |
| 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 | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 111 | :param string $index: POST parameter name |
| 112 | :param bool $xss_clean: Whether to apply XSS filtering |
| 113 | :returns: mixed |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 114 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 115 | The first parameter will contain the name of the POST item you are |
| 116 | looking for:: |
Derek Jones | 8831d11 | 2011-10-05 15:57:02 -0500 | [diff] [blame] | 117 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 118 | $this->input->post('some_data'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 119 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 120 | The method returns NULL if the item you are attempting to retrieve |
| 121 | does not exist. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 122 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 123 | The second optional parameter lets you run the data through the XSS |
Andrey Andreev | 88ebdf7 | 2014-01-08 17:28:02 +0200 | [diff] [blame] | 124 | filter. It's enabled by setting the second parameter to boolean TRUE |
| 125 | or by setting your ``$config['global_xss_filtering']`` to TRUE. |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 126 | :: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 127 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 128 | $this->input->post('some_data', TRUE); |
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 | To return an array of all POST items call without any parameters. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 131 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 132 | To return all POST items and pass them through the XSS filter set the |
| 133 | first parameter NULL while setting the second parameter to boolean TRUE. |
| 134 | :: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 135 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 136 | $this->input->post(NULL, TRUE); // returns all POST items with XSS filter |
Andrey Andreev | 88ebdf7 | 2014-01-08 17:28:02 +0200 | [diff] [blame] | 137 | $this->input->post(NULL, FALSE); // returns all POST items without XSS filter |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 138 | |
Andrey Andreev | 88ebdf7 | 2014-01-08 17:28:02 +0200 | [diff] [blame] | 139 | .. method:: get([$index = NULL[, $xss_clean = NULL]]) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 140 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 141 | :param string $index: GET parameter name |
| 142 | :param bool $xss_clean: Whether to apply XSS filtering |
| 143 | :returns: mixed |
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 | This method is identical to ``post()``, only it fetches GET data. |
| 146 | :: |
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 | $this->input->get('some_data', TRUE); |
| 149 | |
| 150 | To return an array of all GET items call without any parameters. |
| 151 | |
| 152 | To return all GET items and pass them through the XSS filter set the |
| 153 | first parameter NULL while setting the second parameter to boolean TRUE. |
| 154 | :: |
| 155 | |
| 156 | $this->input->get(NULL, TRUE); // returns all GET items with XSS filter |
Andrey Andreev | 88ebdf7 | 2014-01-08 17:28:02 +0200 | [diff] [blame] | 157 | $this->input->get(NULL, FALSE); // returns all GET items without XSS filtering |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 158 | |
Andrey Andreev | ea801ab | 2014-01-20 15:03:43 +0200 | [diff] [blame^] | 159 | .. method:: post_get([$index = ''[, $xss_clean = NULL]]) |
| 160 | |
| 161 | :param string $index: POST/GET parameter name |
| 162 | :param bool $xss_clean: Whether to apply XSS filtering |
| 163 | :returns: mixed |
| 164 | |
| 165 | This method works the same way as ``post()`` and ``get()``, only combined. |
| 166 | It will search through both POST and GET streams for data, looking in POST |
| 167 | first, and then in GET:: |
| 168 | |
| 169 | $this->input->post_get('some_data', TRUE); |
| 170 | |
Andrey Andreev | 88ebdf7 | 2014-01-08 17:28:02 +0200 | [diff] [blame] | 171 | .. method:: get_post([$index = ''[, $xss_clean = NULL]]) |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 172 | |
| 173 | :param string $index: GET/POST parameter name |
| 174 | :param bool $xss_clean: Whether to apply XSS filtering |
| 175 | :returns: mixed |
| 176 | |
Andrey Andreev | ea801ab | 2014-01-20 15:03:43 +0200 | [diff] [blame^] | 177 | This method works the same way as ``post_get()`` only it looks for GET |
| 178 | data first. |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 179 | |
| 180 | $this->input->get_post('some_data', TRUE); |
| 181 | |
Andrey Andreev | ea801ab | 2014-01-20 15:03:43 +0200 | [diff] [blame^] | 182 | .. note:: This method used to act EXACTLY like ``post_get()``, but it's |
| 183 | behavior has changed in CodeIgniter 3.0. |
| 184 | |
Andrey Andreev | 88ebdf7 | 2014-01-08 17:28:02 +0200 | [diff] [blame] | 185 | .. method:: cookie([$index = ''[, $xss_clean = NULL]]) |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 186 | |
| 187 | :param string $index: COOKIE parameter name |
| 188 | :param bool $xss_clean: Whether to apply XSS filtering |
| 189 | :returns: mixed |
| 190 | |
| 191 | This method is identical to ``post()`` and ``get()``, only it fetches cookie |
| 192 | data:: |
| 193 | |
| 194 | $this->input->cookie('some_cookie'); |
| 195 | $this->input->cookie('some_cookie, TRUE); // with XSS filter |
| 196 | |
Andrey Andreev | 88ebdf7 | 2014-01-08 17:28:02 +0200 | [diff] [blame] | 197 | .. method:: server([$index = ''[, $xss_clean = NULL]]) |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 198 | |
| 199 | :param string $index: Value name |
| 200 | :param bool $xss_clean: Whether to apply XSS filtering |
| 201 | :returns: mixed |
| 202 | |
| 203 | This method is identical to the ``post()``, ``get()`` and ``cookie()`` methods, |
| 204 | only it fetches server data (``$_SERVER``):: |
| 205 | |
| 206 | $this->input->server('some_data'); |
| 207 | |
Andrey Andreev | 88ebdf7 | 2014-01-08 17:28:02 +0200 | [diff] [blame] | 208 | .. method:: input_stream([$index = ''[, $xss_clean = NULL]]) |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 209 | |
| 210 | :param string $index: Key name |
| 211 | :param bool $xss_clean: Whether to apply XSS filtering |
| 212 | :returns: mixed |
| 213 | |
| 214 | This method is identical to ``get()``, ``post()`` and ``cookie()``, |
| 215 | only it fetches the *php://input* stream data. |
| 216 | |
| 217 | .. method:: set_cookie($name = ''[, $value = ''[, $expire = ''[, $domain = ''[, $path = '/'[, $prefix = ''[, $secure = FALSE[, $httponly = FALSE]]]]]]]) |
| 218 | |
| 219 | :param mixed $name: Cookie name or an array of parameters |
| 220 | :param string $value: Cookie value |
| 221 | :param int $expire: Cookie expiration time in seconds |
| 222 | :param string $domain: Cookie domain |
| 223 | :param string $path: Cookie path |
| 224 | :param string $prefix: Cookie name prefix |
| 225 | :param bool $secure: Whether to only transfer the cookie through HTTPS |
| 226 | :param bool $httponly: Whether to only make the cookie accessible for HTTP requests (no JavaScript) |
| 227 | :returns: void |
| 228 | |
| 229 | Sets a cookie containing the values you specify. There are two ways to |
| 230 | pass information to this method so that a cookie can be set: Array |
| 231 | Method, and Discrete Parameters: |
| 232 | |
Connor Tumbleson | 75b3fb2 | 2014-01-11 06:58:43 -0600 | [diff] [blame] | 233 | **Array Method** |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 234 | |
| 235 | Using this method, an associative array is passed to the first |
| 236 | parameter:: |
| 237 | |
| 238 | $cookie = array( |
| 239 | 'name' => 'The Cookie Name', |
| 240 | 'value' => 'The Value', |
| 241 | 'expire' => '86500', |
| 242 | 'domain' => '.some-domain.com', |
| 243 | 'path' => '/', |
| 244 | 'prefix' => 'myprefix_', |
| 245 | 'secure' => TRUE |
| 246 | ); |
| 247 | |
| 248 | $this->input->set_cookie($cookie); |
| 249 | |
| 250 | **Notes:** |
| 251 | |
| 252 | Only the name and value are required. To delete a cookie set it with the |
| 253 | expiration blank. |
| 254 | |
| 255 | The expiration is set in **seconds**, which will be added to the current |
| 256 | time. Do not include the time, but rather only the number of seconds |
| 257 | from *now* that you wish the cookie to be valid. If the expiration is |
| 258 | set to zero the cookie will only last as long as the browser is open. |
| 259 | |
| 260 | For site-wide cookies regardless of how your site is requested, add your |
| 261 | URL to the **domain** starting with a period, like this: |
| 262 | .your-domain.com |
| 263 | |
| 264 | The path is usually not needed since the method sets a root path. |
| 265 | |
| 266 | The prefix is only needed if you need to avoid name collisions with |
| 267 | other identically named cookies for your server. |
| 268 | |
| 269 | The secure boolean is only needed if you want to make it a secure cookie |
| 270 | by setting it to TRUE. |
| 271 | |
Connor Tumbleson | 75b3fb2 | 2014-01-11 06:58:43 -0600 | [diff] [blame] | 272 | **Discrete Parameters** |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 273 | |
| 274 | If you prefer, you can set the cookie by passing data using individual |
| 275 | parameters:: |
| 276 | |
| 277 | $this->input->set_cookie($name, $value, $expire, $domain, $path, $prefix, $secure); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 278 | |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 279 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 280 | .. method:: ip_address() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 281 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 282 | :returns: string |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 283 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 284 | Returns the IP address for the current user. If the IP address is not |
| 285 | valid, the method will return '0.0.0.0':: |
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 | echo $this->input->ip_address(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 288 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 289 | .. important:: This method takes into account the ``$config['proxy_ips']`` |
| 290 | setting and will return the reported HTTP_X_FORWARDED_FOR, |
| 291 | HTTP_CLIENT_IP, HTTP_X_CLIENT_IP or HTTP_X_CLUSTER_CLIENT_IP |
| 292 | address for the allowed IP addresses. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 293 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 294 | .. method:: valid_ip($ip[, $which = '']) |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 295 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 296 | :param string $ip: IP address |
| 297 | :param string $which: IP protocol ('ipv4' or 'ipv6') |
| 298 | :returns: bool |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 299 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 300 | Takes an IP address as input and returns TRUE or FALSE (boolean) depending |
| 301 | on whether it is valid or not. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 302 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 303 | .. note:: The $this->input->ip_address() method above automatically |
| 304 | validates the IP address. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 305 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 306 | :: |
Andrey Andreev | 5a25718 | 2012-06-10 06:18:14 +0300 | [diff] [blame] | 307 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 308 | if ( ! $this->input->valid_ip($ip)) |
| 309 | { |
| 310 | echo 'Not Valid'; |
| 311 | } |
| 312 | else |
| 313 | { |
| 314 | echo 'Valid'; |
| 315 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 316 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 317 | Accepts an optional second string parameter of 'ipv4' or 'ipv6' to specify |
| 318 | an IP format. The default checks for both formats. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 319 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 320 | .. method:: user_agent() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 321 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 322 | :returns: string |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 323 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 324 | Returns the user agent string (web browser) being used by the current user, |
| 325 | or NULL if it's not available. |
| 326 | :: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 327 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 328 | echo $this->input->user_agent(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 329 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 330 | See the :doc:`User Agent Class <user_agent>` for methods which extract |
| 331 | information from the user agent string. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 332 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 333 | .. method:: request_headers([$xss_clean = FALSE]) |
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 | :param bool $xss_clean: Whether to apply XSS filtering |
| 336 | :returns: array |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 337 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 338 | Returns an array of HTTP request headers. |
| 339 | Useful if running in a non-Apache environment where |
| 340 | `apache_request_headers() <http://php.net/apache_request_headers>`_ |
| 341 | will not be supported. |
| 342 | :: |
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 | $headers = $this->input->request_headers(); |
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 | .. method:: get_request_header($index[, $xss_clean = FALSE]) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 347 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 348 | :param string $index: HTTP request header name |
| 349 | :param bool $xss_clean: Whether to apply XSS filtering |
| 350 | :returns: string |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 351 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 352 | Returns a single member of the request headers array or NULL |
| 353 | if the searched header is not found. |
| 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 | $this->input->get_request_header('some-header', TRUE); |
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:: is_ajax_request() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 359 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 360 | :returns: bool |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 361 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 362 | Checks to see if the HTTP_X_REQUESTED_WITH server header has been |
| 363 | set, and returns boolean TRUE if it is or FALSE if not. |
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 | .. method:: is_cli_request() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 366 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 367 | :returns: bool |
Michiel Vugteveen | be0ca26 | 2012-03-07 19:09:51 +0100 | [diff] [blame] | 368 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 369 | Checks to see if the application was run from the command-line |
| 370 | interface. |
Michiel Vugteveen | be0ca26 | 2012-03-07 19:09:51 +0100 | [diff] [blame] | 371 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 372 | .. note:: This method checks both the PHP SAPI name currently in use |
| 373 | and if the ``STDIN`` constant is defined, which is usually a |
| 374 | 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] | 375 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 376 | :: |
| 377 | |
| 378 | $this->input->is_cli_request() |
| 379 | |
Andrey Andreev | ea801ab | 2014-01-20 15:03:43 +0200 | [diff] [blame^] | 380 | .. note:: This method is DEPRECATED and is now just an alias for the |
| 381 | :func:`is_cli()` function. |
| 382 | |
Andrey Andreev | 04535c7 | 2014-01-06 10:57:05 +0200 | [diff] [blame] | 383 | .. method:: method([$upper = FALSE]) |
| 384 | |
| 385 | :param bool $upper: Whether to return the request method name in upper or lower case |
| 386 | :returns: string |
| 387 | |
| 388 | Returns the ``$_SERVER['REQUEST_METHOD']``, with the option to set it |
| 389 | in uppercase or lowercase. |
| 390 | :: |
| 391 | |
| 392 | echo $this->input->method(TRUE); // Outputs: POST |
| 393 | echo $this->input->method(FALSE); // Outputs: post |
| 394 | echo $this->input->method(); // Outputs: post |