Basically I want to display a simple piece of code (jscript) for uk visitors, and a different script for others. I'm guessing I could possibly detect locations using php then output the correct code accordingly? Any help much appreciated, Martyn
Yes, you would want to use something to covert the users ip address to a geographic location. Google maps' Geocoder may do this, http://www.trueknowledge.com/api/ is another. Then just have a few checks to see if they are in the country that needs the proper script.
What you are talking about is geolocating an IP address. There are scripts to do that here: http://www.tellinya.com/read/2007/06/03/2.html and here: http://www.ardamis.com/2008/01/27/how-to-geolocate-visitors-using-an-ip-to-country-database/ These create databases on your server with lookup tables of IP ranges. Alternatively you could use hostip.info. Here is an example script to do that: <?php $ip=$_SERVER['REMOTE_ADDR']; $country = file_get_contents("http://api.hostip.info/country.php?ip=".$ip); echo "Your IP address is $ip<br />\n"; echo "Your country code is $country<br />\n"; if ($country == "XX") { echo "Unable to locate your country"; } elseif ($country == "UK") { echo "You are in the UK"; } else { echo "You are NOT in the UK."; } ?> PHP:
Cheers stoli, That gives me something to work with... Makes me wonder whether I could use it to change language of site to suit the visitor, although that may be going to far! Thanks for the code and links, Martyn
Glad to be able to help. Once you have geolocated a user and stored that location as part of a session, then it is pretty much unlimited how far you want to go with specific regional variations, so long as it degrades when you are unable to locate them and you provide the option to change location.
We cannot Directly get Location Name or Details but we get Ip of that location By ipwe can get address details. For more information: cattechnologies.com
One we use targets down to the City: http://www.maxmind.com/app/geolitecity They also provide a PHP api to simplify integration. Note that they offer two versions, free and fee. The free DB is not as accurate as the fee based counterpart. -Bing
maxmind works great, and they provide free PHP code with examples for their API. If you only need to find out the country the free geolite City db is good enough. If you want more accuracy you might want to look at the paid versions. Take a look at http://getmyip.eu/ for a little demonstration of what you can do with even more detailed (paid) webservices.