2017 © Pedro Peláez
 

library whip

A PHP class for retrieving accurate IP address information for the client.

image

vectorface/whip

A PHP class for retrieving accurate IP address information for the client.

  • Monday, October 30, 2017
  • by ckdarby
  • Repository
  • 22 Watchers
  • 344 Stars
  • 51,617 Installations
  • PHP
  • 7 Dependents
  • 0 Suggesters
  • 19 Forks
  • 1 Open issues
  • 8 Versions
  • 9 % Grown

The README.md

Whip

Build Status Code Coverage Scrutinizer Code Quality Latest Stable Version License, (*1)

Whip (stands for Which Ip) is a lightweight class for returning a client's IP address in PHP., (*2)

The Problem

It may seem trivial to simply pull the client's IP address from $_SERVER['REMOTE_ADDR'] but this address is not always accurate. For example, if your web servers are behind a reverse proxy like Varnish, the IP address listed will be that of your proxy and not the client., (*3)

Many solutions propose checking multiple headers but those headers can be spoofed as well and we want to present a final solution anyone can deploy., (*4)

Installing Whip.

Simply run the following composer command:, (*5)

$ composer require vectorface/whip

Using Whip

Add the required use statement to your class, (*6)

use Vectorface\Whip\Whip;

To fetch an IP address using every implemented method, you can simply do, (*7)

$whip = new Whip();
$clientAddress = $whip->getValidIpAddress();

The class will attempt every method to retrieve the client's IP address starting with very specific use cases and falling back to more general use cases., (*8)

Note, that the method Whip::getValidIpAddress will return false if no valid IP address could be determined, so it is important to check for errors., (*9)

$whip = new Whip();
if (false === ($clientAddress = $whip->getValidIpAddress())) {
    // handle the error
}

To fetch an IP address using a specific method, you can pass a bitmask of enabled methods to the constructor. Here is an example of looking up the IP address using CloudFlare's custom HTTP header, and falling back to $_SERVER['REMOTE_ADDR'] otherwise., (*10)

$whip = new Whip(Whip::CLOUDFLARE_HEADERS | Whip::REMOTE_ADDR);
$clientAddress = $whip->getValidIpAddress();

This method works, but there is the problem that the custom HTTP header can easily be spoofed if your sites accept traffic not from CloudFlare. To prevent this, Whip allows you to specify a whitelist of IP addresses (or address ranges) that you accept per method., (*11)

Using Whip Behind a Trusted Proxy

A common use case is to deploy a trusted proxy (nginx, varnish, and many others) in front of an application server. To forward the correct client IP, the trusted proxy should be configured to inject a header for Whip to read with the custom headers method., (*12)

If the trusted proxy is configured to send a X-My-Client-IP header, Whip could be used as follows:, (*13)

$whip = new Whip(
    Whip::CUSTOM_HEADERS,
    [Whip::CUSTOM_HEADERS => [ // Whitelist your proxies.
        Whip::IPV4 => ['10.0.0.2', '10.0.0.3']
    ]]
);
$whip->addCustomHeader('HTTP_X_MY_CLIENT_IP');
$ip = $whip->getValidIpAddress();

Using the CloudFlare IP Range Whitelist

As a common example, Whip can accept a whitelist of IP ranges for CloudFlare when using their custom header and fall back to $_SERVER['REMOTE_ADDR'] if the custom header was not found or if the source IP address does match any in the whitelist., (*14)

$whip = new Whip(
    Whip::CLOUDFLARE_HEADERS | Whip::REMOTE_ADDR,
    [
        Whip::CLOUDFLARE_HEADERS => [
            Whip::IPV4 => [
                '199.27.128.0/21',
                '173.245.48.0/20',
                '103.21.244.0/22',
                '103.22.200.0/22',
                '103.31.4.0/22',
                '141.101.64.0/18',
                '108.162.192.0/18',
                '190.93.240.0/20',
                '188.114.96.0/20',
                '197.234.240.0/22',
                '198.41.128.0/17',
                '162.158.0.0/15',
                '104.16.0.0/12'
            ],
            Whip::IPV6 => [
                '2400:cb00::/32',
                '2606:4700::/32',
                '2803:f800::/32',
                '2405:b500::/32',
                '2405:8100::/32'
            ]
        ]
    ]
);
$clientAddress = $whip->getValidIpAddress();

Please be sure to use the actual list of IP ranges from CloudFlare for IPv4 and IPv6., (*15)

List of Methods

The individual methods are stored as integer constants on the Whip class. To combine methods, use the bitwise OR operator |. The current methods are:, (*16)

  • Whip::REMOTE_ADDR - Uses the standard $_SERVER['REMOTE_ADDR'].
  • Whip::PROXY_HEADERS - Uses any of the following values:
    • $_SERVER['HTTP_CLIENT_IP']
    • $_SERVER['HTTP_X_FORWARDED_FOR']
    • $_SERVER['HTTP_X_FORWARDED']
    • $_SERVER['HTTP_X_CLUSTER_CLIENT_IP']
    • $_SERVER['HTTP_FORWARDED_FOR']
    • $_SERVER['HTTP_FORWARDED']
    • $_SERVER['HTTP_X_REAL_IP']
  • Whip::CLOUDFLARE_HEADERS - Uses the CloudFlare provided HTTP header "CF-Connecting-IP".
  • Whip::INCAPSULA_HEADERS - Use the Incapsula provided HTTP header "Incap-Client-IP".
  • Whip::CUSTOM_HEADERS - Uses a custom list of HTTP headers passed into Whip::addCustomHeader.

Please note that the proxy headers method can be susceptible to client spoofing because it extracts addresses from several possible HTTP headers. This means that using the proxy headers method is not appropriate where trust is required, like in the context of authentication., (*17)

Using a Custom Header

Whip can also allow you to specify a custom header to use. For example, you may configure your own proxy to send a unique obfuscated header internally that would be hard to spoof. In this example, we assume Varnish is run locally and we use a custom HTTP header "X-SECRET-REAL-IP" (and fall back to $_SERVER['REMOTE_ADDR'] if the custom header doesn't work)., (*18)

$whip = new Whip(
    Whip::CUSTOM_HEADERS | Whip::REMOTE_ADDR,
    [
        Whip::CUSTOM_HEADERS => [
            Whip::IPV4 => [
                '127.0.0.1'
            ],
            Whip::IPV6 => [
                '::1'
            ]
        ]
    ]
);
$whip->addCustomHeader('X-SECRET-REAL-IP');
$clientAddress = $whip->getValidIpAddress();

Valid IP Ranges

For IPv4, Whip accepts three types of IP ranges:, (*19)

  • Asterisk wildcard (192.168.*)
  • Dashed range (192.168.0.0-192.168.255.255)
  • CIDR bitmask notation (192.168.0.0/16)

For IPv6, Whip only accepts the CIDR bitmask notation (fc00::/7)., (*20)

Furthermore, you can specify a list of exact IP addresses instead of a list of ranges., (*21)

IP Range Filtering

Whip can also be used to provide simple IP range matching. For example,, (*22)

$range = new Vectorface\Whip\IpRange\Ipv4Range('10.0.*');
if ($range->containsIp($ipv4Address)) {
    // handle the IP address being within the range
}

$range = new Vectorface\Whip\IpRange\Ipv6Range('::1/32');
if ($range->containsIp($ipv6Address)) {
    // handle the IP address being within the range
}

PSR-7 Requests, and Others

Whip supports using PSR-7 (http-message) request instances in place of the $_SERVER superglobal. For example,, (*23)

// Get a Psr\Http\Message\ServerRequestInterface implementation from somewhere.
$request = ServerRequestFactory::fromGlobals();

// You can pass the request in the constructor.
$whip = new Whip(Whip::REMOTE_ADDR, [], $request);

// ... or set the request as the source of data.
$whip->setSource($request);

// ... or pass it to any function accepting a source argument.
$ip = $whip->getValidIpAddress($request);

Other request formats can be supported via a RequestAdapter (src/Request/RequestAdapter) implementation., (*24)

The Versions

30/10 2017

dev-master

9999999-dev https://github.com/Vectorface/whip

A PHP class for retrieving accurate IP address information for the client.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Cory Darby
by Daniel Bruce

cdn ip cloudflare

30/10 2017

dev-dev

dev-dev https://github.com/Vectorface/whip

A PHP class for retrieving accurate IP address information for the client.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Cory Darby
by Daniel Bruce

cdn ip cloudflare

30/10 2017

v0.3.2

0.3.2.0 https://github.com/Vectorface/whip

A PHP class for retrieving accurate IP address information for the client.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Cory Darby
by Daniel Bruce

cdn ip cloudflare

27/01 2016

v0.3.1

0.3.1.0 https://github.com/Vectorface/whip

A PHP class for retrieving accurate IP address information for the client.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Cory Darby
by Daniel Bruce

cdn ip cloudflare

22/01 2016

v0.3.0

0.3.0.0 https://github.com/Vectorface/whip

A PHP class for retrieving accurate IP address information for the client.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Cory Darby
by Daniel Bruce

cdn ip cloudflare

14/12 2015

v0.2.1

0.2.1.0 https://github.com/Vectorface/whip

A PHP class for retrieving accurate IP address information for the client.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Cory Darby
by Daniel Bruce

cdn ip cloudflare

06/10 2014

v0.2.0

0.2.0.0 https://github.com/Vectorface/whip

A PHP class for retrieving accurate IP address information for the client.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Cory Darby
by Daniel Bruce

cdn ip cloudflare

03/10 2014

v0.1.0

0.1.0.0 https://github.com/Vectorface/whip

A PHP class for retrieving accurate IP address information for the client.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Cory Darby
by Daniel Bruce

cdn ip cloudflare