Giter Club home page Giter Club logo

php-client-ip's Introduction

php Client IP

Get client IP with safe and coincident way from server even behind Proxy or Load-Balancer.

Latest Stable Version License Total Downloads Monthly Downloads

Real IP implement on Web application, which solve the problem that the server receiving requests through trust proxies or load-balancers without Transparent-Mode.


DEMONSTRATION

echo ClientIP::get();
ClientIP::config([
    'proxyIPs' => ['192.168.0.0/16', '172.217.3.11'],
    'headerKeys' => ['HTTP_X_FORWARDED_FOR']
    ]);
echo ClientIP::get();

If the client IP is 203.169.1.37, there are some connection situation for demonstrating referring by above sample code:

Load-Balancer normal network

your server is behind a Load-Balencer and in a private network.

Client Load-Balancer Server
203.169.1.37 → 172.217.2.88 ↓
192.168.0.10 → 192.168.4.100
ClientIP::config([
    'proxyIPs' => true
    ]);

Setting proxyIPs as true means all requests are go through Load-balancer, which will always get forward IP, same as above setting:

ClientIP::config([
    'proxyIPs' => ['0.0.0.0/32']
    ]);

The result from the server:

192.168.0.10 //Before setting the config
203.169.1.37 //After setting the config, get the forward IP

Proxy optional network

If your server is in public network, not only receives requests directly, but also supports trust proxies for going through:

Client Proxy Server
Way 1 203.169.1.37 → 172.217.4.100
Way 2 203.169.1.37 → 172.217.2.89 ↓
172.217.3.11 → 172.217.4.100
ClientIP::config([
    'proxyIPs' => ['172.217.3.11']
    ]);

The result from the server

  • Way 1: Client connect to server directly:
203.169.1.37 //Before setting the config
203.169.1.37 //The request IP is not from proxyIPs, so identify as a Client.
  • Way 2: Client connect to server through Proxy:
172.217.3.11 //Before setting the config
203.169.1.37 //The request IP comes from proxyIPs, get the forward IP.

INSTALLATION

Run Composer in your project:

composer require yidas/client-ip

Then initialize it at the bootstrap of application such as config file:

require __DIR__ . '/vendor/autoload.php';
ClientIP::config([
   'proxyIPs' => ['192.168.0.0/16']
   ]);

CONFIGURATION

Example configuration:

ClientIP::config([
   'proxyIPs' => ['192.168.0.0/16', '172.217.2.89'],
   'headerKeys' => ['HTTP_X_FORWARDED_FOR'],
   ]);
Attribute Type Description
proxyIPs   array Trust Proxies' IP list, which support subnet mask for each IP set.
headerKeys array Header Key list for IP Forward.

DISCUSSION

Implement from Web Server

The another way to fetch real IP is implemnet from the Web server side:

php-client-ip's People

Contributors

yidas avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.