Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

IP address to Country conversion

 
Post new topic   Reply to topic    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 423

PostPosted: Mon Jan 26, 2009 4:56 pm    Post subject: IP address to Country conversion Reply with quote

Links like http://ip-to-country.webhosting.info/node/view/6 train on how to import ip to country database into mysql. Their discussion forum is more than

this.

Here is my prototype for displaying a visitor's country name.

* Install the database of IPs and countries.
* Use a plugin to convert $_SERVER['REMOTE_ADDR'] into SQL drawn country name.
* Show/use the country name, as required.

We need a modifier plugin for this, that looks like:

# modifier.ip2c.php

Code:
<?php

function smarty_modifier_ip2c($ip='0.0.0.0')
{
   if(!$ip)
      $ip = $_SERVER['REMOTE_ADDR'];

   # Make a database connection or use an existing global database object.
   # Write some SQLs and PHP code to convert this IP into database.
   # You can learn more details in http://ip-to-country.webhosting.info/node/view/6

   $country = # Read and set the country name.

   if(!$country)
      $country='Unknown';

   return($country);
}
   
?>


Then in your template files, the access or IP to country conversion will be like:

{$smarty.server.REMOTE_ADDR|ip2c}
or,
{'206.131.206.81'|ip2c}
or,
{$ip_variable|ip2c}

You can use this modifier to:

* Choose a default country in a drop down box of country.
* Displaying you are from ... county, and its flag.
* Displaying In ... country ..., our services are in the cities ...
* Many more...

Please feedback me.

Here, I am lazy to publish the original source code, because I once did it in a project, but now, do not have the access to these files. For well functioning codes, I too need to rewrite them back myself again.
Back to top
View user's profile Send private message Visit poster's website
elpmis
Smarty Elite


Joined: 07 Jun 2007
Posts: 321

PostPosted: Tue Jan 27, 2009 7:45 am    Post subject: Reply with quote

Useful tag - thx!
Back to top
View user's profile Send private message
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 423

PostPosted: Tue Jan 27, 2009 3:51 pm    Post subject: Update your database every month Reply with quote

Their database (CSV export) is modified every month. Be sure to update your IP-to-Country database every month. And, please design your table NOT TO use InnoDB storage engine in mysql, to prevent problems while updating the countries for their IPs.

It is a genuine truth that IP to Country database changes so frequently, and the free version is available every month only.
Back to top
View user's profile Send private message Visit poster's website
mohrt
Administrator


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

PostPosted: Tue Jan 27, 2009 3:58 pm    Post subject: Re: Update your database every month Reply with quote

bimal wrote:
Their database (CSV export) is modified every month. Be sure to update your IP-to-Country database every month. And, please design your table NOT TO use InnoDB storage engine in mysql, to prevent problems while updating the countries for their IPs.

It is a genuine truth that IP to Country database changes so frequently, and the free version is available every month only.


Why would InnoDB cause a problem with updates?
Back to top
View user's profile Send private message Visit poster's website
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 423

PostPosted: Tue Jan 27, 2009 4:47 pm    Post subject: Foreign key problems Reply with quote

I think, I gave incomplete information. (Oh, I feel a sorry). The InnoDB table is basically used to hold foreign keys, and if your database that you might update monthly (the country ID and IPs) is if having foreign keys, it might restrict updating new IDs.

So, it is better to keep the IP-to-Country table away from your foreign keys. Otherwise, converting ip to country would result in old country names, that may not be true.

In my normal practice, I truncate this table first, and put the new entries from the csv file.
Back to top
View user's profile Send private message Visit poster's website
mitchenall
Smarty Pro


Joined: 27 Feb 2004
Posts: 107
Location: London, UK

PostPosted: Thu Jan 29, 2009 11:27 am    Post subject: Re: IP address to Country conversion Reply with quote

bimal wrote:

Please feedback me.


I can't conceive of any good reason to import this data into a database for lookup. MUCH better to just use the Apache mod or php extension. Even the native PHP code. It is truly pointless storing the entire thing in your database.
_________________
Mark Mitchenall
Back to top
View user's profile Send private message Visit poster's website
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 423

PostPosted: Thu Feb 12, 2009 5:35 pm    Post subject: ip2c - now works Reply with quote

I have placed a new working version of a light weight plugin. It works by crawling on provider's websites.
http://www.phpinsider.com/smarty-forum/viewtopic.php?p=55581#55581

This plugin may stop working, if the providers modify the output in their HTML pages. The main theme of this plugin is:

* Less frequent use
* Real time conversion
* Not storing own database
Back to top
View user's profile Send private message Visit poster's website
anurdh65
Smarty n00b


Joined: 07 May 2009
Posts: 4

PostPosted: Thu May 07, 2009 10:07 am    Post subject: Reply with quote

Hi

I think the code is incomplete, but usually i used the website ip-details.com to get the ip address, location. Could any body help me to get the details through PHP coding.
Back to top
View user's profile Send private message
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 423

PostPosted: Thu May 07, 2009 5:05 pm    Post subject: Not out of service Reply with quote

As we have to depend upon the third party services for these details, it is always likely that these kinds of scripts might out of service, so soon.

But, here ( http://www.phpinsider.com/smarty-forum/viewtopic.php?p=55759 ) you can find another kind of service.
You can even modify it yourself to extract more details.
Or, even add a different provider like, ip-details.

Thanks.
Back to top
View user's profile Send private message Visit poster's website
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 423

PostPosted: Tue Oct 20, 2009 6:32 pm    Post subject: How to import the data if you want to store it yourself? Reply with quote

I have established a different thread at Self - Help, to inform, how to maintain your own copy of the database. You can see this as well.

Just click on the link above to visit more details, or at:
http://www.help-self.com/forum/viewtopic.php?f=9&t=38

The csv file is of 4.65 MB with 96909 records, as of now.

This can help to detect frauds like, a user is signing up with a different country name, than s/he is currently in, or accessing credit card information abroad one's location. Or, to disseminate the geography specific information on your website.

Enjoy.
Back to top
View user's profile Send private message Visit poster's website
newjav
Smarty n00b


Joined: 21 Jan 2010
Posts: 3

PostPosted: Thu Jan 21, 2010 7:14 am    Post subject: Reply with quote

Thanks bimal nice plugin
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    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