Retrieving IP address

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 = ['', ''];
$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').

Last update: Tue, 13 Sep 2022 14:32:15