php 正则判断 ipv4 ipv6

180it 2021-11-12 PM 1660℃ 0条

<?php
header('Content-type: application/json');

if ( array_key_exists('addr', $_GET) )

$ip = $_GET['addr'];

else

$ip = $_SERVER['REMOTE_ADDR'];

$v6 = preg_match("/^[0-9a-f]{1,4}:([0-9a-f]{0,4}:){1,6}[0-9a-f]{1,4}$/", $ip);
$v4 = preg_match("/^([0-9]{1,3}\.){3}[0-9]{1,3}$/", $ip);

if ( $v6 != 0 )

$type = "IPv6";

elseif ( $v4 != 0 )

$type = "IPv4";

else

$type = "unknown";

echo json_encode(array("ip" => $ip, "type" => $type));
?>

支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

php 正则判断 ipv4 ipv6