Avec de vrais morceaux de 2.0 !

PHP IP utils: Normalize IPv6 address, generate random IPv4 or v6 address and more

I've published some PHP functions useful for IP stuff on KD2 SVN : http://svn.kd2.org/svn/misc/libs/tools/ip_utils.php

The first one, normalize_ipv6 is used to get a normalized notation of an IPv6 (following the RFC), eg. ::1 will be transformed in 0000:0000:0000:0000:0000:0000:0000:0001 and 2001:db8:85a3::8a2e:370:7334 in 2001:0db8:85a3:0000:0000:8a2e:0370:7334. It's useful for storing or parsing IPv6 addresses.

Then the other functions are useful for making your visitors more anonymous than usual or do some fun stuff. get_random_ip will return a random IP, either IPv4 or IPv6 depending on the arguments you're passing. You can also generate a unique random IP address from an existing one.

From that you can use the filter_remote_client function, it will replace automatically IP addresses by anonymous ones in all $_SERVER headers. A simple example:

_SERVER remote client IPs unfiltered:
[REMOTE_ADDR] => 2001:e05a:2439:5460:10c:e4ff:fe30:6481

_SERVER remote client IPs filtered:
[REMOTE_ADDR] => ebd1:2901:58d6:150f:6b5c:0841:e6e1:37f0

You'll always get the same values for the same original adresses. If you put this in a prepend_file of your php.ini, your PHP applications will never know the true IP address of the client but that won't matter to them, they will still get valid IP addresses and as long as the real client IP address won't change, the fake one won't change either.

Re-use this code as long as you want, it's under the do what the fuck you want public license.

Note: here's more ideas on how to store IPv6 addresses and other stuff.

Écrire un commentaire
  ___(_)___  ___  __ _ _   ___  __
 / __| / __|/ _ \/ _` | | | \ \/ /
| (__| \__ \  __/ (_| | |_| |>  < 

Les adresses internet seront converties automatiquement.
Tags autorisés : <blockquote> <cite> <pre> <code> <var> <strong> <em> <del> <ins> <kbd> <samp> <abbr>