In: , , ,
On: 2004 / 08 / 27
Shorter URL for this post: http://ozh.in/1t

There it is, my very first real plugin for WordPress ("very first" meaning of course you can expect two or three more within the next 200 years). This plugin guesses your visitor's Country from his IP (check these examples). Based upon the very good ip2nation free data, I've simply packaged it as a plugin.

Set up the MySQL table

First of all, go to ip2nation and download the .sql file (I'm not mirroring it because it is updated from times to times, so get the latest one) This .sql file will create two tables, ip2nation and ip2nationCountries, containing top secret data you don't want explanations about :)

Newbie tips : first, install PHPMyAdmin on your website. This is a great interface for anything you can do with MySQL databases. Once it's done, look for the "SQL" link in the menu : you will be prompted for a .sql file, for example the one you just downloaded on your computer. It will set up and populate the required tables.

Download the plugin

Download the plugin :
ozhs-ip-to-nation.zip
Extract and upload to your blog, preserving directory structure if any.
Note: download counter here and stats on wordpress.org may differ and reflect the number of downloads before this plugin was hosted on the plugin directory

Use the plugin

I've created two "template tags" you can use in your pages :

  • wp_ozh_getCountryName() : prints the country name (France, Belgium, Vanuatu, stuff like this).
  • wp_ozh_getCountryCode() : prints the country "code" : 2 letters, as in top level domains (fr, be, it, stuff like this)

These two functions have to optional arguments :

  • $display : defaults to 0 (zero), if you want to print the result or just return it (see examples below)
  • $ip : defaults to $_SERVER['REMOTE_ADDR'], which is the visitor's IP address.

(Note : If you make a call to just one or to both template tags, it will cost you only 1 SQL query.)

(Note 2 : I named the plugin file and the function with a beginning "wp_ozh_" to prevent any duplicate function name, if someone ever creates another similar plugin)

Examples of use

For example, you could use the plugin with the following code :

  1. <?php
  2. echo "You are probably from ". wp_ozh_getCountryName() ."<br />";
  3. echo "If so, your country flag is  <img alt=\"your flag\"    
  4.   src=\"/images/flags/flag_"
  5.   . wp_ozh_getCountryCode() . ".gif\"><br />";
  6. ?>

The template wp_ozh_getCountryCode() goes well with this awesome tiny flags pack you've seen everywhere on the web (which were originally created by a guy I know, Zarkof, and are free to use, which is a nice gift considering the amount of pixel skills and time he must have put in these 175 flags :)

Passing the parameter "0" (zero) to a function could be useful for example to test a language redirection without printing anything. The following example determines wether the user is supposedly speaking French or not :

  1. <?php
  2. $country = wp_ozh_getCountryName(0) ;
  3. switch ($country) {
  4.    case "France" :
  5.    case "Guadeloupe" :
  6.    case "Luxembourg" :
  7.    case "Monaco" :
  8.    case "Martinique" :
  9.    case "New Caledonia" :
  10.    case "French Polynesia" :
  11.    case "St. Pierre and Miquelon" :
  12.    case "Reunion" :
  13.    case "French Southern Territories" :
  14.    case "Wallis and Futuna Islands" :
  15.         echo "you speak French";
  16.         break;
  17.    default :
  18.         echo "you may not speak French";
  19. }
  20. ?>

You can also pass an IP address to the functions, which could be used to print your commenters' country : in blogroot/wp-comments.php (WP 1.2) or blogroot/wp-content/themes/yourtheme/comments.php (WP 1.5+), where you see <?php comment_author_link() ?>, put the following :

  1. <?php comment_author_link() ?> from
  2.  <?php wp_ozh_getCountryName(1,$comment->;comment_author_IP) ?>

This will output something like : JohnDoe from India

To put a tiny flag by the name (like in comments here), you could use the following code :

  1. <?php
  2. echo '<img alt="your flag" src="/images/flags/flag_'
  3.      . wp_ozh_getCountryCode(0,$comment->comment_author_IP)
  4.      . '.gif">' ;
  5. ?>

Disclaimer

I am not positive that the ip2nation data produce a 100% correct result. You may happen to get generic or incorrect results, such as "Europe" or "US Educationnal" instead of a country. You might tell the author of the data, I'm just a packager :)

Sandbox, testing and debugging help

Please, don't post a comment here just to check what flag will come up for you. To do so, please have a look at the examples and demo page, as stated in the very first line of this article. Test comments will be removed and their author won't start with a good karma if they ask for my support afterwards.

Please, be accurate and give details when you're asking for support. "The plugin doesn't work, can you help ?" is completely useless yet very frequent. State the PHP code you use and the error or unexpected output it generates.

Please, don't post multiple lines of code here when you're asking for support or debugging help. Upload your code somewhere I can check it, or use this site to paste your code and link it here.

And please, please, please … read the instructions and examples here before asking something that is already answered and explained here.

Shorter URL

Want to share or tweet this post? Please use this short URL: http://ozh.in/1t

Metastuff

This entry "IP to Nation WordPress Plugin" was posted on 27/08/2004 at 8:22 pm and is tagged with , , ,
Watch this discussion : Comments RSS 2.0.

377 Blablas

  1. sven says:

    Hi Ozh,

    I have a problem with this great plugin and I hope you can help me.

    I used this code withing the comments.php:

    <img alt="comment_author_IP) ?>" src="http://www.donguriyama.net/blog/px/flags/flag_comment_author_IP) ?>.gif">

    The problem is, that every comment I (or anyone else) write gets a German flag, although I'm located in Japan. My webspace host is in Germany though.
    Do I have to modify something with the code I used, or does my host has a problem with this?

  2. sven says:

    Argh, sorry again.
    Please just look at it here: http://blog.donguriyama.net/code.txt

  3. Ozh says:

    sven » looks like $comment (and therefore $comment->comment_author_IP) is empty (what happens if you just "echo $comment->comment_author_IP" ?). Are the commenters IP correctly recorded in your database? (table wp_comment, there should be a column containing IPs). Did you enclose the flag displaying in a custom function and you forgot to issue a "global $comment" inside it?
    Cannot be more helpful than this, sorry…

  4. sven says:

    Hi Ozh!

    Thanks for your reply!
    I checked the wp_comment table and I figured that your IP is the same with mine (as well as with some spam I got earlier). That means, for some strange reason, the IP is just recorded wrong… It's always the same…
    I guess it can only have something to do with my host then, right?

  5. Ozh says:

    sven » could be a server side problem, indeed. There must be some kind of local proxy filtering connections, or something like this. This is very problematic, as it can interfere with a lot of other things (spam detection for example). You should ask your server admins to check this.

  6. sven says:

    Ya, I thought something like that, too. But I already talked to my webhost's support team and they uploaded a small script to my webspace: http://donguriyama.net/testip.php

    As you can see, it shows the correct IP there. That means no server problem… The problem must be somewhere within wordpress itself or the plugin. :(

    Do you think it could have something to do with my current theme? I remember that ip2nation worked correct on my blog a few months ago, but that was under an another theme… Although, no it seems to not work with any theme I try… :(
    Kind of have no clue here…

  7. Ozh says:

    sven » the plugin doesn't store any IP. It just reads the IP that has been stored previously. The problem must be coming from another plugin, if it used to work. I don't think it can be theme related. The question you have to ask yourself is: what plugin can interfere with WP at the time of *posting* a comment?

  8. sven says:

    Even I tried to deactivate all plugins (except ip2nation of course), it still has the same error…
    May there be anything else than plugins that could possibly interfere with posting comments?

  9. Ozh says:

    sven » sent you an email

  10. Riri Audiya says:

    is it possible to show the cities names?

  11. Ozh says:

    Riri Audiya » No.

  12. nice one says:

    very cool plugin doest work with latest version of wordpress?

  13. Mia says:

    Ozh, you are great =D

    I think I can use all your plugins, because it is soo damn good!

    I am going to put this flag things in my blog, thank you!!

    =D

  14. walter says:

    I have this error, could tell why,

    Parse error: syntax error, unexpected ';', expecting T_STRING or T_VARIABLE or '{' or '$' in /home/cdgen0r/public_html/pruebadepaternidad/wp-admin/moderation.php on line 167

    Thank you very much

  15. Ozh says:

    walter » probably not related to my plugin.

  16. J.S says:

    is it work really?

  17. mory says:

    can you make this only visible in wordpress admin comment manager?

  18. Ozh says:

    mory » Interesting suggestion. I'll add this feature to a next release.

  19. Seyyed says:

    hello,

    I've imported the SQL file, and uploaded the folder to my plugin folder, but the plugin does not appear in my plugin list in admin panel!!

    I'm using WP 2.5

    what's the reason?

  20. Faiz says:

    Yeap…having the same problem here. WordPress 2.5 doesn't show up in the plugin list.

  21. Ozh says:

    Faiz & Seyyed » OOPS! My bad! Re download the plugin, or edit it and on the very first line replace "Hack Name" with "Script Name". Really I don't know how such a typo got there :)

  22. Faiz says:

    Hi Ozh, its working now. Thanks! :-)

    As for Seyyed, have you checked the path for the flags? the default path is "/images/flags"

  23. Anggie says:

    Is it same with Priyadi's Plugin?

    And I see in Priyadi's blog, there're show commenter's cities name, do you know how to activate the cities name?

  24. Ozh says:

    Anggie » Priyadi's plugin was made after this one with another geo database which is a lot bigger (and which, apparently, tries to resolve IP to country *and* city, although I guess it's probably approximative). No city with my plugin.

  25. hi. great and easier plugin plus a very accessible maker of the plugin. could you modify this with city included? it will blast priyadi's,lol..

  26. prony says:

    Resolved: I just deleted the cache file for ip to nation…

    I had installed the useronline user first, then your ip2nation, and only then did I RTFM ("read the fucking manual"…) and installed the sql.

    It works marvels, and I love it.

  27. […] ????????? ? ?????, ?????????? ??? ?????????? ??????? ??????? ip2nation ??? ???? ??, ?? ?? ???????? ????? ??????? […]

  28. Hi,
    I got some problem with this plugin. It worked very nice until yesterday. Since then I get some fwrite() and fclose() error for every comment. Look here
    How can I solve this problem?

  29. Ozh says:

    Sven Schellberg » if it worked fined till yesterday, ask yourself what you've changed then. Looks like the wp-content/cache directory is missing maybe.

  30. ok, its done… I have to delete the ip2nation folder in wp-content/cache but I don't know why
    Now it works again

  31. Shoshana says:

    I'm CSS challenged. I've activated the plugin, but can't seem to make it work. Do I have to add some code to my template?

  32. Ozh says:

    Shoshana » Please read the doc here.

  33. Greg says:

    Ozh
    I'm getting the following errors when I try to activate the plugin

    Warning: mkdir() [function.mkdir]: Permission denied in mysite/wp-content/plugins/ozhs-ip-to-nation/wp_ozh_ip2nation.php on line 27

    and

    Warning: mkdir() [function.mkdir]: No such file or directory in mysite/wp-content/plugins/ozhs-ip-to-nation/wp_ozh_ip2nation.php on line 28

    any ideas?

    Thanks
    Greg

  34. Ozh says:

    Greg » make sure there's a wp-content/cache/ directory. I will improve some checks when I upgrade this plugin

  35. Greg says:

    Ozh
    Ok I created the cache directory, chmoded it to 755 and that took care of one message.
    Now I\'m getting

    Warning: mkdir() [function.mkdir]: Permission denied in mysite/wp-content/plugins/ozhs-ip-to-nation/wp_ozh_ip2nation.php on line 28

    and when I go into the wordpress admin interface to deactivate it, I get

    Warning: Cannot modify header information – headers already sent by (output started at mysite/wp-content/plugins/ozhs-ip-to-nation/wp_ozh_ip2nation.php:28) in mysite/wp-includes/pluggable.php on line 694

    sorry to be such a pain ; )

    Greg

  36. LeviathanC says:

    Okay, I noticed that this plugin caused the website lagged and slow when activated on version 1.2.1.1 especially try to loggin into wp-admin is the worst part when it comes to lagging. I think that it had to do with the cache or something like that. Not sure. Any idea how to fix it?

  37. Ozh says:

    LeviathanC » I don't notice any particular lag on your website. Plus, the plugin is totally inactive in the admin area, so it's not doing anything when you log in.

  38. LeviathanC says:

    Ozh,

    I figure out that one of the plugins lagged my site down and had to disabled it for awhile. And my apology for being mistake on that.

  39. Ann says:

    Hi,

    I am getting the exact same messages as Greg. I am using WP 2.5.1.

    Any ideas as to what the problem may be?

  40. Ann says:

    Since the error suggested that it wasn't being allowed to make directories, I looked in the php file to see what other directories it was trying to make.

    As already mentioned by Ozh, you need a wp-content/cache directory. You also need a wp-content/cache/ip2nation directory. That took care of the error messages that Greg mentioned that I was also getting so I assume that it is working now.

  41. Ozh says:

    Ann & Greg » I've modified the plugin, please download it again if you still get errors.

  42. Ann says:

    Ozh, I downloaded it again. I am getting the following error when I try to use either your example for determining whether someone speaks French and when I use the Who-Sees-Ads plugin using your US readers only example. I got this error with the old version too:

    1. Warning: fwrite(): supplied argument is not a valid stream resource in /f1/content/example/public/blog/wp-content/plugins/ozhs-ip-to-nation/wp_ozh_ip2nation.php on line 60
    2.  
    3. Warning: fclose(): supplied argument is not a valid stream resource in /f1/content/example/public/blog/wp-content/plugins/ozhs-ip-to-nation/wp_ozh_ip2nation.php on line 61

    Any ideas? I'd love to get this working as I'm trying to get the geotargeting of ads working.

  43. Get It From Boy! says:

    hi,

    just to let you know, this plugin has crashed my blog in 2 webhosts! first, my local webhost kicked me out because of server load caused i'm pretty sure by this plugin because it is the only plugin i installed before the crash and when i removed this everything went back to normal..it caused a heavy database load when traffic increases..but i really loved this plugin and what it can do, so when i transferred to a world famous web host i thought it can handle the load, wrong assumption, this dream host also crashed and informed me that they will kick me out if the heavy server load will not be solved, and the only plugin i installed is this one. they disabled all my plugins except the anti-spam ones. and it solved the database crash..i activated all plugins one by one thereafter, except this ip to nation and everything are back to normal.

    i really want to use this amazing feature, esp. since i have a large international traffic, i hope you update or fix this heavy database/server load use of this plugin..

  44. Ozh says:

    Get It From Boy! » There's nothing to fix except your host.
    Each IP that's stored with a comment needs an SQL query the first time, and then it's cached on disk so there's no additional query. I'm using it here, several posts have 300+ comments, and I never had any problem with it.

  45. hi ozh,

    that's strange, we have the SAME host..i noticed you have dreamhost invites..so i figured, you're hosted there too..

  46. Ozh says:

    Get It From Boy! » Yes, I am hosted on DH

  47. Nina says:

    There are some places that when I post from, neither the flag or the country name appears, and there are other places that works fine. Do you have a clue of what can cause this?

    Thanks in advance

  48. Tom Riddle says:

    Sorry but your database is wrong !!

    I'm from Viet Nam but the flag is Australia

    So how can I update the database ?

  49. Ann says:

    Ozh, any ideas on the error message I posted?

  50. Ozh says:

    Ann » Sorry, I don't know… At least you could prefix the lines with "@" (ie "@fwrite" and "@fclose") to remove the warnings and make things silent. You sure there's a cache folder specified and that it's writeable ? (see Codex)

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
Gravatars: Curious about the little images next to each commenter's name ? Go to Gravatar and sign for a free account
Spam: Various spam plugins may be activated. I'll put pins in a Voodoo doll if you spam me.

Read more ?