Digital Survivors
 

Displaying Partial IP Addresses Using PHP

Scott Manning
August 25, 2003

For those of you who run community-type websites where people can post comments directly to your pages, you know the woes of dealing with people who post under fake names. Although I'm all for freedom of speech and privacy, when the same guy posts 15 times under names like "Bob Joe", "Jan", "ODB", and "Bill Clinton", it gets a little annoying. It is especially annoying when the same person is trying to create the illusion that there are 15 different people with the same opinion as them.

To deal with this, I'm now displaying partial IP Addresses next to comments on any site I work on. An IP Address of 666.66.78.234 will now be displayed next to a username as xxx.66.78.234 (Example). It still gives a little privacy, but allows readers to realize that Bob Joe, Jan, ODB, and Bill Clinton are all the same person.

How to display the partial IP Addresses
If you can at least get the IP address, you can display part of it using PHP. The code looks like so:

<?php
$ip = "999.99.99.999";
$quads = split('\.', $ip);
$quads[0] = ereg_replace("[0-9]", "x", $quads[0]);
$concealed = join(".", $quads);
print $concealed;
?>

"999.99.99.999" would of course be the IP Address you want to display partially. This code takes all numbers before the first period (.) in an IP Address and replaces them with x's. The final result in the above example would be xxx.99.99.999

Using this code with Movable Type
If you already have comments set up on Movable Type, you can add the following code to implement the same feature:

<?php
$ip = "<$MTCommentIP$>";
$quads = split('\.', $ip);
$quads[0] = ereg_replace("[0-9]", "X", $quads[0]);
$concealed = join(".", $quads);
print $concealed;
?>

The above code would need to be in between the <MTComments> tags.

Using this code with Greymatter
You can use the following code to implement the same feature in Greymatter:

<?php
$ip = "{{commentauthorip}}";
$quads = split('\.', $ip);
$quads[0] = ereg_replace("[0-9]", "X", $quads[0]);
$concealed = join(".", $quads);
print $concealed;
?>

Using this code on non-PHP pages
If you at least have PHP installed on your server, you can use this code on web pages even if they don't have the .php extension.

First, create a file in your root web directory called ".htaccess" (without the quotation marks). The root web directory is where your index file would reside.

In the file, put the following code:
AddType application/x-httpd-php .html .htm

The above code makes it so files with a .html or .htm extension will be treated like PHP files. To add more extension types, just add them at the end of the code. Be sure to put a space between each type.

That's it.

Hopefully, if more and more people start using this code, we can put a choke on the anonymous people who post under fake names. We may also reveal all those people who seem honest, but have 15 different sock puppets in their arsenal.

If you use this code, post a link to your site here. I'd love to see it in action.