-1

Does anyone know of a way to load a page ONLY if the visitor IP is from a specified country? If not, the visitor should be redirected to the right country page. I'm looking to do this in PHP if possible.

Eg. If visitor IP is showing UK, the page loads. Otherwise, they are redirected depending on their IP; if it's a US IP address, they are redirected to the US webpage; if it's a CA IP address, they are redirected to the Canadian webpage.

Note:
1) I know this method may not always be accurate since the visitor may be accessing via VPN/proxies, but it's still the preferred method so appreciate any help on this.
2) The alternative to the above example is of course to simply redirect all visitors depending on their IPs. But I am specifically looking to load a page and not redirect a visitor if they are from a specified country. Thanks for your help in advance!
3) I already know how to redirect visitors based on IP address. That's not the part I'm having trouble with. It's the part on "load THIS page if IP address is from a specified country". Apologies for any confusion!

archer9
  • 59
  • 1
  • 5
  • Possible duplicate of [PHP - Detect country](http://stackoverflow.com/questions/14414705/php-detect-country), [package for detecting users contry in php?](http://stackoverflow.com/questions/4457920/package-for-detecting-users-contry-in-php) or [Geo Location based on IP Address - PHP](http://stackoverflow.com/questions/7766978/geo-location-based-on-ip-address-php) – Oriol Oct 06 '13 at 01:02

1 Answers1

0

I'm not going to just write you some code, but I'll get you started on some concepts.

You are right in thinking that IP's have certain characteristics that can help to distinguish country. So do some research on what those characteristics are.

Here's how to apply them: You will want to have a function that takes there IP and splits it into several strings based on the "."s. Then, use your research to determine the country.

After that, its a simple matter of an if statement that sends off the user to another site based on the "country" var that you should have created in the function.

<?php
if (country == blah)
    header("Location: http:***");
?>

Helpful?

Isaiah Taylor
  • 615
  • 1
  • 4
  • 17
  • Not really... I already knew how to redirect to multiple pages based on IP. That's very easy to figure out with a quick Google. That's not the part I am having trouble with. I can't seem to find an answer to "load THIS page if the IP address is from a specified country". – archer9 Oct 06 '13 at 01:04
  • But I am specifically looking to load a page and not redirect a visitor if they are from a specified country. Thanks for your help in advance! What is the difference? – Isaiah Taylor Oct 06 '13 at 01:07
  • There's a multitude of commercial reasons why loading a page instead of redirecting may be helpful. A simple reason I can think of is to ensure that visitors are only able to view a page relevant to their country and would NOT see another country's page. Sure, you can get around this if you really want to, but the idea is to make it harder for this to be done. So do you know how to do this? Thanks. – archer9 Oct 06 '13 at 01:11
  • Yeah. A redirect to the correct website. Are you talking about loading content only and not a whole new actual page? – Isaiah Taylor Oct 06 '13 at 01:13
  • Yep, the main goal is loading content not a whole new page. Was hoping to avoid iframes if possible... – archer9 Oct 06 '13 at 01:15
  • AHH ok so you want to load specific stuff based on country. – Isaiah Taylor Oct 06 '13 at 01:18
  • Re-reading my last post, I think it was quite misleading. Clarification: although the main goal is to load different content baesd on country, I need to do this by creating different pages for different countries and hence my original question. – archer9 Oct 06 '13 at 01:24
  • So different pages meaning... blahblahblag.com/usa or /russia? If so, you would still be redirecting. – Isaiah Taylor Oct 06 '13 at 01:26
  • The page would ONLY redirect if the visitor is trying to visit the wrong page. eg. US IP addresses trying to visit page with UK information would get redirected to US page. But if it's a UK IP address, the page JUST LOADS as it should. It does NOT redirect a visitor to another page. Is that clear? – archer9 Oct 06 '13 at 01:30
  • Very. So just have an if statement that operates on it if the IP is from the wrong country. For example, if it is /usa and a russian is trying to access it, the if statement would catch it and redirect. If it was an american, the ip would not fall under those conditions and nothing would happen. – Isaiah Taylor Oct 06 '13 at 01:34
  • Ok, never mind. Looks like you don't know the code either so I'll look on my own... – archer9 Oct 06 '13 at 02:03
  • Whatever, man. I've answered the question based on what you've given me. Sorry if I was unable to read your mind. – Isaiah Taylor Oct 06 '13 at 13:36