Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

IP address to Country conversion - working plugin published.

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 423

PostPosted: Thu Feb 12, 2009 5:31 pm    Post subject: IP address to Country conversion - working plugin published. Reply with quote

Location specific applications or reports need yo know: from where the visitors are from. I have tried to show a country of the visitor or any specific IP based on the data from standard websites. In the following plugin, I leech the data from Interet Frog (http://www.internetfrog.com/). There are many websites offering real time IP to country conversion.

If we can take benefits of these services, it is still a good idea NOT to store a big bunch of IP to country database in our own server. I discussed a little about this at http://www.phpinsider.com/smarty-forum/viewtopic.php?t=14816&highlight=country and inspired by Mark Mitchenall's feedback. So, a leecher is now borned.

Here, we say the IP to country providing websites about our desired IP, by actually filling their online website form. And, we pick up the results to display the converted field. For examply, the country name of an IP.

This plugin is extensible. You can use it to read conversion from more/other websites. Also, you can extract other fields, like city or contact addresses if possible.

The core trick is to use curl and make a query onto the provider's website, and parse the results to find our field values. For later, I have used regular expressions.

Here are more websites that you can crawl for IP to country data conversion:
http://www.internetfrog.com
http://www.maxmind.com
http://www.dnsstuff.com

Use this plugin much responsibly and DO NOT SEND A LOT OF REQUESTS TO THESE PROVIDERS. These servers are interested to serve the people/public, than to such automated scripts.

Here is the go: put the following plugin file (modifier.ip2c.php) into your Smarty's plugin directory. Thats all. Here is how you may use the plugin in your template files:

Code:

www.smarty.net: {'206.131.206.81'|ip2c:'frog':'country'}<br />
bimal.org.np: {'66.40.52.75'|ip2c:'frog':'country'}<br />
Your IP (visitor): {$smarty.server.REMOTE_ADDR|ip2c}


The plugin file (modifier.ip2c.php):
Code:

<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */


/**
 * Smarty IP to country converter plugin
 * This code reads the IP information from third party websites, and thus no need to store database in your own.
 * Example urls who serve such facilities are:
 * @url http://www.internetfrog.com/
 * @url http://www.dnsstuff.com/
 *
 * Type:     modifier<br>
 * Name:     upper<br>
 * Purpose:  convert string to uppercase
 * @link http://bimal.org.np
 * @link http://smarty.php.net/manual/en/language.modifier.ip2c.php
 *          ip2c (Smarty online manual)
 * @author   Bimal Poudel <smarty at bimal dot org dot com dot np>
 * @param string IP value to convert
 * @param string Resource to handle IP and format the IP
 * @param string Field name related the information IP collected from the selected resource
 * @return string Field value, for example: city, country
 */
function smarty_modifier_ip2c($ip='0.0.0.0', $ip_resource='frog', $field_name='country')
{
   $ip_converted = '';
   switch($ip_resource)
   {
   case 'frog':
   case 'seocentro':
      $data = array();
      $post_data = array();
      $data['IP'] = $ip;
      foreach($data as $k => $v)
      {
         $v = urlencode($v);
         $post_data[] = "$k=$v";
      }
         
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_HEADER, 0);
      curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_URL, "http://www.seocentro.com/tools/online/ip-country.html");
      curl_setopt($ch, CURLOPT_POSTFIELDS, implode('&', $post_data));
      $results_html = curl_exec($ch);
      curl_close($ch);

      # Many data are possible about an IP. Determine just what you need.
      # Here, write your own code to read particular information about the IP in the result.
      # Rules to extract various parts of the collected information
      switch($field_name)
      {
      case 'country':
         # Original HTMl code seen in the result html:
         # <tr><td align="right">&nbsp;country name:&nbsp;</td><td align="left">&nbsp;Argentina&nbsp;</td></tr>
         $patterns='/country name\:\&nbsp;\<\/td\>\<td align\=\"left\"\>\&nbsp;(.*?)\&nbsp;\<\/td\>\<\/tr\>/is';
         $data = array();
         preg_match_all($patterns, $results_html, $data, PREG_SET_ORDER);
         $ip_converted = $data[0][1];
      
      # Add more ... city, latitude, longitude
      }
      break;
   default:
      $ip_converted = 'IP conversion resource not defined';
   }

   $ip_converted = ($ip_converted)?$ip_converted:"No {$field_name} information about this IP {$ip}";

   return($ip_converted);
}
?>



Things to remember:
* I am sorry for the Online manual link. It does not exist now, and neither I have hosted it on my website.
* The result is dependent on the HTML results of the provider's pages, and so, they are likely to change over time. This will cause the plugin stop working.
* However, you can modify it again with the new HTML parsing.
* Read out yourself the source code of the plugin and modify yourself for the new changes. I am not describing how to exend it more.
Back to top
View user's profile Send private message Visit poster's website
echoDreamz
Smarty Rookie


Joined: 06 Jun 2008
Posts: 10

PostPosted: Fri Feb 20, 2009 10:47 am    Post subject: Reply with quote

Nice plugin, but now your website and PHP parser are dependent on waiting for the request to complete for obtaining the information from the remote host. I believe the best option is to stick with using the database or CSV file for lookups.
Back to top
View user's profile Send private message
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 423

PostPosted: Fri Feb 20, 2009 8:11 pm    Post subject: We discussed not to use database. Reply with quote

We discussed not to use our own database for some reasons:

* IPs keep on changing. So, our database becomes stale.
* IP to country database is a bulky, but rarely used.

So, owning a bulk amount of data (which might not be fresh) seems less important, unless we are producing a serious tool based on IPs. So, even if I had worked with own IP-to-Country database, I decided to use third party providers.

Even though this seems a light weight and copy/paste plugin, it can have some defects:

* Some hosting companies do not allow outgoing ports, and we can not use this plugin.
* The third party providers may not like us to *steal* information from their websites.
* Those providers may change their HTML output, causing our plugin unable to parse the information.

What if remote server does not respond?
We should not hang around waiting for the response.
Rather, we must reduce the connection time to the server, that anyone can do with curl.

But isn't it better to refer to those providers who always have the fresh information about the IPs?
Back to top
View user's profile Send private message Visit poster's website
echoDreamz
Smarty Rookie


Joined: 06 Jun 2008
Posts: 10

PostPosted: Sat Feb 21, 2009 12:29 am    Post subject: Reply with quote

Personally for me, updating a database every month or so isnt all that bad. But creating my site to rely on a outside 3rd party website that could block my sites IP etc and end up being pointless anyways is just out of the question.

Now if the site was designed for this use, and offered redundancy etc and didnt mind you connecting to there services etc then it would not be that big of a deal.
Back to top
View user's profile Send private message
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 423

PostPosted: Sat Feb 21, 2009 7:07 am    Post subject: Holding your database is good Reply with quote

If you have enough resources to hold database and do not mind updating it frequently, it is good. Your application becomes standalone.

I have described this scene in: http://www.phpinsider.com/smarty-forum/viewtopic.php?t=14816
Back to top
View user's profile Send private message Visit poster's website
echoDreamz
Smarty Rookie


Joined: 06 Jun 2008
Posts: 10

PostPosted: Sat Feb 21, 2009 8:09 am    Post subject: Reply with quote

Yeah, we have a internally used smarty plugin for maxminds GEOip.

Also maxminds database isnt too large ~12 MB i believe. There are some databases though that are hugh +400MB.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Sat Feb 21, 2009 3:39 pm    Post subject: Reply with quote

Just a note, this ip->country conversion stuff should be left to business logic (PHP). Just use Smarty for presentation.
Back to top
View user's profile Send private message Visit poster's website
vova
Smarty n00b


Joined: 14 Dec 2010
Posts: 2

PostPosted: Tue Dec 14, 2010 6:43 pm    Post subject: Reply with quote

Hi bimal,

Could you pls let me know what variable if any can I use to show ip address using this plug in (the country feature works, just wondering if I can show ip as well)

Thank you in advance.
Back to top
View user's profile Send private message
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 423

PostPosted: Wed Dec 15, 2010 5:45 am    Post subject: IP 2 Country Reply with quote

This is to show IP to Country Name.

In your template file, to show an IP use:

Code:
{$smarty.server.REMOTE_ADDR}


To show the Country Name that had this IP, you should use this plugin as:
{$smarty.server.REMOTE_ADDR|ip2c}

It will map to PHP's $_SERVER['REMOTE_ADDR'] for the IP address.
Back to top
View user's profile Send private message Visit poster's website
vova
Smarty n00b


Joined: 14 Dec 2010
Posts: 2

PostPosted: Wed Dec 15, 2010 1:25 pm    Post subject: Reply with quote

Thank you very much.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP