Skip to content
Advertisement

Tag: ipv6

Anonymize IPv4 and IPv6 addresses with PHP preg_replace?

I needed to anonymize IPv4 and IPv6 addresses so I coded this crude solution: It works fine with full length IPv4 and IPv6 addresses like 207.142.131.005 2001:0db8:0000:08d3:0000:8a2e:0070:7344 but not with abbreviated addresses like 207.142.131.5 2001:0db8::8d3::8a2e:7:7344 I wonder if there is an elegant solution with preg_replace and some regular expression magic? Answer No conditional is necessary. You can write two patterns

How can I check in PHP if 2 IPv6-Adresses are equal?

I want to check if an IP is whitelisted, but IPv6-Adresses can have different representations: For example 2001:0DB8:0:0:1::1 is the short form of 2001:0db8:0000:0000:0001:0000:0000:0001. Thus, string comparison doesn’t work. How can I know two adresses are equal? Answer Use inet_pton. (However, this will only work if PHP was not compiled with –disable-ipv6 option. You can check this by using if

Advertisement