Created
August 2, 2016 11:49
-
-
Save h1k3r/487d89e127c0dcf003d74fd7295fc6c8 to your computer and use it in GitHub Desktop.
Anonymize IP according like Google Analytics (according to German laws)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Last byte for IPv4 and last 80 bits for IPv6 are set to zero | |
* @see https://support.google.com/analytics/answer/2763052?hl=en | |
*/ | |
function anonymizeIp($ip) | |
{ | |
$binary = inet_pton($ip); | |
if ($binary === false) { | |
return false; | |
} | |
if (strlen($binary) === 16) { | |
$maskIp = inet_pton('ffff:ffff:ffff:0000:0000:0000:0000:0000'); | |
} else { | |
$maskIp = inet_pton('255.255.255.0'); | |
} | |
return inet_ntop($binary & $maskIp); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment