GeoIP - Redirecting user to a specific page based on their country

A place to make for-pay "CMS made simple" job offerings
asfahaan
Forum Members
Forum Members
Posts: 43
Joined: Wed Aug 15, 2007 11:28 am

GeoIP - Redirecting user to a specific page based on their country

Post by asfahaan »

Hi,

I am working on a website which has multiple regions, e.g. Australia, New Zealand, Ireland,etc. The website is built in CMSMS and is currently hosted on a windows server.

Currently user is taken to a global region and then user needs to change the region on the header.
In future we want to detect what country the user is coming from and redirect them automatically to their relevant region. for example, someone visiting website from Ireland will be taken to Ireland homepage.

Looking for a working solution with CMSMS in 1weeks  time.

Cheers,
Asfahaan.

For any questions, contact me via PM.
Asfahaan Mirza
Auckland, New Zealand
jmcgin51
Power Poster
Power Poster
Posts: 1899
Joined: Mon Jun 12, 2006 9:02 pm

Re: GeoIP - Redirecting user to a specific page based on their country

Post by jmcgin51 »

you can certainly hire someone if you want, but this should be very easy to do with CustomContent and a known set of IP ranges for various countries (i.e. http://www.find-ip-address.org/ip-country/)
jmcgin51
Power Poster
Power Poster
Posts: 1899
Joined: Mon Jun 12, 2006 9:02 pm

Re: GeoIP - Redirecting user to a specific page based on their country

Post by jmcgin51 »

asfahaan wrote: In future we want to detect what country the user is coming from and redirect them automatically to their relevant region. for example, someone visiting website from Ireland will be taken to Ireland homepage.
Should the user be able to see pages belonging to other regions, or is he restricted to only pages belonging to his own region?
asfahaan
Forum Members
Forum Members
Posts: 43
Joined: Wed Aug 15, 2007 11:28 am

Re: GeoIP - Redirecting user to a specific page based on their country

Post by asfahaan »

Well the visitor by default will be redirect to a particular region (page) based on their ip. He is allowed to view pages of other regions by changing the region via region selector dropdown menu. This is already built.

The scope of this work is only redirecting the visitor to their region based on ip,etc.
Asfahaan Mirza
Auckland, New Zealand
asfahaan
Forum Members
Forum Members
Posts: 43
Joined: Wed Aug 15, 2007 11:28 am

Re: GeoIP - Redirecting user to a specific page based on their country

Post by asfahaan »

jmcgin51 wrote: you can certainly hire someone if you want, but this should be very easy to do with CustomContent and a known set of IP ranges for various countries (i.e. http://www.find-ip-address.org/ip-country/)
Well if its a easy implementation, I dont mind rewarding the person who exposes me to this functionality of CMSMS. Can you help?
Asfahaan Mirza
Auckland, New Zealand
jmcgin51
Power Poster
Power Poster
Posts: 1899
Joined: Mon Jun 12, 2006 9:02 pm

Re: GeoIP - Redirecting user to a specific page based on their country

Post by jmcgin51 »

is this for logged-in frontend users?  if yes, then something like

{if $ccuser->ipmatches(ip_address_range_goes_here)}
{redirect_page page='whatever'}
{elseif $ccuser->ipmatches(ip_address_range_goes_here)
{redirect_page page='whatever'}
{elseif $ccuser->ipmatches(ip_address_range_goes_here)
{redirect_page page='whatever'}
etc
{/if}

If it's not for logged-in FEUs, I'd have to think a little more.  Would likely involve a UDT with similar logic to the above pseudo-code.

Then you need to decide if you're going to remember the last region the visitor viewed, or if you always redirect to the visitor's "real" region.

And then you have to figure out how to only do this redirect the FIRST time a visitor views the page (during a session?  a day?? a week??)

some other things than need to be added to your spec for this project before work could begin..
asfahaan
Forum Members
Forum Members
Posts: 43
Joined: Wed Aug 15, 2007 11:28 am

Re: GeoIP - Redirecting user to a specific page based on their country

Post by asfahaan »

Hi.

Thanks for your help. Well I am more interested in visitors who are not logged in.

On the site i am using FEU and Self Reg modules.

Can you please help me with the implementation for general visitors? I will PM you the url.

Cheers,
Asfahaan
Asfahaan Mirza
Auckland, New Zealand
applejack
Power Poster
Power Poster
Posts: 1014
Joined: Fri Mar 30, 2007 2:28 am

Re: GeoIP - Redirecting user to a specific page based on their country

Post by applejack »

Use a UDT to get the users ip address then compare it to the ip range then pass back the country value i.e. uk and auto direct based upon page alias i.e. home-{$page_alias} or just feed different content.

Website Design & Production
http://www.applejack.co.uk
asfahaan
Forum Members
Forum Members
Posts: 43
Joined: Wed Aug 15, 2007 11:28 am

Re: GeoIP - Redirecting user to a specific page based on their country

Post by asfahaan »

Hmm okay. Can someone do this for me? If I give you guys access to the development site & server? I dont mind paying for your valuable time!
Asfahaan Mirza
Auckland, New Zealand
spcherub
Power Poster
Power Poster
Posts: 402
Joined: Fri Jun 06, 2008 5:54 pm

Re: GeoIP - Redirecting user to a specific page based on their country

Post by spcherub »

I was doing some research into how this can be done and I came across GeoPlugin which makes it easy to retrieve the name of the country in a UDT. This means you will not have to maintain a local ip-country database lookup.

Before I can speculate how this can be used in your scenario it will useful to know how each of your country-specific sections are organized on your site. Are they different top level pages for each supported country? Or are they specially labeled content blocks within each page or are they child pages for each main page? How are they organized today?

You can PM me the access information to your Dev server and I'll be happy to take a look.

Sanjay
spcherub
Power Poster
Power Poster
Posts: 402
Joined: Fri Jun 06, 2008 5:54 pm

Re: GeoIP - Redirecting user to a specific page based on their country

Post by spcherub »

Ok here is something that seems to work for me, verified by some quick testing.

Create a UDT call "get_country" with the following code:

Code: Select all

global $gCms;
$smarty = &$gCms->GetSmarty();
$myarray = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.getenv("REMOTE_ADDR")));
$smarty->assign('country_code', $myarray['geoplugin_countryCode']);
The in your home (global) page content (or template) insert the following smarty code:

Code: Select all

{get_country} 

{if $country_code == 'IN'}
	{redirect_page page='ind'}
{elseif $country_code == 'AU' }
	{redirect_page page='aus'}
{elseif $country_code == 'NZ' }
	{redirect_page page='ire'}
{elseif $country_code == 'IR' }
	{redirect_page page='nz'}
{else}
	{* Nothing to do *}
{/if}
This should work for you. The only unknown part is the actual values for country_code for the listed countries. You will need to setup a test page and have visitors from those supported countries tell you what country code they see, then plug those values in. I was not able to find a list of all country code returned by geoPlugin.

This list http://www.theodora.com/country_digraphs.html may represent the 2-character country codes, so you can try that as well. I was not able to test it out on your dev site as per your request, so you can try it out and let me know what you find. Note: This will not work if you are testing on a local site (like on your PC) as your IP address will be listed as 127.0.0.1 or something which cannot be converted into a country code.

Let me know what you find.

Sanjay
cowtan
Forum Members
Forum Members
Posts: 70
Joined: Mon Jan 19, 2009 5:04 pm

Re: GeoIP - Redirecting user to a specific page based on the

Post by cowtan »

Hi Sanjay - Just thought I should say that I am working on a site at the moment where I needed the same functionality and your code works well for me. Many thanks.

Gordon
mr.bacan

Re: GeoIP - Redirecting user to a specific page based on the

Post by mr.bacan »

Hello everyone,

I used spcherub UDT and works great, but not exactly for what I need. I'm using FormBuilder 0.7. I basically need the email I get to have the sender's Country Name. Right now I get the IP because it's a built-in function on FB.

Is it possible to convert that IP, from FB, into a Country Name and also send it on the email?

Thanks a lot for any help on this.
spcherub
Power Poster
Power Poster
Posts: 402
Joined: Fri Jun 06, 2008 5:54 pm

Re: GeoIP - Redirecting user to a specific page based on the

Post by spcherub »

If you can call the UDT within the Form email template, you should be able to use the country code in the smarty variable as per the original post.

I have not done this myself, so am not sure if UDTs can be processed within FormBuilder templates. Does not hurt to try though.

-S
spcherub
Power Poster
Power Poster
Posts: 402
Joined: Fri Jun 06, 2008 5:54 pm

Re: GeoIP - Redirecting user to a specific page based on the

Post by spcherub »

*** Caveat: I've not actually tested this myself ***

So if your current email template looks like this...

Code: Select all

<h1>Someone's Testing a FormBuilder Submission!</h1>
<strong>Form Name</strong>: {$sub_form_name}<br />
<strong>Date of Submission</strong>: {$sub_date}<br />
<strong>Your server</strong>: {$sub_host}<br />
<strong>IP address of person using form</strong>: {$sub_source_ip}<br />
<strong>URL of page containing form</strong>: {$sub_url}<br />

<hr />
Then alter is to look something like this:

Code: Select all

{get_country}
<h1>Someone's Testing a FormBuilder Submission!</h1>
<strong>Form Name</strong>: {$sub_form_name}<br />
<strong>Date of Submission</strong>: {$sub_date}<br />
<strong>Your server</strong>: {$sub_host}<br />
<strong>IP address of person using form</strong>: {$sub_source_ip}<br />
<strong>URL of page containing form</strong>: {$sub_url}<br />

<strong>Country of user</strong>: {$country_code}<br />

<hr />
This will not work if the UDT cannot be processed inside the template.

-S
Locked

Return to “Help Wanted (commercial)”