Well this could be done by using the free GeoIP database from Maxmind
http://www.maxmind.com/app/geolitecity"GeoLite City
GeoLite City is similar to the GeoIP City database, but is less accurate. Should you require greater accuracy, GeoIP City is a drop-in replacement for GeoLite City. GeoLite City is an ideal cost-effective solution, since it is more accurate than many other commercial geolocation solutions." etc