rafalmasiarek/real-ip-resolver
最新稳定版本:1.2.1
Composer 安装命令:
composer require rafalmasiarek/real-ip-resolver
包简介
Simple and extensible real IP resolver for PHP behind proxies and load balancers.
README 文档
README
A lightweight PHP library to resolve the real client IP address, with optional support for trusted proxy lists like Cloudflare, AWS, or localhost.
Installation
composer require rafalmasiarek/real-ip-resolver
Basic Usage (no trusted proxy)
If you're not behind any proxies or load balancers, you can use the resolver directly:
use rafalmasiarek\Http\RealIpResolver\RealIpResolver; $resolver = new RealIpResolver(); $realIp = $resolver->getIp(); echo "Real IP: " . $realIp;
Advanced Usage (with trusted proxies)
You can specify trusted proxy IP ranges using predefined providers:
use rafalmasiarek\RealIpResolver; use rafalmasiarek\RealIpResolver\TrustedProxy; use rafalmasiarek\RealIpResolver\IPLists\Cloudflare; use rafalmasiarek\RealIpResolver\IPLists\Localhost; $trustedIps = array_merge( Localhost::get(), Cloudflare::get() ); $trustedProxy = new TrustedProxy($trustedIps);$resolver = new RealIpResolver($trustedProxy); $realIp = $resolver->getIp(); echo "Real IP: " . $realIp;
Creating a Custom IP List
To define your own trusted proxy list, implement the IpListInterface:
namespace rafalmasiarek\RealIpResolver\IPLists; class MyCustomProxy implements IpListInterface { public static function get(): array { return [ '203.0.113.5', '203.0.113.6', '2001:db8::abcd:1234', ]; } }
Using it:
use rafalmasiarek\RealIpResolver; use rafalmasiarek\RealIpResolver\TrustedProxy; use rafalmasiarek\RealIpResolver\IPLists\MyCustomProxy; $trustedIps = MyCustomProxy::get(); $trustedProxy = new TrustedProxy($trustedIps); $resolver = new RealIpResolver($trustedProxy);
Namespace Change in 1.2.0
Starting from version 1.2.0, the library uses a cleaner and flatter namespace structure.
What changed
Old namespace (before 1.2.0):
rafalmasiarek\Http\RealIpResolver\
New namespace (1.2.0 and later):
rafalmasiarek\RealIpResolver\
Updated imports
Before:
use rafalmasiarek\Http\RealIpResolver\RealIpResolver;
After:
use rafalmasiarek\RealIpResolver; use rafalmasiarek\RealIpResolver\TrustedProxy; use rafalmasiarek\RealIpResolver\IPLists\Cloudflare; use rafalmasiarek\RealIpResolver\IPLists\Localhost;
All users should update their imports accordingly.
License
MIT
统计信息
- 总下载量: 10
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-07-24