TinyPortal

Development => Resources => Topic started by: ApplianceJunk on August 26, 2009, 11:47:53 PM

Title: View block by what country user is from?
Post by: ApplianceJunk on August 26, 2009, 11:47:53 PM
I have a new appliance parts affiliate that has two different websites.

They have one site for Canada (.ca) and a .com site.

Could two TP blocks be set up so visitors to our site from Canada see one block and visitors from the USA see the second block?
Title: Re: View block by what country user is from?
Post by: JPDeni on August 26, 2009, 11:52:29 PM
Probably. I don't know how to tell where the visitors are from, though. There must be some environmental variable, but I don't know what it is.

Actually, though, it wouldn't be two blocks. It would be one block with different content, depending on where they come from.
Title: Re: View block by what country user is from?
Post by: ApplianceJunk on August 27, 2009, 12:38:13 AM
QuoteIt would be one block with different content, depending on where they come from.

What ever would work best. I was thinking how I can set up a block in say the top right corner and have it set up so only visitors see it and then a second block in the same location that only members see.

This way it's two different block in the same location, but only one is being viewed at a time depending on if you are signed in or just visiting as a guest.
Title: Re: View block by what country user is from?
Post by: ApplianceJunk on August 27, 2009, 12:39:58 AM
QuoteI don't know how to tell where the visitors are from, though

How does google analytics show me in my stats how many visitors come from Canada?

Is it done with IP address?
Title: Re: View block by what country user is from?
Post by: JPDeni on August 27, 2009, 01:02:08 AM
QuoteIs it done with IP address?

Probably. I found some partial code, but I couldn't find the whole thing that would actually work.
Title: Re: View block by what country user is from?
Post by: Freddy on August 27, 2009, 12:18:44 PM
I wanted to do this once on a site i had, but all I found at the time was I needed a huge database.  So this interested me, I have some code on looking up the country from the IP which works (without big database) so let me know if you need it JP :)

Oh BTW, this isn't SMF code...not sure if it would be better to use SMF code or this  ???
Title: Re: View block by what country user is from?
Post by: JPDeni on August 27, 2009, 02:25:51 PM
I may have found something that I can use, but I'm not sure I understand how it works. Or if it works at all. Before I post anything here, I'll do a little test on my site and get my members to help out. I'll be back in a day or so after I've had a chance to work with it.
Title: Re: View block by what country user is from?
Post by: mc on August 27, 2009, 03:18:51 PM
I've just read the first post, and if there are two seperate domains, then would it not be easier to detect what domain (ca or com) the visitor is using?
Title: Re: View block by what country user is from?
Post by: Freddy on August 27, 2009, 03:23:15 PM
Yes good point  8)
Title: Re: View block by what country user is from?
Post by: JPDeni on August 27, 2009, 03:27:15 PM
The way I read the original post is that ApplianceJunk has a website with one domain. He is an affiliate of an appliance parts company which has two websites, one for the US and one for Canada. He is wanting to direct his US visitors to the parts company's US site and his Canadian visitors to the parts company's Canadian site.
Title: Re: View block by what country user is from?
Post by: mc on August 27, 2009, 03:32:50 PM
JP is right.

Just re-read the original post again.
How about a simple re-direct page with the options for going to the ca or com website?
It would be a far simpler solution, unless you want to display country specific writing within the block..
Title: Re: View block by what country user is from?
Post by: JPDeni on August 27, 2009, 03:36:19 PM
I'm finding that it's not that hard to find out the country, although I haven't had any Canadian members come by yet so I can't be sure that it picks up Canada.

I should have code for this tomorrow.
Title: Re: View block by what country user is from?
Post by: JPDeni on August 27, 2009, 04:05:18 PM
If anybody wants to help, I have a page at http://www.morethanspike.com/index.php?page=35 to test it out. Since ApplianceJunk is particularly interested in Canadian visitors, it would be great if anybody from Canada would let me know if it works for you.

I noticed that some people get "XX" returned. So far it's just been folks in the US who have that, but it could be others as well.
Title: Re: View block by what country user is from?
Post by: Freddy on August 27, 2009, 04:16:04 PM
Not from Canada of course, but just to let you know it worked...

"Your country is UK"
Title: Re: View block by what country user is from?
Post by: JPDeni on August 27, 2009, 04:23:25 PM
Well, it looks like things are working, so I'll go ahead with the code.


$country = file_get_contents('http://api.hostip.info/country.php?ip='.$_SERVER['REMOTE_ADDR']);
if ($country == 'CA') {
  echo 'whatever you want to show to Canadian people';
}
elseif ($country == 'US' || $country == 'XX') {
  echo 'whatever you want to show to US people';
}
else {
  echo 'whatever you want to show to everyone else';
}


The variable $country will have an uppercase two-letter term that corresponds to the country. There is also 'XX' which so far appears to only show up for US folks, but it's possible that it's any place that the database can't determine.
Title: Re: View block by what country user is from?
Post by: ZarPrime on August 27, 2009, 05:34:31 PM
Works for me for US.
Title: Re: View block by what country user is from?
Post by: IchBin on August 27, 2009, 06:22:46 PM
Using a Canadian proxy it indeed showed that I was from Canada. :) Good work Deni!
Title: Re: View block by what country user is from?
Post by: JPDeni on August 27, 2009, 06:24:22 PM
Cool. Looks like it's working, then. It was just something I found on a Google search. :) It's a tool that can be used in a lot of different things.