Request Class

The request class is an object-oriented representation of an HTTP request. This is meant towork for both incoming, such as a request to the application from a browser, and outgoing requests,like would be used to send a request from the application to a third-party application. This classprovides the common functionality they both need, but both cases have custom classes that extendfrom the Request class to add specific functionality.

See the documentation for the IncomingRequest Class andCURLRequest Class for more usage details.

Class Reference

  • CodeIgniter\HTTP\Request
    • getIPAddress()

Returns:The user’s IP Address, if it can be detected, or null. If the IP addressis not a valid IP address, then will return 0.0.0.0Return type:string

Returns the IP address for the current user. If the IP address is not valid, the methodwill return ‘0.0.0.0’:

  1. echo $request->getIPAddress();

Important

This method takes into account the App->proxyIPs setting and willreturn the reported HTTP_X_FORWARDED_FOR, HTTP_CLIENT_IP, HTTP_X_CLIENT_IP, orHTTP_X_CLUSTER_CLIENT_IP address for the allowed IP address.

  • isValidIP($ip[, $which = ''])

Parameters:

  1. - **$ip** (_string_) IP address
  2. - **$which** (_string_) IP protocol (‘ipv4 or ipv6’)Returns:

true if the address is valid, false if notReturn type:bool

Takes an IP address as input and returns true or false (boolean) dependingon whether it is valid or not.

Note

The $request->getIPAddress() method above automatically validates the IP address.

  1. if ( ! $request->isValidIP($ip))
  2. {
  3. echo 'Not Valid';
  4. }
  5. else
  6. {
  7. echo 'Valid';
  8. }

Accepts an optional second string parameter of ‘ipv4’ or ‘ipv6’ to specifyan IP format. The default checks for both formats.

  • getMethod([$upper = FALSE])

Parameters:

  1. - **$upper** (_bool_) Whether to return the request method name in upper or lower caseReturns:

HTTP request methodReturn type:string

Returns the $_SERVER['REQUEST_METHOD'], with the option to set itin uppercase or lowercase.

  1. echo $request->getMethod(TRUE); // Outputs: POST
  2. echo $request->getMethod(FALSE); // Outputs: post
  3. echo $request->getMethod(); // Outputs: post
  • getServer([$index = null[, $filter = null[, $flags = null]]])

Parameters:

  1. - **$index** (_mixed_) Value name
  2. - **$filter** (_int_) The type of filter to apply. A list of filters can be found [here](http://php.net/manual/en/filter.filters.php).
  3. - **$flags** (_int_) Flags to apply. A list of flags can be found [here](http://php.net/manual/en/filter.filters.flags.php).Returns:

$_SERVER item value if found, NULL if notReturn type:mixed

This method is identical to the post(), get() and cookie() methods from theIncomingRequest Class, only it fetches getServer data ($_SERVER):

  1. $request->getServer('some_data');

To return an array of multiple $_SERVER values, pass all the required keysas an array.

  1. $require->getServer(['SERVER_PROTOCOL', 'REQUEST_URI']);