You may already know how to check IP v4 is in range using ip2long function, but it doesn’t support ipv6 so we have to find another way if we want to support ipv6 addresses as well. Here I wrote small function that will check ipv4 and ipv6 too.
1 2 3 4 5 6 |
function isIPBetweenRange($ip,$startIP,$endIP){ if(inet_pton($ip)>=inet_pton($startIP) && inet_pton($ip)<=inet_pton($endIP)) { return true; } return false; } |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
// check ipv4 range $startIP = "192.168.0.10"; $endIP = "192.168.0.100"; $ip = "192.168.0.45"; if(isIPBetweenRange($ip,$startIP,$endIP)) { echo "In range!<br>"; } else { echo "Out of range!<br>"; } // check ipv6 range $startIP = "2001:0db8:85a3:0000:0000:8a2e:0370:7334"; $endIP = "2001:0db8:85a3:0000:0000:8a2e:0370:7528"; $ip = "2001:0db8:85a3:0000:0000:8a2e:0370:7461"; if(isIPBetweenRange($ip,$startIP,$endIP)) { echo "In range!<br>"; } else { echo "Out of range!<br>"; } |