{"id":15308196,"url":"https://github.com/yidas/php-client-ip","last_synced_at":"2025-06-12T14:07:43.536Z","repository":{"id":57086785,"uuid":"95657311","full_name":"yidas/php-client-ip","owner":"yidas","description":"Get client IP with safe and coincident way from server even behind Proxy or Load-Balancer","archived":false,"fork":false,"pushed_at":"2019-01-14T01:50:31.000Z","size":12,"stargazers_count":23,"open_issues_count":0,"forks_count":10,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-15T09:55:08.131Z","etag":null,"topics":["ip","load-balancer","php","proxy","real-ip","trust-proxies"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yidas.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-06-28T10:27:04.000Z","updated_at":"2024-08-20T14:30:24.000Z","dependencies_parsed_at":"2022-08-25T00:50:30.252Z","dependency_job_id":null,"html_url":"https://github.com/yidas/php-client-ip","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/yidas/php-client-ip","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidas%2Fphp-client-ip","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidas%2Fphp-client-ip/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidas%2Fphp-client-ip/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidas%2Fphp-client-ip/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yidas","download_url":"https://codeload.github.com/yidas/php-client-ip/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidas%2Fphp-client-ip/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259479594,"owners_count":22864362,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ip","load-balancer","php","proxy","real-ip","trust-proxies"],"created_at":"2024-10-01T08:14:29.228Z","updated_at":"2025-06-12T14:07:43.515Z","avatar_url":"https://github.com/yidas.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"*php* Client IP\n===============\n\nGet client IP with safe and coincident way from server even behind Proxy or Load-Balancer.\n\n[![Latest Stable Version](https://poser.pugx.org/yidas/client-ip/v/stable?format=flat-square)](https://packagist.org/packages/yidas/client-ip)\n[![License](https://poser.pugx.org/yidas/client-ip/license?format=flat-square)](https://packagist.org/packages/yidas/client-ip)\n[![Total Downloads](https://poser.pugx.org/yidas/client-ip/downloads?format=flat-square)](https://packagist.org/packages/yidas/client-ip)\n[![Monthly Downloads](https://poser.pugx.org/yidas/client-ip/d/monthly?format=flat-square)](https://packagist.org/packages/yidas/client-ip)\n\nReal IP implement on Web application, which solve the problem that the server receiving requests through trust proxies or load-balancers without Transparent-Mode.\n\n---\n\nDEMONSTRATION\n-------------\n\n```php\necho ClientIP::get();\nClientIP::config([\n    'proxyIPs' =\u003e ['192.168.0.0/16', '172.217.3.11'],\n    'headerKeys' =\u003e ['HTTP_X_FORWARDED_FOR']\n    ]);\necho ClientIP::get();\n```\n\nIf the client IP is `203.169.1.37`, there are some connection situation for demonstrating referring by above sample code:\n\n### Load-Balancer normal network\n\nyour server is behind a Load-Balencer and in a private network.\n\n| Client         | Load-Balancer  | Server        |\n|:--------------:|:--------------:|:-------------:|\n| 203.169.1.37 → | 172.217.2.88 ↓ |               |\n|                | 192.168.0.10 → | 192.168.4.100 |\n\n```php\nClientIP::config([\n    'proxyIPs' =\u003e true\n    ]);\n```\n\nSetting `proxyIPs` as `true` means all requests are go through Load-balancer, which will always get forward IP, same as above setting:\n\n```php\nClientIP::config([\n    'proxyIPs' =\u003e ['0.0.0.0/32']\n    ]);\n```\n\n**The result from the server:**\n\n```\n192.168.0.10 //Before setting the config\n203.169.1.37 //After setting the config, get the forward IP\n```\n\n### Proxy optional network\n\nIf your server is in public network, not only receives requests directly, but also supports trust proxies for going through:\n\n|     | Client         | Proxy          | Server        |\n|:---:|:--------------:|:--------------:|:-------------:|\n|Way 1| 203.169.1.37 → |                | 172.217.4.100 |\n|Way 2| 203.169.1.37 → | 172.217.2.89 ↓ |               |\n|     |                | 172.217.3.11 → | 172.217.4.100 |\n\n```php\nClientIP::config([\n    'proxyIPs' =\u003e ['172.217.3.11']\n    ]);\n```\n\n**The result from the server**\n\n- Way 1: Client connect to server directly:\n\n```\n203.169.1.37 //Before setting the config\n203.169.1.37 //The request IP is not from proxyIPs, so identify as a Client.\n```\n\n- Way 2: Client connect to server through Proxy:\n\n```\n172.217.3.11 //Before setting the config\n203.169.1.37 //The request IP comes from proxyIPs, get the forward IP.\n```\n\n\n---\n\nINSTALLATION\n------------\n\nRun Composer in your project:\n\n    composer require yidas/client-ip\n    \nThen initialize it at the bootstrap of application such as `config` file:\n\n```php\nrequire __DIR__ . '/vendor/autoload.php';\nClientIP::config([\n   'proxyIPs' =\u003e ['192.168.0.0/16']\n   ]);\n```\n\n---\n\nCONFIGURATION\n-------------\n\nExample configuration:\n\n```php\nClientIP::config([\n   'proxyIPs' =\u003e ['192.168.0.0/16', '172.217.2.89'],\n   'headerKeys' =\u003e ['HTTP_X_FORWARDED_FOR'],\n   ]);\n```\n\n| Attribute | Type  | Description |\n|-----------|-------|-------------|\n|proxyIPs   |array  |Trust Proxies' IP list, which support subnet mask for each IP set.|\n|headerKeys |array  |Header Key list for IP Forward.|\n\n---\n\nDISCUSSION\n----------\n\n### Implement from Web Server\n\nThe another way to fetch real IP is implemnet from the Web server side:\n\n- [Nginx: ngx_http_realip_module](http://nginx.org/en/docs/http/ngx_http_realip_module.html)\n\n- [Apache: mod_remoteip](https://httpd.apache.org/docs/trunk/mod/mod_remoteip.html)\n\n\n\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyidas%2Fphp-client-ip","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyidas%2Fphp-client-ip","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyidas%2Fphp-client-ip/lists"}