In this Slim package the dependency akrabat/ip-address-middleware
is loaded by default.
composer require akrabat/rka-ip-address-middleware
To use it, register the middleware with the App, providing a list of trusted proxies (e.g. varnish servers) if you are using them.:
$checkProxyHeaders = true;
$trustedProxies = ['10.0.0.1', '10.0.0.2'];
$app->add(new RKA\Middleware\IpAddress($checkProxyHeaders, $trustedProxies));
$app->get('/', function ($request, $response, $args) {
$ipAddress = $request->getAttribute('ip_address');
return $response;
});
The middleware stores the client’s IP address in a request attribute, so access is via $request->getAttribute('ip_address').