\CodeIgniter\HTTPIncomingRequest

Class IncomingRequest

Represents an incoming, getServer-side HTTP request.

Per the HTTP specification, this interface includes properties for each of the following:

  • Protocol version
  • HTTP method
  • URI
  • Headers
  • Message body

Additionally, it encapsulates all data as it has arrived to the application from the CGI and/or PHP environment, including:

  • The values represented in $_SERVER.
  • Any cookies provided (generally via $_COOKIE)
  • Query string arguments (generally via $_GET, or as parsed via parse_str())
  • Upload files, if any (as represented by $_FILES)
  • Deserialized body binds (generally from $_POST)

Summary

Methods
Properties
Constants
__construct()
getIPAddress()
isValidIP()
getMethod()
setMethod()
getServer()
getEnv()
setGlobal()
fetchGlobal()
getBody()
setBody()
appendBody()
populateHeaders()
getHeaders()
getHeader()
hasHeader()
getHeaderLine()
setHeader()
removeHeader()
appendHeader()
prependHeader()
getProtocolVersion()
setProtocolVersion()
detectLocale()
getDefaultLocale()
getLocale()
setLocale()
isCLI()
isAJAX()
isSecure()
getVar()
getJSON()
getRawInput()
getGet()
getPost()
getPostGet()
getGetPost()
getCookie()
getUserAgent()
getOldInput()
getFiles()
getFileMultiple()
getFile()
detectPath()
negotiate()
$uri
$config
No constants found
populateGlobals()
getHeaderName()
detectURI()
parseRequestURI()
parseQueryString()
removeRelativeDirectory()
$ipAddress
$proxyIPs
$method
$globals
$headers
$headerMap
$protocolVersion
$validProtocolVersions
$body
$enableCSRF
$files
$negotiator
$defaultLocale
$locale
$validLocales
$oldInput
$userAgent
N/A
No private methods found
No private properties found
N/A

Properties

$config

$config : \Config\App

Configuration settings.

Type

\Config\App

$ipAddress

$ipAddress : string

IP address of the current user.

Type

string

$proxyIPs

$proxyIPs : string|array

Proxy IPs

Type

string|array

$method

$method : string

Request method.

Type

string

$globals

$globals : array

Stores values we've retrieved from PHP globals.

Type

array

$headers

$headers : array

List of all HTTP request headers.

Type

array

$headerMap

$headerMap : array

Holds a map of lower-case header names and their normal-case key as it is in $headers.

Used for case-insensitive header access.

Type

array

$protocolVersion

$protocolVersion : string

Protocol version

Type

string

$validProtocolVersions

$validProtocolVersions : array

List of valid protocol versions

Type

array

$body

$body : string

Message body

Type

string

$enableCSRF

$enableCSRF : boolean

Enable CSRF flag

Enables a CSRF cookie token to be set. Set automatically based on Config setting.

Type

boolean

$defaultLocale

$defaultLocale : string

The default Locale this request should operate under.

Type

string

$locale

$locale : string

The current locale of the application.

Default value is set in Config\App.php

Type

string

$validLocales

$validLocales : array

Stores the valid locale codes.

Type

array

$oldInput

$oldInput : array

Holds the old data from a redirect.

Type

array

Methods

__construct()

__construct(object  $config, \CodeIgniter\HTTP\URI  $uri = null, string|null  $body = 'php://input', \CodeIgniter\HTTP\UserAgent  $userAgent) 

Constructor

Parameters

object $config
\CodeIgniter\HTTP\URI $uri
string|null $body
\CodeIgniter\HTTP\UserAgent $userAgent

getIPAddress()

getIPAddress() : string

Gets the user's IP address.

Returns

string —

IP address

isValidIP()

isValidIP(string  $ip = null, string  $which = null) : boolean

Validate an IP address

Parameters

string $ip

IP Address

string $which

IP protocol: 'ipv4' or 'ipv6'

Returns

boolean

getMethod()

getMethod(boolean  $upper = false) : string

Get the request method.

Parameters

boolean $upper

Whether to return in upper or lower case.

Returns

string

setMethod()

setMethod(string  $method) : \CodeIgniter\HTTP\Request

Sets the request method. Used when spoofing the request.

Parameters

string $method

Returns

\CodeIgniter\HTTP\Request

getServer()

getServer(string|array|null  $index = null, integer|null  $filter = null, null  $flags = null) : mixed

Fetch an item from the $_SERVER array.

Parameters

string|array|null $index

Index for item to be fetched from $_SERVER

integer|null $filter

A filter name to be applied

null $flags

Returns

mixed

getEnv()

getEnv(null  $index = null, null  $filter = null, null  $flags = null) : mixed

Fetch an item from the $_ENV array.

Parameters

null $index

Index for item to be fetched from $_ENV

null $filter

A filter name to be applied

null $flags

Returns

mixed

setGlobal()

setGlobal(string  $method,   $value) : $this

Allows manually setting the value of PHP global, like $_GET, $_POST, etc.

Parameters

string $method
$value

Returns

$this

fetchGlobal()

fetchGlobal(string  $method, string|array|null  $index = null, integer|null  $filter = null, mixed  $flags = null) : mixed

Fetches one or more items from a global, like cookies, get, post, etc.

Can optionally filter the input when you retrieve it by passing in a filter.

If $type is an array, it must conform to the input allowed by the filter_input_array method.

http://php.net/manual/en/filter.filters.sanitize.php

Parameters

string $method

Input filter constant

string|array|null $index
integer|null $filter

Filter constant

mixed $flags

Returns

mixed

getBody()

getBody() : mixed

Returns the Message's body.

Returns

mixed

populateHeaders()

populateHeaders() 

Populates the $headers array with any headers the getServer knows about.

getHeaders()

getHeaders() : array

Returns an array containing all headers.

Returns

array —

An array of the request headers

getHeader()

getHeader(string  $name) : array|\CodeIgniter\HTTP\Header

Returns a single header object. If multiple headers with the same name exist, then will return an array of header objects.

Parameters

string $name

Returns

array|\CodeIgniter\HTTP\Header

hasHeader()

hasHeader(string  $name) : boolean

Determines whether a header exists.

Parameters

string $name

Returns

boolean

getHeaderLine()

getHeaderLine(string  $name) : string

Retrieves a comma-separated string of the values for a single header.

This method returns all of the header values of the given case-insensitive header name as a string concatenated together using a comma.

NOTE: Not all header values may be appropriately represented using comma concatenation. For such headers, use getHeader() instead and supply your own delimiter when concatenating.

Parameters

string $name

Returns

string

setHeader()

setHeader(string  $name, array|null|string  $value) : \CodeIgniter\HTTP\Message|\CodeIgniter\HTTP\Response

Sets a header and it's value.

Parameters

string $name
array|null|string $value

Returns

\CodeIgniter\HTTP\Message|\CodeIgniter\HTTP\Response

removeHeader()

removeHeader(string  $name) : \CodeIgniter\HTTP\Message

Removes a header from the list of headers we track.

Parameters

string $name

Returns

\CodeIgniter\HTTP\Message

appendHeader()

appendHeader(string  $name, string  $value) : \CodeIgniter\HTTP\Message

Adds an additional header value to any headers that accept multiple values (i.e. are an array or implement ArrayAccess)

Parameters

string $name
string $value

Returns

\CodeIgniter\HTTP\Message

prependHeader()

prependHeader(string  $name, string  $value) : \CodeIgniter\HTTP\Message

Adds an additional header value to any headers that accept multiple values (i.e. are an array or implement ArrayAccess)

Parameters

string $name
string $value

Returns

\CodeIgniter\HTTP\Message

getProtocolVersion()

getProtocolVersion() : string

Returns the HTTP Protocol Version.

Returns

string

setProtocolVersion()

setProtocolVersion(string  $version) : \CodeIgniter\HTTP\Message

Sets the HTTP protocol version.

Parameters

string $version

Returns

\CodeIgniter\HTTP\Message

detectLocale()

detectLocale(  $config) 

Handles setting up the locale, perhaps auto-detecting through content negotiation.

Parameters

$config

getDefaultLocale()

getDefaultLocale() : string

Returns the default locale as set in Config\App.php

Returns

string

getLocale()

getLocale() : string

Gets the current locale, with a fallback to the default locale if none is set.

Returns

string

setLocale()

setLocale(string  $locale) : \CodeIgniter\HTTP\IncomingRequest

Sets the locale string for this request.

Parameters

string $locale

Returns

\CodeIgniter\HTTP\IncomingRequest

isCLI()

isCLI() : boolean

Determines if this request was made from the command line (CLI).

Returns

boolean

isAJAX()

isAJAX() : boolean

Test to see if a request contains the HTTP_X_REQUESTED_WITH header.

Returns

boolean

isSecure()

isSecure() : boolean

Attempts to detect if the current connection is secure through a few different methods.

Returns

boolean

getVar()

getVar(string|array|null  $index = null, integer|null  $filter = null, mixed  $flags = null) : mixed

Fetch an item from the $_REQUEST object. This is the simplest way to grab data from the request object and can be used in lieu of the other get* methods in most cases.

Parameters

string|array|null $index
integer|null $filter

Filter constant

mixed $flags

Returns

mixed

getJSON()

getJSON(boolean  $assoc = false, integer  $depth = 512, integer  $options) : mixed

A convenience method that grabs the raw input stream and decodes the JSON into an array.

If $assoc == true, then all objects in the response will be converted to associative arrays.

Parameters

boolean $assoc

Whether to return objects as associative arrays

integer $depth

How many levels deep to decode

integer $options

Bitmask of options

Returns

mixed

getRawInput()

getRawInput() : mixed

A convenience method that grabs the raw input stream(send method in PUT, PATCH, DELETE) and decodes the String into an array.

Returns

mixed

getGet()

getGet(string|array|null  $index = null, integer|null  $filter = null, mixed|null  $flags = null) : mixed

Fetch an item from GET data.

Parameters

string|array|null $index

Index for item to fetch from $_GET.

integer|null $filter

A filter name to apply.

mixed|null $flags

Returns

mixed

getPost()

getPost(string|array|null  $index = null, integer|null  $filter = null, mixed  $flags = null) : mixed

Fetch an item from POST.

Parameters

string|array|null $index

Index for item to fetch from $_POST.

integer|null $filter

A filter name to apply

mixed $flags

Returns

mixed

getPostGet()

getPostGet(string|array|null  $index = null, integer|null  $filter = null, mixed  $flags = null) : mixed

Fetch an item from POST data with fallback to GET.

Parameters

string|array|null $index

Index for item to fetch from $_POST or $_GET

integer|null $filter

A filter name to apply

mixed $flags

Returns

mixed

getGetPost()

getGetPost(string|array|null  $index = null, integer|null  $filter = null, mixed  $flags = null) : mixed

Fetch an item from GET data with fallback to POST.

Parameters

string|array|null $index

Index for item to be fetched from $_GET or $_POST

integer|null $filter

A filter name to apply

mixed $flags

Returns

mixed

getCookie()

getCookie(string|array|null  $index = null, integer|null  $filter = null, mixed  $flags = null) : mixed

Fetch an item from the COOKIE array.

Parameters

string|array|null $index

Index for item to be fetched from $_COOKIE

integer|null $filter

A filter name to be applied

mixed $flags

Returns

mixed

getOldInput()

getOldInput(string  $key) : mixed

Attempts to get old Input data that has been flashed to the session with redirect_with_input(). It first checks for the data in the old POST data, then the old GET data and finally check for dot arrays

Parameters

string $key

Returns

mixed

getFiles()

getFiles() : array

Returns an array of all files that have been uploaded with this request. Each file is represented by an UploadedFile instance.

Returns

array

getFileMultiple()

getFileMultiple(string  $fileID) : array|null

Verify if a file exist, by the name of the input field used to upload it, in the collection of uploaded files and if is have been uploaded with multiple option.

Parameters

string $fileID

Returns

array|null

getFile()

getFile(string  $fileID) : \CodeIgniter\HTTP\Files\UploadedFile|null

Retrieves a single file by the name of the input field used to upload it.

Parameters

string $fileID

Returns

\CodeIgniter\HTTP\Files\UploadedFile|null

detectPath()

detectPath(string  $protocol = '') : string

Based on the URIProtocol Config setting, will attempt to detect the path portion of the current URI.

Parameters

string $protocol

Returns

string

negotiate()

negotiate(string  $type, array  $supported, boolean  $strictMatch = false) : string

Provides a convenient way to work with the Negotiate class for content negotiation.

Parameters

string $type
array $supported
boolean $strictMatch

Returns

string

populateGlobals()

populateGlobals(string  $method) 

Saves a copy of the current state of one of several PHP globals so we can retrieve them later.

Parameters

string $method

getHeaderName()

getHeaderName(string  $name) : string

Takes a header name in any case, and returns the normal-case version of the header.

Parameters

string $name

Returns

string

detectURI()

detectURI(string  $protocol, string  $baseURL) 

Sets up our URI object based on the information we have. This is either provided by the user in the baseURL Config setting, or determined from the environment as needed.

Parameters

string $protocol
string $baseURL

parseRequestURI()

parseRequestURI() : string

Will parse the REQUEST_URI and automatically detect the URI from it, fixing the query string if necessary.

Returns

string —

The URI it found.

parseQueryString()

parseQueryString() : string

Parse QUERY_STRING

Will parse QUERY_STRING and automatically detect the URI from it.

Returns

string

removeRelativeDirectory()

removeRelativeDirectory(string  $uri) : string

Remove relative directory (.

./) and multi slashes (///)

Do some final cleaning of the URI and return it, currently only used in static::_parse_request_uri()

Parameters

string $uri

Returns

string