2023-02-17 17:10:16 +08:00

33 lines
829 B
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Utils;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Di\Annotation\Inject;
class Http
{
#[Inject]
protected RequestInterface $request;
/**
* 获取客户端ip地址
* @return string
*/
public function getIp():string
{
$res = $this->request->getServerParams();
if (isset($res['http_client_ip'])) {
return $res['http_client_ip'];
} elseif (isset($res['http_x_real_ip'])) {
return $res['http_x_real_ip'];
} elseif (isset($res['http_x_forwarded_for'])) {
//部分CDN会获取多层代理IP所以转成数组取第一个值
$arr = explode(',', $res['http_x_forwarded_for']);
return $arr[0];
} else {
return $res['remote_addr'];
}
}
}