PHP code example of middlewares / client-ip

1. Go to this page and download the library: Download middlewares/client-ip library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

middlewares / client-ip example snippets


$dispatcher = new Dispatcher([
	new Middlewares\ClientIp(),

    function ($request) {
        //Get the client ip
        $ip = $request->getAttribute('client-ip');
    }
]);

$response = $dispatcher->dispatch(new ServerRequest());

//Use proxies
$middleware = (new Middlewares\ClientIp())->proxy();

//Trust only some proxies by ip
$middleware = (new Middlewares\ClientIp())->proxy(['10.10.10.10', '10.10.10.11']);

//Trust only some proxies by ip using a specific header
$middleware = (new Middlewares\ClientIp())->proxy(['10.10.10.10', '10.10.10.11'], ['X-Forwarded-For']);

// Trust only some proxies by cidr range
// usefull when you have an autoscaled proxy(like haproxy) in a subnet
$middleware = (new Middlewares\ClientIp())->proxy(['192.168.0.0/16', '10.0.0.0/8']);

//Save the ip in the "ip" attribute
$middleware = (new Middlewares\ClientIp())->attribute('ip');