There can be some requirements that redirect the page or may be block the access of some section or some part of website based on country. There can be some situation that you may need to change some contents based on country.
Requirement: 1. To find the country when website is opened or find the country from IP address.
Solution: This requirement can be catered in several ways.
You can add some code in application start to identify the IP and then country, city, region likewise details from the IP.
There are many API(s) that will give the details.
- IPSTACK: ipstack offers a powerful, real-time IP to geolocation API capable of looking up accurate location data and assessing security threats originating from risky IP addresses. Results are delivered within milliseconds in JSON or XML format. Using the ipstack API you will be able to locate website visitors at first glance and adjust your user experience and application accordingly.
IPSTACK Ap is very simple to use. https://ipstack.com/documentation
https://api.ipstack.com/134.201.250.155 ? access_key = YOUR_ACCESS_KEY
You can get free access_key by following above link.
API Response:
{ "ip": "134.201.250.155", "hostname": "134.201.250.155", "type": "ipv4", "continent_code": "NA", "continent_name": "North America", "country_code": "US", "country_name": "United States", "region_code": "CA", "region_name": "California", "city": "Los Angeles", "zip": "90013", "latitude": 34.0453, "longitude": -118.2413, "location": { "geoname_id": 5368361, "capital": "Washington D.C.", "languages": [ { "code": "en", "name": "English", "native": "English" } ], "country_flag": "https://assets.ipstack.com/images/assets/flags_svg/us.svg", "country_flag_emoji": "", "country_flag_emoji_unicode": "U+1F1FA U+1F1F8", "calling_code": "1", "is_eu": false }, "time_zone": { "id": "America/Los_Angeles", "current_time": "2018-03-29T07:35:08-07:00", "gmt_offset": -25200, "code": "PDT", "is_daylight_saving": true }, "currency": { "code": "USD", "name": "US Dollar", "plural": "US dollars", "symbol": "$", "symbol_native": "$" }, "connection": { "asn": 25876, "isp": "Los Angeles Department of Water & Power" }, "security": { "is_proxy": false, "proxy_type": null, "is_crawler": false, "crawler_name": null, "crawler_type": null, "is_tor": false, "threat_level": "low", "threat_types": null } }
Solution 2: using https://ipinfo.io/ api.
Using simple javascript:
$.get(“http://ipinfo.io”, function (response) {
if(response.country == “NP”)
{
//window.location.replace(“site for Nepal”);
}
else
{
//window.location.replace(“http://www.w3schools.com”);
}
Response in above js
{ "ip": "27.147.135.242", "city": "Paltan", "region": "Dhaka", "country": "BD", "loc": "23.7916,90.4152", "org": "AS23688 Link3 Technologies Ltd.", "postal": "1212", "timezone": "Asia/Dhaka", "readme": "https://ipinfo.io/missingauth" }
Solution 3: C#
Get IP
//Get CountyLocation based on IP public static string GetCountyLocation(string IP) { string url = "http://api.ipstack.com/" + IP + "?access_key=[KEY]"; var request = System.Net.WebRequest.Create(url); using (WebResponse wrs = request.GetResponse()) using (Stream stream = wrs.GetResponseStream()) using (StreamReader reader = new StreamReader(stream)) { string json = reader.ReadToEnd(); var obj = JObject.Parse(json); string City = (string)obj["city"]; string Country = (string)obj["region_name"]; string CountryCode = (string)obj["country_code"]; return (CountryCode + " - " + Country +"," + City); } return ""; } //To Get IP protected string GetIPAddress() { System.Web.HttpContext context = System.Web.HttpContext.Current; string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (!string.IsNullOrEmpty(ipAddress)) { string[] addresses = ipAddress.Split(','); if (addresses.Length != 0) { return addresses[0]; } } return context.Request.ServerVariables["REMOTE_ADDR"]; }
No comments:
Post a Comment