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

Installs: 68 125

Dependents: 0

Suggesters: 0

Security: 0

Stars: 22

Watchers: 2

Forks: 8

Open Issues: 0


1.0.0 2017-09-05 09:23 UTC

This package is auto-updated.

Last update: 2024-05-14 18:31:13 UTC


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.


echo ClientIP::get();
    'proxyIPs' => ['', ''],
    'headerKeys' => ['HTTP_X_FORWARDED_FOR']
echo ClientIP::get();

If the client IP is, 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 → ↓ →
    'proxyIPs' => true

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

    'proxyIPs' => ['']

The result from the server: //Before setting the config //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 →
Way 2 → ↓ →
    'proxyIPs' => ['']

The result from the server

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


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';
   'proxyIPs' => ['']


Example configuration:

   'proxyIPs' => ['', ''],
   '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.


Implement from Web Server

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