Ip2Nation Improved.

It’s a bank holiday here in Hellas and I decided to take some time off work.

Being bored with ‘important’ stuff, I added a graphical country identifier to comments. It’s mostly an aesthetic addition, but might also prove useful in other ways. The information comes from comparing the commenter’s IP address (the address of his/her computer on the internet) with a cache of publicly available records. No personally identifiable information is transmitted to third parties nor is the IP of the commenter shown anywhere on this site. Just the country they are from.

I am using a heavily updated version of the free ip2nation database. Why and how is it updated, you might ask? Well, while checking it out, it became immediately apparent that a whole lot of EU networks were classified under ‘eu’ which (although charmingly idealistic) is too broad and generic to be of any use right now.

So I decided to write a small perl script that queried the RIPE (Réseaux IP Européens) whois server for all ‘eu’ blocks provided by ip2nation and updated the database accordingly. Because of the number of queries (and some pauses to play nice), this took a while to complete. As it turns out only a handful of the 2000 or so netblocks were actually registered as ‘eu’.

Get the PERL script that queries the RIPE whois servers and updates the ip2nation table here

Disclaimer: I accept no responsibility whatsoever for anything you might do with the script and any unexpected results your use of the code might have on your own or third party computer(s), network(s) etc. You’re assuming all responsibility for your use of the provided script, so don’t blame me if something goes wrong or if RIPE sues you for flooding their servers or misusing their data.

15 Responses to “Ip2Nation Improved.”

  1. S G says:

    good work!

    I guess you are using it in combination with this plugin and the small flags?

  2. cosmix says:

    Thank you.

    Indeed, I am. By the way, you can find which plugins any wordpress site is using just by navigating to http://site/wp-content/plugins/

    If you find errors in the database, please let me know. For example, assuming you’re the same S G that commented on other posts on this site, did you really go to España since last night, are you using AOL or is this just an error? :)

  3. S G says:

    heh no, don’t worry, I did go to Spain…

    could you maybe give me the exact code you are using? I can make it show the names of the countries but for some reason it doesnot show the flags (and it is not that it doesn’t find the flags, it is some other problem…)

  4. cosmix says:

    Ahh, great.

    Well, using the flags is no big deal. Use the wp_ozh_getCountryCode() function inside an tag, to form the name of the file containing the flag image. This is my code:

    <img alt="country flag" src="<?php bloginfo('url'); ?>/images/flags/flag_<?php wp_ozh_getCountryCode(1,$comment->comment_author_IP); ?>.gif" title="<?php wp_ozh_getCountryName(1,$comment->comment_author_IP);?>" style="vertical-align: middle; border: none;" />

  5. S G says:

    Your code does not work, there is some error message. i have something similar which does not give an error message, but simply doesnt display the flags…

    comment_author_IP) . \”.gif\”>”;
    ?>

  6. cosmix says:

    I am sorry to hear that. The code was copied and pasted from my source, so it does work. Perhaps there’s something wrong with your installation, file permissions, a syntax error or what-have-you.

    In the snippet you provide, remove the first \” you’ve got there, assuming it’s all part of an echo statement.

  7. S G says:

    hmm I just thought of something. What version of wordpress are you using? Can it be that my 2.0 has some bug?

  8. cosmix says:

    The latest. I don’t think so.

  9. Foehammer says:

    A shame you can’t make that SQL available. I have a visitor from what appears to be Ireland who’s flag is never appearing correctly and that’s how I ended up here.

    That aside though, I’m curious as to how you managed to get the alt/title to work for each individual flag. I’d love to get that working instead of just a generic “country flag” on mouseover. If you could share that bit of code that would be fantastic.

    Thanks.

  10. cosmix says:

    Hey Foehammer, thanks for your message.

    First of all, the SQL data from March 2006 would have been outdated by now. Why not get the script, which is still there and run it? It will query the RIPE whois server and produce a series of UPDATE SQL queries for you to run on your database to update the ip2nation data.

    The recent ip2nation update (2007-March-14) is much improved anyway, although the script did update about 30 netblocks (down from several hundreds). The errors you might be getting may have to do with the fact that ip2nation is often very inaccurate and that does not necessarily have to do with the problem described above. On this site I have been using ip2nation in conjunction with a number of other scripts I wrote that post-process the ip2nation provided tables, and even then there are cases where the country is not valid. My suggestion to you is to give the script a try, after updating your database to the latest ip2nation release.

    Re: alt/title. I believe the answer to this question is in the comments above. :)

  11. Foehammer says:

    Re: alt/title. I believe the answer to this question is in the comments above. :)

    *slaps forehead*

    So it was. I just didn’t look at the code carefully enough before. I implemented it in place of my own code with not too much hassle and it’s working perfectly. Thank you!

    As to the SQL and Ip2Nation stuff, I’m just too lazy to go much more than update the SQL, which I already had, and ironically it wasn’t until I did that, that I saw the first flags not rendering. :/ That’s when I started seeking a solution and ended up Googling my way here to your site.

    I might run your script sometime, but as I have just recently removed some perl script software from my Windows system because it was causing me some sort of registry problems, I’m not too hot on the idea of messing with scripts for awhile. I’ve got too much on my plate as a writing/warring blogger as it is, and I’m not the savvy techie type.

    But, I will find a way to improve things eventually because my perfectionist nature will overcome my laziness eventually. Hehe.

    Thanks again. I’m very happy having country names over the flags — it’s the little things in life that matter!

  12. Foehammer says:

    Oh, and get this — now that I installed your code, I noted that the hover-over on the broken flags was giving me the correct (or close to) nation name! This obviously sent me to thinking and I realized that the problem may not be the SQL from Ip2Nation entirely — that my flags folder might needs some tweaking. Presto. Added flag_gb.gif for the Great Britain country code that was no represented in the abundant flag files (already had flag_uk.gif) and this variant on a flag theme problem was resolved.

    So, again, thanks to this page, my problems were resolved and I found an additional work-around for the future!

  13. cosmix says:

    You’re welcome. I’m glad that you found the page useful despite not using the script.

  14. Charlie says:

    I just want to say thanks for the script its very useful. When I got the latest IP2Nation DB (Dec 07) it had over 2000 “eu” entries. After using the script and applying the generated SQL theres only 54. Which his going to improve the accuracy a lot.

    One thing you didn’t mention was it relies on the whois command which wasn’t installed be default on my Ubuntu server. Still one command to apt-get fixed that.

{Ping,Track}backs

Have your say.

Write in the language of the post. Comments are meant to encourage on-topic discussion. For general comments, observations, complaints (e.g. about the site), you can use the form found in the Contact page. Make sure you've read the Terms of Use before commenting.

Comments Feed for this post Comments Feed for this entry.

Download Spinalonga's Podsafe rock music for your podcast. From Athens, Greece, with love.'