|
1 | | -# php-ip |
| 1 | +*php* Client IP |
| 2 | +=============== |
| 3 | + |
| 4 | +Get client IP with safe and coincident way from server even behind Proxy or Load-Balancer. |
| 5 | + |
| 6 | +[](https://packagist.org/packages/yidas/client-ip) |
| 7 | +[](https://packagist.org/packages/yidas/client-ip) |
| 8 | +[](https://packagist.org/packages/yidas/client-ip) |
| 9 | + |
| 10 | +Real IP implement on Web application, which solve the problem that the server receiving requests through trust proxies or load-balancers without Transparent-Mode. |
| 11 | + |
| 12 | +--- |
| 13 | + |
| 14 | +DEMONSTRATION |
| 15 | +------------- |
| 16 | + |
| 17 | +```php |
| 18 | +echo ClientIP::get(); |
| 19 | +ClientIP::config([ |
| 20 | + 'proxyIPs' => ['192.168.0.0/16', '172.217.3.11'], |
| 21 | + 'headerKeys' => ['HTTP_X_FORWARDED_FOR'] |
| 22 | + ]); |
| 23 | +echo ClientIP::get(); |
| 24 | +``` |
| 25 | + |
| 26 | +If the client IP is `203.169.1.37`, there are some connection situation for demonstrating referring by above sample code: |
| 27 | + |
| 28 | +### Load-Balancer normal network |
| 29 | + |
| 30 | +your server is behind a Load-Balencer and in a private network. |
| 31 | + |
| 32 | +| Client | Load-Balancer | Server | |
| 33 | +|:--------------:|:--------------:|:-------------:| |
| 34 | +| 203.169.1.37 → | 172.217.2.88 ↓ | | |
| 35 | +| | 192.168.0.10 → | 192.168.4.100 | |
| 36 | + |
| 37 | +```php |
| 38 | +ClientIP::config([ |
| 39 | + 'proxyIPs' => true |
| 40 | + ]); |
| 41 | +``` |
| 42 | + |
| 43 | +Setting `proxyIPs` as `true` means all requests are go through Load-balancer, which will always get forward IP, same as above setting: |
| 44 | + |
| 45 | +```php |
| 46 | +ClientIP::config([ |
| 47 | + 'proxyIPs' => ['0.0.0.0/32'] |
| 48 | + ]); |
| 49 | +``` |
| 50 | + |
| 51 | +**The result from the server:** |
| 52 | + |
| 53 | +``` |
| 54 | +192.168.0.10 //Before setting the config |
| 55 | +203.169.1.37 //After setting the config, get the forward IP |
| 56 | +``` |
| 57 | + |
| 58 | +### Proxy optional network |
| 59 | + |
| 60 | +If your server is in public network, not only receives requests directly, but also supports trust proxies for going through: |
| 61 | + |
| 62 | +| | Client | Proxy | Server | |
| 63 | +|:---:|:--------------:|:--------------:|:-------------:| |
| 64 | +|Way 1| 203.169.1.37 → | | 172.217.4.100 | |
| 65 | +|Way 2| 203.169.1.37 → | 172.217.2.89 ↓ | | |
| 66 | +| | | 172.217.3.11 → | 172.217.4.100 | |
| 67 | + |
| 68 | +```php |
| 69 | +ClientIP::config([ |
| 70 | + 'proxyIPs' => ['172.217.3.11'] |
| 71 | + ]); |
| 72 | +``` |
| 73 | + |
| 74 | +**The result from the server** |
| 75 | + |
| 76 | +- Way 1: Client connect to server directly: |
| 77 | + |
| 78 | +``` |
| 79 | +203.169.1.37 //Before setting the config |
| 80 | +203.169.1.37 //The request IP is not from proxyIPs, so identify as a Client. |
| 81 | +``` |
| 82 | + |
| 83 | +- Way 2: Client connect to server through Proxy: |
| 84 | + |
| 85 | +``` |
| 86 | +172.217.3.11 //Before setting the config |
| 87 | +203.169.1.37 //The request IP comes from proxyIPs, get the forward IP. |
| 88 | +``` |
| 89 | + |
| 90 | + |
| 91 | +--- |
| 92 | + |
| 93 | +INSTALLATION |
| 94 | +------------ |
| 95 | + |
| 96 | +Run Composer in your project: |
| 97 | + |
| 98 | + composer require yidas/client-ip |
| 99 | + |
| 100 | +Then initialize it at the bootstrap of application such as `config` file: |
| 101 | + |
| 102 | +```php |
| 103 | +require __DIR__ . '/vendor/autoload.php'; |
| 104 | +ClientIP::config([ |
| 105 | + 'proxyIPs' => ['192.168.0.0/16'] |
| 106 | + ]); |
| 107 | +``` |
| 108 | + |
| 109 | +--- |
| 110 | + |
| 111 | +CONFIGURATION |
| 112 | +------------- |
| 113 | + |
| 114 | +Example configuration: |
| 115 | + |
| 116 | +```php |
| 117 | +ClientIP::config([ |
| 118 | + 'proxyIPs' => ['192.168.0.0/16', '172.217.2.89'], |
| 119 | + 'headerKeys' => ['HTTP_X_FORWARDED_FOR'], |
| 120 | + ]); |
| 121 | +``` |
| 122 | + |
| 123 | +| Attribute | Type | Description | |
| 124 | +|-----------|-------|-------------| |
| 125 | +|proxyIPs |array |Trust Proxies' IP list, which support subnet mask for each IP set.| |
| 126 | +|headerKeys |array |Header Key list for IP Forward.| |
| 127 | + |
| 128 | + |
0 commit comments