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?
|
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? |
|||||||||||||
|
|
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 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:
...Six months later...
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! |
|||||||||||||||||||||
|
|
You can use the http://ipinfo.io API for this. The PHP code would look something like this:
See http://ipinfo.io/developers for more details. |
|||||||||
|
|
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. |
|||||||||||||
|
|
if you use the answer of Ben Dowling and the code doesn't work, try this:
instead of:
|
|||
|
|
|
There's also a pear package |
|||||
|
|
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 |
|||||||||||||
|