This commit is contained in:
48
app/Service/SysService.php
Normal file
48
app/Service/SysService.php
Normal file
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
/**
|
||||
* Author: ykxiao
|
||||
* Date: 2025/6/4
|
||||
* Time: 上午9:51
|
||||
* Description:
|
||||
*
|
||||
* (c) ykxiao <yk_9001@hotmail.com>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
/**
|
||||
* Author: ykxiao
|
||||
* Date: 2025/6/4
|
||||
* Time: 上午10:19
|
||||
* Description: 系统服务
|
||||
*
|
||||
* (c) ykxiao <yk_9001@hotmail.com>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
class SysService
|
||||
{
|
||||
/**
|
||||
* 获取客户端IP信息
|
||||
* @param ServerRequestInterface $request
|
||||
* @return string
|
||||
*/
|
||||
public function getClientIpInfo(ServerRequestInterface $request): string
|
||||
{
|
||||
// 尝试从请求头获取客户端真实IP或转发的IP
|
||||
$realIP = $request->getHeaderLine('x-real-ip');
|
||||
$forwardedFor = $request->getHeaderLine('x-forwarded-for');
|
||||
// 确定客户端IP,优先使用 x-real-ip,其次是 x-forwarded-for,最后是默认IP
|
||||
$clientIP = $realIP ?: $forwardedFor;
|
||||
|
||||
return $clientIP ?: '127.0.0.1';
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user