hyperf获取客户端真实ip
作者:互联网
hyperf框架获取客户端真实ip
use Hyperf\Utils\ApplicationContext;
use Hyperf\HttpServer\Contract\RequestInterface;
function getRealIp(): string
{
$request = ApplicationContext::getContainer()->get(RequestInterface::class);
$headers = $request->getHeaders();
if(isset($headers['x-forwarded-for'][0]) && !empty($headers['x-forwarded-for'][0])) {
return $headers['x-forwarded-for'][0];
} elseif (isset($headers['x-real-ip'][0]) && !empty($headers['x-real-ip'][0])) {
return $headers['x-real-ip'][0];
}
$serverParams = $request->getServerParams();
return $serverParams['remote_addr'] ?? '';
}
标签:real,return,forwarded,ip,request,headers,hyperf,客户端 来源: https://blog.csdn.net/jianai_2018/article/details/121684902