In: , , ,
On: 2004 / 08 / 27 Viewed: 212795 times
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

    Pages: « 1 2 3 4 5 [6] 7 8 » Show All

  1. 251
    sven Japan »
    commented, on 12/Jan/08 at 7:37 am # :

    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. 252
    sven Japan »
    commented, on 12/Jan/08 at 7:44 am # :

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

  3. 253
    Ozh France »
    replied, on 12/Jan/08 at 10:40 am # :

    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. 254
    sven Japan »
    wrote, on 12/Jan/08 at 11:46 am # :

    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. 255
    Ozh France »
    replied, on 12/Jan/08 at 3:00 pm # :

    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. 256
    sven Japan »
    replied, on 12/Jan/08 at 4:10 pm # :

    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. 257
    Ozh France »
    said, on 12/Jan/08 at 4:22 pm # :

    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. 258
    sven Japan »
    wrote, on 12/Jan/08 at 9:02 pm # :

    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. 259
    Ozh France »
    thought, on 12/Jan/08 at 9:10 pm # :

    sven » sent you an email

  10. 260
    Riri Audiya Indonesia »
    wrote, on 07/Mar/08 at 2:51 am # :

    is it possible to show the cities names?

  11. 261
    Ozh France »
    wrote, on 07/Mar/08 at 10:22 am # :

    Riri Audiya » No.

  12. 262
    nice one Saudi Arabia »
    thought, on 09/Mar/08 at 10:59 pm # :

    very cool plugin doest work with latest version of wordpress?

  13. 263
    Mia Brazil »
    replied, on 19/Mar/08 at 2:36 pm # :

    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. 264
    walter Mexico »
    wrote, on 25/Mar/08 at 8:10 pm # :

    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. 265
    Ozh France »
    said, on 26/Mar/08 at 8:24 am # :

    walter » probably not related to my plugin.

  16. 266
    J.S Japan »
    said, on 31/Mar/08 at 12:21 pm # :

    is it work really?

  17. 267
    mory United States »
    wrote, on 08/Apr/08 at 8:01 am # :

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

  18. 268
    Ozh France »
    said, on 08/Apr/08 at 8:35 am # :

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

  19. 269
    Seyyed Czech Republic »
    said, on 11/Apr/08 at 7:22 pm # :

    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. 270
    Faiz Malaysia »
    replied, on 12/Apr/08 at 11:43 am # :

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

  21. 271
    Ozh France »
    said, on 12/Apr/08 at 11:56 am # :

    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. 272
    Faiz Malaysia »
    replied, on 12/Apr/08 at 7:21 pm # :

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

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

  23. 273
    Anggie Indonesia »
    said, on 19/Apr/08 at 2:16 pm # :

    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. 274
    Ozh France »
    commented, on 19/Apr/08 at 5:09 pm # :

    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. 275
    Showbiz Intriga? GetIt From Boy! Philippines »
    thought, on 14/May/08 at 8:05 pm # :

    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. 276
    prony Ireland »
    commented, on 17/May/08 at 1:02 pm # :

    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. 277
    ?????? | Insight IT Russia »
    pingback on 21/May/08 at 5:40 pm # :

    [...] ????????? ? ?????, ?????????? ??? ?????????? ??????? ??????? ip2nation ??? ???? ??, ?? ?? ???????? ????? ??????? [...]

  28. 278
    Sven Schellberg Germany »
    replied, on 28/May/08 at 9:49 am # :

    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. 279
    Ozh France »
    commented, on 28/May/08 at 10:17 am # :

    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. 280
    Sven Schellberg Germany »
    thought, on 28/May/08 at 1:14 pm # :

    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. 281
    Shoshana United States »
    said, on 29/May/08 at 9:18 pm # :

    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. 282
    Ozh France »
    replied, on 29/May/08 at 10:58 pm # :

    Shoshana » Please read the doc here.

  33. 283
    Greg United States »
    replied, on 06/Jun/08 at 1:19 am # :

    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. 284
    Ozh France »
    wrote, on 06/Jun/08 at 7:57 am # :

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

  35. 285
    Greg United States »
    said, on 06/Jun/08 at 4:25 pm # :

    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. 286
    LeviathanC United States »
    commented, on 20/Jun/08 at 7:23 am # :

    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. 287
    Ozh France »
    replied, on 20/Jun/08 at 7:57 am # :

    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. 288
    LeviathanC United States »
    said, on 20/Jun/08 at 8:28 am # :

    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. 289
    Ann Canada »
    wrote, on 23/Jun/08 at 8:08 pm # :

    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. 290
    Ann Canada »
    said, on 23/Jun/08 at 8:26 pm # :

    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. 291
    Ozh France »
    replied, on 23/Jun/08 at 9:12 pm # :

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

  42. 292
    Ann Canada »
    said, on 23/Jun/08 at 11:00 pm # :

    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. 293
    Get It From Boy! Philippines »
    replied, on 28/Jun/08 at 5:32 pm # :

    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. 294
    Ozh France »
    wrote, on 28/Jun/08 at 5:36 pm # :

    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. 295
    Get It From Boy! Philippines »
    replied, on 28/Jun/08 at 5:55 pm # :

    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. 296
    Ozh France »
    wrote, on 28/Jun/08 at 7:26 pm # :

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

  47. 297
    Nina Brazil »
    replied, on 03/Jul/08 at 3:42 am # :

    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. 298
    Tom Riddle Viet Nam »
    commented, on 04/Jul/08 at 5:46 am # :

    Sorry but your database is wrong !!

    I'm from Viet Nam but the flag is Australia

    So how can I update the database ?

  49. 299
    Ann Canada »
    replied, on 04/Jul/08 at 5:47 am # :

    Ozh, any ideas on the error message I posted?

  50. 300
    Ozh France »
    thought, on 04/Jul/08 at 7:34 am # :

    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)

Pages: « 1 2 3 4 5 [6] 7 8 » Show All

Leave a Reply

Comment Guidelines or Die

  • HTML: You can use these tags: <a href=""> <em> <i> <b> <strong> <blockquote>
  • Posting code: Post raw code (no <> &lt; etc) within appropriate tags : [php][/php], [css][/css], [html][/html], [js][/js], [sql][/sql], [xml][/xml], or generic [code][code]
  • Gravatars: Curious about the little images next to each commenter's name ? Go to Gravatar.
  • Spam: Various spam plugins on patrol. I'll put pins in a Voodoo doll if you spam me.
  • I will mark as Spam test comments, all comments with SEO names (ie "My Cool Online Shop" instead of "Joe") or containing forum-like signatures.

Read more ?