*/ namespace App\Utils; use Hyperf\Context\ApplicationContext; use Hyperf\HttpServer\Contract\RequestInterface; class Ip { /** * Author: cfn * @return string */ public static function ip(): string { $container = ApplicationContext::getContainer(); $request = $container->get(RequestInterface::class); $res = $request->getHeaders(); if (isset($res['http_client_ip'])) { return $res['http_client_ip'][0]; } elseif (isset($res['x-real-ip'])) { return $res['x-real-ip'][0]; } elseif (isset($res['x-forwarded-for'])) { return $res['x-forwarded-for'][0]; } else { $serverParams = $request->getServerParams(); return $serverParams['remote_addr'][0]; } } /** * Author: cfn * @return string */ public static function ua(): string { $container = ApplicationContext::getContainer(); $request = $container->get(RequestInterface::class); return $request->header("user-agent", "unknown"); } public static function getBrowserAndOS(string $userAgent): array { $browser = 'Unknown'; $os = 'Unknown'; // 操作系统识别 if (preg_match('/windows/i', $userAgent)) { $os = 'Windows'; } elseif (preg_match('/macintosh|mac os x/i', $userAgent)) { $os = 'Mac OS'; } elseif (preg_match('/linux/i', $userAgent)) { $os = 'Linux'; } elseif (preg_match('/iphone/i', $userAgent)) { $os = 'iPhone'; } elseif (preg_match('/android/i', $userAgent)) { $os = 'Android'; } // 浏览器识别 if (preg_match('/MSIE|Trident/i', $userAgent)) { $browser = 'Internet Explorer'; } elseif (preg_match('/Edge/i', $userAgent)) { $browser = 'Microsoft Edge'; } elseif (preg_match('/Firefox/i', $userAgent)) { $browser = 'Mozilla Firefox'; } elseif (preg_match('/Chrome/i', $userAgent)) { $browser = 'Google Chrome'; } elseif (preg_match('/Safari/i', $userAgent)) { $browser = 'Safari'; } elseif (preg_match('/Opera|OPR/i', $userAgent)) { $browser = 'Opera'; } return [ 'browser' => $browser, 'os' => $os, ]; } }