Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

In there an easy way to do this in PHP. I want to make sure that only web requests from certain countries are able to access my website.

Any ideas?

share|improve this question
3  
I'm not going to down vote the question (I'm kinda interested from a technical perspective), but I hate it when websites do that. I've always been able to get around it anyway by using the right proxy. – wcm Oct 3 '08 at 14:24
    
Proxies can be blocked too and remember that he might need to block access for legal reasons. In many cases it's illegal for US-based businesses to have their websites available to some countries that are in an official blacklist (Iran for example). – Gilles Oct 3 '08 at 14:26
1  
And if I lived in those countries I would dislike it as well. I just don't like things that smack of censorship. I actually up voted the question (and your response) because I understand there are legitimate reasons for doing this. I just don't have to like it. – wcm Oct 3 '08 at 14:34
up vote 13 down vote accepted

Use an IP geolocation database (some are free) and $_SERVER["REMOTE_ADDR"] to get the visitor's IP address.

http://www.maxmind.com/app/geolitecity is a free (less accurate) version of a commercial one.

share|improve this answer

Like Gilles, I've used MaxMind's GeoIP stuff for this in the past - configured with the PECL extension for speed. It works reasonably well, but...

The requirement for this kind of thing tends to come from somebody that doesn't understand that it is impossible to reliably determine a visitors location in this way. It's very important that the person asking for it be encouraged to understand that it is almost useless. Typical thing that happens with geo-location in this:

Client: I want to be able to restrict content by IP
Dev: You do know that that is impossible to do reliably?
Client: Ah yes, but this company say they will sell me something that will do it
Dev: Yes but it isn't accurate and is easy to circumvent and usually indicates a poor business model for internet based content
Client: Can you do it?
Dev: Whatever...

...Six months later...

Client: Some of my visitors have been complaining they can't see my content and some bad people who shouldn't see it have been able to!
Dev: /me slaps head

It's only one step on from there to "can I have it so that when a user right clicks in their browser a little sign pops up saying 'these images are copyright Idiot Inc.'?"

Sorry, obviously in a cynical mood today!

share|improve this answer
1  
Remember that he might be legally bound to block some countries. I worked for a big american corp who legally had to block access from a government-issued blacklist of countries. Failure to comply could have landed people in jail. I can't talk about the specifics but this was very real. – Gilles Oct 3 '08 at 15:21
    
Aye, in the case I've been asked to use it has been part of a legal agreement between a content provider and a publisher. But that's kind of my point - this is flawed thinking, legally obliging the impossible. Still implement, but do feedback the reality of the situation! – reefnet_alex Oct 3 '08 at 15:53
    
Oh, and on that note, I've seen people "legally require" the right click copyright message too. It beggars belief! – reefnet_alex Oct 3 '08 at 15:54
1  
Very realistic scene... Alas. – PhiLho Oct 3 '08 at 16:09
1  
+1 for your pain... – nickf Nov 18 '08 at 1:01

You can use the http://ipinfo.io API for this. The PHP code would look something like this:

$allowed_countries = array("US", "GB", ...);
$country = file_get_contents("http://ipinfo.io/{$_SERVER['REMOTE_ADDR']}/country");
if(!in_array($country, $allowed_countries)) {
    // Display an error message, or redirect to another website
}

See http://ipinfo.io/developers for more details.

share|improve this answer
    
Can I do it somehow $restricted_countries = array("US", "GB", ...);? – Kaspar L. Palgi Nov 2 '15 at 15:34
    
Yeah of course, and flip the conditional – Ben Dowling Nov 2 '15 at 21:01

Both of the answers (geolocation, user agent) will work but can be defeated. Someone can use a proxy server, or change their user agent. Firefox even has a plugin for that purpose.

share|improve this answer
    
Proxies can be blocked at a low protocol level. Hulu.com does just that. User agents have nothing to do with the problem here. – Gilles Oct 3 '08 at 14:51
    
How are you going to reliably block all proxies? The only reliable solution is blacklisting, but new proxies pop up all the time. – ceejayoz Oct 3 '08 at 14:59
    
They detect them at a low protocol level, not using a blacklist. I don't remember the specifics, I just remember reading about such a technique being used by hulu, that's all. – Gilles Oct 3 '08 at 15:03

if you use the answer of Ben Dowling and the code doesn't work, try this:

if (!in_array(substr($country, 0, 2), $allowed_countries)) {

instead of:

if(!in_array($country, $allowed_countries)) {
share|improve this answer

There's also a pear package

http://pear.php.net/package/Net_Geo

share|improve this answer
    
It looks like it's querying a website to get the information every time? The data might be more accurate than the free database I've mentioned though. – Gilles Oct 3 '08 at 15:01

There are certain classes that can detect the locale of the user agent. Try something like that. Zend Framework has a great class for this Zend_Locale

share|improve this answer
    
Doesn't a "Locale" just tell how a client is configured not where it is? Couldn't someone have their Locale set to "en_US" but be in Australia? – Dave Webb Oct 3 '08 at 14:41
    
Yeah exactly, the locale is the language of the browser, nothing to do with location. – Gilles Oct 3 '08 at 14:52
    
But it might filter out people you don't like. For example, I am shutting down russian and indian visitors to my website by lang. For the most part, it works. – flaab Jul 26 '13 at 14:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.