Mood Swing » Nostalgic

Journal » Post

pngfix
posted in: code at 08:17 am
0 Reactions

Lack of better things to do I decided to update my guestbook by adding a simple feature which indicates from what country the poster is from. Inspired by an online tracking site I used to maintain, I researched a way to do it in a much easier and quicker fashion.

First up, I get an ip range list which is offered for free by some sites, I get mine from MaxMind. They update this list once a month for the free version. After downloading, I enter them into a sql table for faster querying; then with a few lines of code I get the information I need... hooray!. The trick is to convert the ip address you have to a proper address. Once you have that value you can compare it to your table to return the appropreate country, like so:

 
$ip 
$_SERVER['REMOTE_ADDR'];
$code sprintf("%u"ip2long($ip));

$data mysql_query("SELECT * FROM ip2country WHERE long1 <= $code AND long2 >= $code");
$row mysql_fetch_object($data);
echo 
$row->cc
 

That's basically the gist of the script, maybe some error trapping would be nice. IIf you don't have access to a sql database you can alway manually look it up on the csv files or something, which would be slower.

Talkback

No reactions

Post a reaction



 


*Enter the characters displayed on the image below on the verify image text box above. Note that this is case sensitive.

captcha image

 
Back to top
contact