├── .gitignore ├── LICENSE ├── README.md ├── composer.json ├── example.php └── src └── IpAnonymizer.php /.gitignore: -------------------------------------------------------------------------------- 1 | vendor/ 2 | .idea/ 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Geert Wirken 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Latest Stable Version](https://img.shields.io/packagist/v/geertw/ip-anonymizer.svg)](https://packagist.org/packages/geertw/ip-anonymizer) 2 | [![Total Downloads](https://img.shields.io/packagist/dt/geertw/ip-anonymizer.svg)](https://packagist.org/packages/geertw/ip-anonymizer) 3 | [![License](https://img.shields.io/packagist/l/geertw/ip-anonymizer.svg)](https://packagist.org/packages/geertw/ip-anonymizer) 4 | 5 | # IP address anonymizer for PHP 6 | 7 | This is a library for PHP to anonymize IP addresses. This makes it easier to respect user privacy, and it makes it more 8 | difficult to identify an end user by his IP address. Anonymizing IP addresses can be useful for a lot of cases where the 9 | exact IP address is not important or even undesired, for example in a statistical analysis. 10 | 11 | This library supports both IPv4 and IPv6 addresses. Addresses are anonymized to their network ID. 12 | 13 | The default settings anonymize an IP address to a /24 subnet (IPv4) or a /64 subnet (IPv6), but these can be customized. 14 | 15 | For instance, the IPv4 address `192.168.178.123` is anonymized by default to `192.168.178.0`. 16 | 17 | The IPv6 address `2a03:2880:2110:df07:face:b00c::1` is anonymized by default to `2a03:2880:2110:df07::`. 18 | 19 | ## Example 20 | 21 | ```php 22 | anonymize('127.0.0.1')); 29 | // returns 127.0.0.0 30 | 31 | var_dump($ipAnonymizer->anonymize('192.168.178.123')); 32 | // returns 192.168.178.0 33 | 34 | var_dump($ipAnonymizer->anonymize('8.8.8.8')); 35 | // returns 8.8.8.0 36 | 37 | var_dump($ipAnonymizer->anonymize('::1')); 38 | // returns :: 39 | 40 | var_dump($ipAnonymizer->anonymize('::127.0.0.1')); 41 | // returns :: 42 | 43 | var_dump($ipAnonymizer->anonymize('2a03:2880:2110:df07:face:b00c::1')); 44 | // returns 2a03:2880:2110:df07:: 45 | 46 | var_dump($ipAnonymizer->anonymize('2610:28:3090:3001:dead:beef:cafe:fed3')); 47 | // returns 2610:28:3090:3001:: 48 | 49 | // Use a custom mask: 50 | $ipAnonymizer->ipv4NetMask = "255.255.0.0"; 51 | var_dump($ipAnonymizer->anonymize('192.168.178.123')); 52 | // returns 192.168.0.0 53 | 54 | // You can use this class also in a static way: 55 | var_dump(IpAnonymizer::anonymizeIp('192.168.178.123')); 56 | // returns 192.168.178.0 57 | 58 | var_dump(IpAnonymizer::anonymizeIp('2610:28:3090:3001:dead:beef:cafe:fed3')); 59 | // returns 2610:28:3090:3001:: 60 | ``` 61 | 62 | ## License 63 | 64 | This library is licensed under the MIT License. See the [LICENSE](LICENSE) file for the full license. 65 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "geertw/ip-anonymizer", 3 | "description": "IPv4 and IPv6 address anonymizer", 4 | "type": "library", 5 | "require-dev": { 6 | }, 7 | "license": "MIT", 8 | "authors": [ 9 | { 10 | "name": "Geert Wirken", 11 | "email": "geert@gwirken.nl" 12 | } 13 | ], 14 | "require": {}, 15 | "autoload": { 16 | "psr-4": { 17 | "geertw\\IpAnonymizer\\": "src" 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /example.php: -------------------------------------------------------------------------------- 1 | anonymize('127.0.0.1')); 9 | // returns 127.0.0.0 10 | 11 | var_dump($ipAnonymizer->anonymize('192.168.178.123')); 12 | // returns 192.168.178.0 13 | 14 | var_dump($ipAnonymizer->anonymize('8.8.8.8')); 15 | // returns 8.8.8.0 16 | 17 | var_dump($ipAnonymizer->anonymize('::1')); 18 | // returns :: 19 | 20 | var_dump($ipAnonymizer->anonymize('::127.0.0.1')); 21 | // returns :: 22 | 23 | var_dump($ipAnonymizer->anonymize('2a03:2880:2110:df07:face:b00c::1')); 24 | // returns 2a03:2880:2110:df07:: 25 | 26 | var_dump($ipAnonymizer->anonymize('2610:28:3090:3001:dead:beef:cafe:fed3')); 27 | // returns 2610:28:3090:3001:: 28 | 29 | // Use a custom mask: 30 | $ipAnonymizer->ipv4NetMask = "255.255.0.0"; 31 | var_dump($ipAnonymizer->anonymize('192.168.178.123')); 32 | // returns 192.168.0.0 33 | 34 | // You can use this class also in a static way: 35 | var_dump(IpAnonymizer::anonymizeIp('192.168.178.123')); 36 | // returns 192.168.178.0 37 | 38 | var_dump(IpAnonymizer::anonymizeIp('2610:28:3090:3001:dead:beef:cafe:fed3')); 39 | // returns 2610:28:3090:3001:: 40 | -------------------------------------------------------------------------------- /src/IpAnonymizer.php: -------------------------------------------------------------------------------- 1 | anonymize($address); 25 | } 26 | 27 | /** 28 | * Anonymize an IPv4 or IPv6 address. 29 | * 30 | * @param $address string IP address that must be anonymized 31 | * @return string The anonymized IP address. Returns an empty string when the IP address is invalid. 32 | */ 33 | public function anonymize($address) { 34 | $packedAddress = inet_pton($address); 35 | 36 | if (strlen($packedAddress) == 4) { 37 | return $this->anonymizeIPv4($address); 38 | } elseif (strlen($packedAddress) == 16) { 39 | return $this->anonymizeIPv6($address); 40 | } else { 41 | return ""; 42 | } 43 | } 44 | 45 | /** 46 | * Anonymize an IPv4 address 47 | * @param $address string IPv4 address 48 | * @return string Anonymized address 49 | */ 50 | public function anonymizeIPv4($address) { 51 | return inet_ntop(inet_pton($address) & inet_pton($this->ipv4NetMask)); 52 | } 53 | 54 | /** 55 | * Anonymize an IPv6 address 56 | * @param $address string IPv6 address 57 | * @return string Anonymized address 58 | */ 59 | public function anonymizeIPv6($address) { 60 | return inet_ntop(inet_pton($address) & inet_pton($this->ipv6NetMask)); 61 | } 62 | } 63 | --------------------------------------------------------------------------------