Download the PHP package kuria/request-info without Composer
On this page you can find all versions of the php package kuria/request-info. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package request-info
Request info ############
Get information about the current HTTP request.
:depth: 3
Features
- getting request information:
- headers
- HTTPS detection
- client IP address
- scheme
- method
- host
- port
- URL
- base directory
- base path
- path info
- script name
- trusted proxy header support (x-forwarded / forwarded)
- host validation (inc. defining specific trusted hosts or host patterns)
- optional HTTP method override support
Requirements
- PHP 7.1+
Usage
All configuration and value retrieval is done via the static Kuria\RequestInfo\RequestInfo
class.
Configuration
Trusted proxies
By default all proxy headers are ignored. To trust select proxy headers, call RequestInfo::setTrustedProxies()
with an appropriately configured TrustedProxies
instance.
$trustedProxies = new TrustedProxies( ['192.168.1.10', '192.168.1.20'], // one or more IP adresses or subnets in CIDR notation TrustedProxies::HEADER_FORWARDED // which headers to trust (bit mask) );
RequestInfo::setTrustedProxies($trustedProxies);
Choosing which headers to trust
Trusted headers are a bitmask of the following constants:
Constant | Allowed headers |
TrustedProxies::HEADER_FORWARDED TrustedProxies::HEADER_X_FORWARDED_FOR TrustedProxies::HEADER_X_FORWARDED_HOST TrustedProxies::HEADER_X_FORWARDED_PROTO TrustedProxies::HEADER_X_FORWARDED_PORT TrustedProxies::HEADER_X_FORWARDED_ALL | Forwarded X-Forwarded-For X-Forwarded-Host X-Forwarded-Proto X-Forwarded-Port X-Forwarded-* |
Applications always behind a trusted proxy
If you are sure that an application will always be behind a trusted proxy, you can
use $_SERVER['REMOTE_ADDR']
in place of a hardcoded IP address:
$trustedProxies = new TrustedProxies( [Server::require('REMOTE_ADDR')], TrustedProxies::HEADER_FORWARDED );
RequestInfo::setTrustedProxies($trustedProxies);
Trusted hosts
The request host is always validated according to the standards.
To restrict accepted hosts further, use the following methods:
// specific hosts (exact match) RequestInfo::setTrustedHosts([ 'www.example.com', 'cdn.example.com', ]);
// host patterns RequestInfo::setTrustedHostPatterns([ '{\w+\.example\.com$}AD', '{example-node-\d+$}AD', ]);
HTTP method override
By default, the X-HTTP-Method-Override
header is ignored.
If you need to override the HTTP method via this header (e.g. because of restrictive firewall rules), you can enable its support:
RequestInfo::setAllowHttpMethodOverride(true);
Resetting configuration
To restore default RequestInfo
configuration:
RequestInfo::reset();
Getting request information
Headers
Get all request headers as an array. Header names are lowercased and used as keys.
Example output:
Array
(
[host] => localhost:8080
[connection] => keep-alive
[cache-control] => max-age=0
[upgrade-insecure-requests] => 1
[user-agent] => Mozilla/5.0 (Example)
[accept] => text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
[accept-encoding] => gzip, deflate, br
[accept-language] => en-US,en;q=0.9,cs;q=0.8
)
Trusted proxy detection
Check whether the request originated from a trusted proxy.
if (RequestInfo::isFromTrustedProxy()) { // request is from a trusted proxy }
HTTPS detection
See whether the request uses HTTPS.
if (RequestInfo::isSecure()) { // request uses HTTPS }
Client IP address
Get the client IP address.
var_dump(RequestInfo::getClientIp());
Example output:
string(9) "127.0.0.1"
To get all known client IP addresses (ordered from most trusted to least trusted), use getClientIps()
:
print_r(RequestInfo::getClientIps());
Example output:
Array
(
[0] => 20.30.40.50
[1] => 10.20.30.40
)
Method
Get the request method. The method name will always be in uppercase.
Also see HTTP method override.
var_dump(RequestInfo::getMethod());
Example output:
string(3) "GET"
Scheme
Get the request scheme.
var_dump(RequestInfo::getScheme());
Example output:
string(4) "https"
Host
Get the host name.
Also see Trusted hosts.
var_dump(RequestInfo::getHost());
Example output:
string(9) "localhost"
Port
Get the port number.
var_dump(RequestInfo::getPort());
Example output:
int(80)
URL
Get the request URL. Returns an unique instance of Kuria\Url\Url
.
See the kuria/url component for more information.
$url = RequestInfo::getUrl();
echo "URL:\t", $url->build(), PHP_EOL, "Scheme:\t", $url->getScheme(), PHP_EOL, "Host:\t", $url->getHost(), PHP_EOL, "Port:\t", $url->getPort(), PHP_EOL, "Path:\t", $url->getPath(), PHP_EOL, "Query:\t", json_encode($url->getQuery()), PHP_EOL;
Example output:
URL: http://localhost:8080/test/index.php/foo?bar=baz
Scheme: http
Host: localhost
Port: 8080
Path: /test/index.php/foo
Query: {"bar":"baz"}
Base directory
Get base directory (without script name, if any). The returned path never ends with a "/".
var_dump(RequestInfo::getBaseDir());
Examples:
URL | Base directory |
http://localhost/index.php http://localhost/index.php/page http://localhost/web/index.php http://localhost/we%20b/index.php | (empty string) (empty string) /web /we%20b |
Base path
Get base path (including the script name, if any). The returned path never ends with a "/".
var_dump(RequestInfo::getBasePath());
Examples:
URL | Base path |
http://localhost/index.php http://localhost/index.php/page http://localhost/web/index.php http://localhost/we%20b/index.php | /index.php /index.php /web/index.php /we%20b/index.php |
Path info
Get path info.
var_dump(RequestInfo::getPathInfo());
Examples:
URL | Path info |
http://localhost/index.php http://localhost/index.php/page http://localhost/web/index.php http://localhost/we%20b/index.php/foo%20bar | (empty string) /page (empty string) /foo%20bar |
Script name
Get the current script name.
var_dump(RequestInfo::getScriptName());
Example output:
string(18) "./public/index.php"
Internal cache
Most methods of the RequestInfo
class cache their results internally. If you manipulate $_SERVER
after already reading some request information, you will need to call RequestInfo::clearCache()
to clear the cache.