Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I limit which countries can view my website ( PHP )

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?

like image 394
Brian G Avatar asked Oct 03 '08 14:10

Brian G


2 Answers

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.

like image 160
Gilles Avatar answered Oct 24 '22 10:10

Gilles


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!

like image 27
reefnet_alex Avatar answered Oct 24 '22 12:10

reefnet_alex