Middleware to detect the client ip and save it as a request attribute.
Note: This middleware is intended for server side only
- PHP >= 5.6
- A PSR-7 http mesage implementation (Diactoros, Guzzle, Slim, etc...)
- A PSR-15 middleware dispatcher (Middleman, etc...)
This package is installable and autoloadable via Composer as middlewares/client-ip.
composer require middlewares/client-ip
$dispatcher = new Dispatcher([
new Middlewares\ClientIp(),
function ($request) {
//Get the client ip
$ip = $request->getAttribute('client-ip');
}
]);
$response = $dispatcher->dispatch(new ServerRequest());
List of trusted headers to search the ip if REMOTE_ADDR
server parameter is not valid. By default is:
['Forwarded', 'Forwarded-For', 'Client-Ip', 'X-Forwarded', 'X-Forwarded-For', 'X-Cluster-Client-Ip']
Used to get the ip from localhost environment using http://ipecho.net/plain. Disabled by default.
The attribute name used to store the ip in the server request. By default is client-ip
.
Please see CHANGELOG for more information about recent changes and CONTRIBUTING for contributing details.
The MIT License (MIT). Please see LICENSE for more information.