現(xiàn)在大部分網(wǎng)站會(huì)使用加速,thinkphp6中獲取IP的方法 request()->ip獲取到的IP會(huì)是代理IP,不是真實(shí)的。其實(shí)想要在thinkphp6中獲取真實(shí)的IP,只需要在全局的公共文件中加入以下代碼。
// 獲取真實(shí)IP
if (!function_exists("get_real_ip")) {
function get_real_ip()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$cip = $_SERVER['HTTP_CLIENT_IP'];
} else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
} else if (!empty($_SERVER["REMOTE_ADDR"])) {
$cip = $_SERVER["REMOTE_ADDR"];
} else {
$cip = '';
}
preg_match("/[\d\.]{7,15}/", $cip, $cips);
$cip = isset($cips[0]) ? $cips[0] : 'unknown';
unset($cips);
return $cip;
}
}然后在需要地方調(diào)用就可以了。
$ip = get_real_ip();
