by akrabat

PSR-7 Middleware that determines the client IP address and stores it as an ServerRequest attribute

131 Stars 26 Forks Last release: over 2 years ago (1.0.1) Other 46 Commits 8 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:

Client IP address middleware

PSR-15 Middleware that determines the client IP address and stores it as an

attribute called
. It optionally checks various common proxy headers and then falls back to

Build status


The constructor takes 4 parameters which can be used to configure this middleware.

Check proxy headers

Note that the proxy headers are only checked if the first parameter to the constructor is set to

. If set to false, then only
is used.

Trusted Proxies

If you configure to check the proxy headers (first parameter is

), you have to provide an array of trusted proxies as the second parameter. If the array is empty, the proxy headers will always be evaluated. If the array is not empty, it must contain strings with IP addresses, one of them must be the
variable in order to allow evaluating the proxy headers - otherwise the
itself is returned.

Attribute name

By default, the name of the attribute is '

'. This can be changed by the third constructor parameter.

Headers to inspect

By default, this middleware checks the 'Forwarded', 'X-Forwarded-For', 'X-Forwarded', 'X-Cluster-Client-Ip' and 'Client-Ip' headers. You can replace this list with your own using the fourth constructor parameter.

If you use the nginx, setrealip_from directive, then you should probably set this to:

$headersToInspect = [

If you use CloudFlare, then according to the documentation you should probably set this to:

$headersToInspect = [

Security considerations

A malicious client may send any header to your proxy, including any proxy headers, containing any IP address. If your proxy simply adds another IP address to the header, an attacker can send a fake IP. Make sure to setup your proxy in a way that removes any sent (and possibly faked) headers from the original request and replaces them with correct values (i.e. the currently used

on the proxy server).

This library cannot by design ensure you get correct and trustworthy results if your network environment isn't setup properly.


composer require akrabat/ip-address-middleware


In Slim 3:

$checkProxyHeaders = true; // Note: Never trust the IP address for security processes!
$trustedProxies = ['', '']; // Note: Never trust the IP address for security processes!
$app->add(new RKA\Middleware\IpAddress($checkProxyHeaders, $trustedProxies));

$app->get('/', function ($request, $response, $args) { $ipAddress = $request->getAttribute('ip_address');

return $response;



  • Code style:
    $ vendor/bin/phpcs
  • Unit tests:
    $ vendor/bin/phpunit
  • Code coverage:
    $ vendor/bin/phpunit --coverage-html ./build

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.