You can also use filter_var :
if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
echo "IPV6 valid.";
} else {
echo "Not valid.";
}
You can find the list of filters here.
题
Right now I'm using strcmp but that's not really optimal.
So, what's the best way to do it?
解决方案
You can also use filter_var :
if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
echo "IPV6 valid.";
} else {
echo "Not valid.";
}
You can find the list of filters here.
其他提示
Use strcmp instead of strcpy. That must be the issue.
You can just check, if a :
is in the ip-adress:
if (strpos($_SERVER["REMOTE_ADDR"],":") !== false) //....IPv6
else //....IPv4
Use inet_pton. Also works for ipv4:
function ip_validate($ip){
return inet_pton($ip) !== false;
}
$_SERVER["REMOTE_ADDR"]
this is your buddy. It checks for the adress. I suppose the simplest way would be to check this address for a non-numeric sign. If it contains one, its a IPv6 address. Maybe there are better or more elegant ones, but ive never used it because no provider is using IPv6 yet.