Skip to content

Instantly share code, notes, and snippets.

@h1k3r
Created August 2, 2016 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save h1k3r/487d89e127c0dcf003d74fd7295fc6c8 to your computer and use it in GitHub Desktop.
Save h1k3r/487d89e127c0dcf003d74fd7295fc6c8 to your computer and use it in GitHub Desktop.
Anonymize IP according like Google Analytics (according to German laws)
<?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