60 lines
1.3 KiB
PHP
60 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Extend;
|
|
|
|
use Hyperf\Context\Context;
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
use function Hyperf\Support\env;
|
|
|
|
class SystemUtil
|
|
{
|
|
/**
|
|
* prod 1=生产环境 0=开发环境
|
|
* @return bool
|
|
*/
|
|
static function checkProEnv()
|
|
{
|
|
return Env('APP_ENV') == 'prod';
|
|
}
|
|
|
|
/**
|
|
* 获取客户端 ip
|
|
* @return mixed|string
|
|
*/
|
|
static function getClientIp()
|
|
{
|
|
$request = Context::get(ServerRequestInterface::class);
|
|
|
|
$ip_addr = $request->getHeaderLine('x-forwarded-for');
|
|
if (self::verifyIp($ip_addr)) {
|
|
return $ip_addr;
|
|
}
|
|
|
|
$ip_addr = $request->getHeaderLine('remote-host');
|
|
if (self::verifyIp($ip_addr)) {
|
|
return $ip_addr;
|
|
}
|
|
|
|
$ip_addr = $request->getHeaderLine('x-real-ip');
|
|
if (self::verifyIp($ip_addr)) {
|
|
return $ip_addr;
|
|
}
|
|
|
|
$ip_addr = $request->getServerParams()['remote_addr'] ?? '0.0.0.0';
|
|
if (self::verifyIp($ip_addr)) {
|
|
return $ip_addr;
|
|
}
|
|
|
|
return '0.0.0.0';
|
|
}
|
|
|
|
/**
|
|
* 验证ip
|
|
* @param $realIp
|
|
* @return mixed
|
|
*/
|
|
static function verifyIp($realIp): mixed
|
|
{
|
|
return filter_var($realIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
|
|
}
|
|
} |