In this tutorial we will find country by IP address, usually a website required this functionality to give a personalized experience to its visitor that includes localization of content i.e. if a user comes from Pakistan the content should be in Urdu as this is national language of the country, or if it come form china content should be in Chinese so on and so forth, other reason may include, to give visitor ads according to there locality, a reason may to block visitors from certain country where a specific offer is not available and many more reasons...
Out main concern is how to do it? there are many ways of doing it
Now Let the Code Talk
to find Location from IP address i recommend using an IP database there are two ways of doing this, first download a IP database and install it on you own server and find location from there, and update it monthly or bi-monthly.
Second is more cool way of doing this using an API... go to ipinfodb.com sign up there and get API key and use the following code
Out main concern is how to do it? there are many ways of doing it
- you can use $_SERVER["HTTP_ACCEPT_LANGUAGE"] it returns a comma separated values in the format "en-us,en;q=0.5" first value en-us tell its from US, it may en-uk: UK, en-jp: Janpan..
- or you can use ip database lookup for it (we will discuss it)
- you can buy a paid service (which i am not intrested)
- or you can explicitly ask a user from where he belongs...
Now Let the Code Talk
to find Location from IP address i recommend using an IP database there are two ways of doing this, first download a IP database and install it on you own server and find location from there, and update it monthly or bi-monthly.
Second is more cool way of doing this using an API... go to ipinfodb.com sign up there and get API key and use the following code
function getLocationViaIP($IP_ADDRSS,$CITY=false){ $KEY="YOUR_API_KEY_HERE"; $TYPE=$CITY==true?"ip-city":"ip-country"; // Check if city information is requried too $API_URL="http://api.ipinfodb.com/v3/$TYPE/?key=$KEY&ip=$IP_ADDRSS&format=xml"; // Construst API URL // there two more formats json and raw if preder xml // there is another parameter callback if you use it // via javascript it will be handy $xml=simplexml_load_file($API_URL); if($xml->statusCode=="OK"){ // Check if everything is OK echo "Country Name:".$xml->countryCode."< b r >"; echo "Country Code:".$xml->countryName."< b r >"; if($CITY){ // Print City information too if requried echo "Region Name:".$xml->regionName."< b r >"; echo "City Name:".$xml->cityName."< b r >"; echo "Zip Code:".$xml->zipCode."< b r >"; echo "Latitude:".$xml->latitude."< b r >"; echo "Longitude:".$xml->longitude."< b r >"; echo "Time Zone:".$xml->timeZone."< b r >"; } }else{ echo "Somthing Went Wrong"; } }(break line tag intentionally written like this for display purpose)to call this method for example
$this->getLocationViaIP("74.125.45.100",true); // alternativly you can use this too $this->getLocationViaIP($_SERVER["REMOTE_ADDR"],true);it will be a good idea that you store IP's Location somewhere in Cookies or in you local DB to minimize extra call to API.
0 comments:
Post a Comment