时间:2021-07-01 10:21:17 帮助过:20人阅读
function check_ip($str)
{
return preg_match('/""A((([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))"".){3}(([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))""Z/',$str)>0;
}
function getIP($isHeader=false)
{
if($isHeader)
{
if (isset($_SERVER['HTTP_CLIENT_IP']) && check_ip($_SERVER['HTTP_CLIENT_IP']))
{
return $_SERVER['HTTP_CLIENT_IP'];
}
else if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && check_ip($_SERVER['HTTP_X_FORWARDED_FOR']))
{
return $_SERVER['HTTP_X_FORWARDED_FOR'];
}
}
if (isset($_SERVER['REMOTE_ADDR']) && check_ip($_SERVER['REMOTE_ADDR']))
{
return $_SERVER['REMOTE_ADDR'];
}
else
{
return 'unknown';
}
}
希望根据不同的ip地址段,网站显示不同的内容。目前针对ipv4的地址段做了限制,但是有些客户端访问是通过ipv6的地址访问,做的限制就没有用了。所以我想问一下,有没有可能通过php只获取ipv4的代码。
目前使用的php获取ip的代码如下:
function check_ip($str)
{
return preg_match('/""A((([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))"".){3}(([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))""Z/',$str)>0;
}
function getIP($isHeader=false)
{
if($isHeader)
{
if (isset($_SERVER['HTTP_CLIENT_IP']) && check_ip($_SERVER['HTTP_CLIENT_IP']))
{
return $_SERVER['HTTP_CLIENT_IP'];
}
else if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && check_ip($_SERVER['HTTP_X_FORWARDED_FOR']))
{
return $_SERVER['HTTP_X_FORWARDED_FOR'];
}
}
if (isset($_SERVER['REMOTE_ADDR']) && check_ip($_SERVER['REMOTE_ADDR']))
{
return $_SERVER['REMOTE_ADDR'];
}
else
{
return 'unknown';
}
}