Get Started With IP2location HTTP Module
Dependencies
This module requires IP2Location BIN database to function. You may download the BIN database at
IP2Location LITE BIN Data (Free): https://lite.ip2location.com
IP2Location Commercial BIN Data (Comprehensive): https://www.ip2location.com
Requirements
Visual Studio 2010 or later.
Microsoft .NET 3.5 framework.
IntX.
Microsoft ILMerge.
Supported Microsoft IIS Versions: 7.0, 7.5, 8.0, 8.5, 10.0 (website needs to be running under a .NET 2.0 application pool in integrated mode)
Compilation
Just open the solution file in Visual Studio and compile. Or just use the IP2LocationHTTPModule.dll in the dll folder.
Note
After compilation, the final IP2LocationHTTPModule.dll will be in the merged folder as the post-build event will merge the IntXLib.dll with the original IP2LocationHTTPModule.dll to make it easier for deployment.
Installation
You can choose to install the IP2Location IP Geolocation HTTP Module in either per website mode or per server mode.
Note
In per-website mode, you must install and configure IP2Location for each site individually.
In per-server mode, a single installation covers all websites on the server.
Per website mode
Copy the IP2LocationHTTPModule.dll, IP2Location-config.xml and the BIN data file to the bin folder of your website.
-
Modify your web.config as below:
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <modules runAllManagedModulesForAllRequests="true"> <add name="IP2LocationModule" type="IP2Location.HTTPModule" /> </modules> </system.webServer> </configuration>
-
Open the IP2Location-config.xml file in your bin folder using any text editor and you can see the below:
<?xml version="1.0" encoding="utf-8"?> <IP2Location_Configuration xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Settings> <BIN_File>bin\your_database_file.BIN</BIN_File> <Company_Name></Company_Name> <License_Key></License_Key> <Custom_IP_Server_Variable>HTTP_X_FORWARDED_FOR</Custom_IP_Server_Variable> <Enabled_Server_Variables>True</Enabled_Server_Variables> </Settings> <ByPassIPs> <ByPassIP> <IP>1.2.3.4</IP> </ByPassIP> </ByPassIPs> <BlockRules> <BlockRule> <URL_Regex>.*\.php</URL_Regex> <Comparison>NOT IN</Comparison> <Countries>US,CA,MY</Countries> <Enabled_Rule>True</Enabled_Rule> </BlockRule> </BlockRules> <RedirectRules> <RedirectRule> <URL_Regex>.*/Default\.aspx</URL_Regex> <Redirect_To_URL>http://www.google.sg</Redirect_To_URL> <Comparison>IN</Comparison> <Countries>MY,SG,AU</Countries> <Enabled_Rule>True</Enabled_Rule> </RedirectRule> </RedirectRules> </IP2Location_Configuration>
What should I change? (ByPassIPs, BlockRules & RedirectRules are optional if you do not require them)
- <BIN_File> – The relative path for the BIN database file that you have copied into the bin folder of your website. (relative to the root of your website)
- <Company_Name> – No longer required, just leave this blank
- <License_Key> – No longer required, just leave this blank
- <Custom_IP_Server_Variable> – Leave blank unless you need to read the IP from a custom field.
- <Enabled_Server_Variables> – When the value is True, your webpages can have access to IP Geolocation data for your visitor’s IP address via server variables. You can turn this off by changing the value to False.
- Under <ByPassIPs> , each <ByPassIP> is used to configure an IP address to bypass the block/redirect rules. Just add another <ByPassIP> segment if you want to have another IP address bypass the rules.
- Under <BlockRules> , each <BlockRule> is used to configure a rule for blocking visitor access to the website. Just add another <BlockRule> segment if you want another rule.
- <URL_Regex> – A regular expression string to match various pages and sub-folders.
- <Comparison> – Allows 2 values; either IN or NOT IN. In the case of IN, if the visitor’s country is in the <Countries> list for that rule and the <URL_Regex> matches the browsed URL then the visitor will be blocked via a HTTP 403 status.
- <Enabled_Rule> – Accepts either True or False and this just turns on or off the rule.
- <RedirectRules> and <RedirectRule> works similar to the <BlockRules> , except the visitor is redirected to the URL specified in <Redirect_To_URL> instead of being blocked.
Per server mode
Create a new folder.
Copy the IP2ProxyHTTPModule.dll, IP2Proxy-config.xml and the BIN data file to that folder.
-
Create a Windows environment system variable to store the path of the new folder.
Open the Control Panel then double-click on System then click on Advanced System Settings.
Click on the Environment Variables button to open up the Environment Variable settings.
Under System variables, create a new variable called IP2ProxyHTTPModuleConfig and set the value to the full path of the new folder.
-
Create a PowerShell script called installgac.ps1 and paste the following code into it.
Set-location "C:\<new folder>" [System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a") $publish = New-Object System.EnterpriseServices.Internal.Publish $publish.GacInstall("C:\<new folder>\IP2ProxyHTTPModule.dll") iisreset
-
Create a PowerShell script called uninstallgac.ps1 and paste the following code into it.
Set-location "C:\<new folder>" [System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a") $publish = New-Object System.EnterpriseServices.Internal.Publish $publish.GacRemove("C:\<new folder>\IP2LocationHTTPModule.dll") iisreset
In both scripts, edit the 2 lines containing the path to the full path for your new folder then save the scripts.
Run installgac.ps1 to install the dll into the GAC. Keep the uninstallgac.ps1 in case you need to uninstall the dll.
-
Installing the module in IIS.
- Open the IIS Manager then navigate to the server level settings and double-click on the Modules icon.
- In the Modules settings, click on the Add Managed Module at the right-hand side.
- Key in IP2LocationHTTPModule for the Name and select IP2Location.HTTPModule as the Type.
- Click OK then restart IIS to complete the installation.
-
Open the IP2Location-config.xml in your new folder using any text editor. Fill in the
tag with the absolute path to your BIN data file and remove the HTTP_X_FORWARDED_FOR if your website is not behind a proxy. Save your changes. <?xml version="1.0" encoding="utf-8"?> <IP2Location_Configuration xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Settings> <BIN_File>C:\<folder>\<BIN filename></BIN_File> <Company_Name></Company_Name> <License_Key></License_Key> <Custom_IP_Server_Variable>HTTP_X_FORWARDED_FOR</Custom_IP_Server_Variable> <Enabled_Server_Variables>True</Enabled_Server_Variables> </Settings> </IP2Location_Configuration>
Sample Codes
Query geolocation information
You can query the geolocation information by using codes below:
Private Sub ShowServerVariable()
Response.Write(Request.ServerVariables("REMOTE_ADDR") & "<br>")
Response.Write(Request.ServerVariables("HTTP_X_COUNTRY_SHORT") & "<br>")
Response.Write(Request.ServerVariables("HTTP_X_COUNTRY_LONG") & "<br>")
Response.Write(Request.ServerVariables("HTTP_X_COUNTRY_REGION") & "<br>")
Response.Write(Request.ServerVariables("HTTP_X_COUNTRY_CITY") & "<br>")
Response.Write(Request.ServerVariables("HTTP_X_COUNTRY_LATITUDE") & "<br>")
Response.Write(Request.ServerVariables("HTTP_X_COUNTRY_LONGITUDE") & "<br>")
Response.Write(Request.ServerVariables("HTTP_X_COUNTRY_ZIPCODE") & "<br>")
Response.Write(Request.ServerVariables("HTTP_X_COUNTRY_TIMEZONE") & "<br>")
Response.Write(Request.ServerVariables("HTTP_X_COUNTRY_ISP") & "<br>")
Response.Write(Request.ServerVariables("HTTP_X_COUNTRY_DOMAIN") & "<br>")
Response.Write(Request.ServerVariables("HTTP_X_COUNTRY_NETSPEED") & "<br>")
Response.Write(Request.ServerVariables("HTTP_X_COUNTRY_IDD_CODE") & "<br>")
Response.Write(Request.ServerVariables("HTTP_X_COUNTRY_AREA_CODE") & "<br>")
Response.Write(Request.ServerVariables("HTTP_X_COUNTRY_WEATHER_CODE") & "<br>")
Response.Write(Request.ServerVariables("HTTP_X_COUNTRY_WEATHER_NAME") & "<br>")
Response.Write(Request.ServerVariables("HTTP_X_COUNTRY_MCC") & "<br>")
Response.Write(Request.ServerVariables("HTTP_X_COUNTRY_MNC") & "<br>")
Response.Write(Request.ServerVariables("HTTP_X_COUNTRY_MOBILE_BRAND") & "<br>")
Response.Write(Request.ServerVariables("HTTP_X_COUNTRY_ELEVATION") & "<br>")
Response.Write(Request.ServerVariables("HTTP_X_COUNTRY_USAGE_TYPE") & "<br>")
Response.Write(Request.ServerVariables("HTTP_X_COUNTRY_ADDRESS_TYPE") & "<br>")
Response.Write(Request.ServerVariables("HTTP_X_COUNTRY_CATEGORY") & "<br>")
Response.Write(Request.ServerVariables("HTTP_X_COUNTRY_DISTRICT") & "<br>")
Response.Write(Request.ServerVariables("HTTP_X_COUNTRY_ASN") & "<br>")
Response.Write(Request.ServerVariables("HTTP_X_COUNTRY_AS") & "<br>")
End Sub
private void ShowServerVariable()
{
Response.Write(Request.ServerVariables["REMOTE_ADDR"] + "\n");
Response.Write(Request.ServerVariables["HTTP_X_COUNTRY_SHORT"] + "\n");
Response.Write(Request.ServerVariables["HTTP_X_COUNTRY_LONG"] + "\n");
Response.Write(Request.ServerVariables["HTTP_X_COUNTRY_REGION"] + "\n");
Response.Write(Request.ServerVariables["HTTP_X_COUNTRY_CITY"] + "\n");
Response.Write(Request.ServerVariables["HTTP_X_COUNTRY_LATITUDE"] + "\n");
Response.Write(Request.ServerVariables["HTTP_X_COUNTRY_LONGITUDE"] + "\n");
Response.Write(Request.ServerVariables["HTTP_X_COUNTRY_ZIPCODE"] + "\n");
Response.Write(Request.ServerVariables["HTTP_X_COUNTRY_TIMEZONE"] + "\n");
Response.Write(Request.ServerVariables["HTTP_X_COUNTRY_ISP"] + "\n");
Response.Write(Request.ServerVariables["HTTP_X_COUNTRY_DOMAIN"] + "\n");
Response.Write(Request.ServerVariables["HTTP_X_COUNTRY_NETSPEED"] + "\n");
Response.Write(Request.ServerVariables["HTTP_X_COUNTRY_IDD_CODE"] + "\n");
Response.Write(Request.ServerVariables["HTTP_X_COUNTRY_AREA_CODE"] + "\n");
Response.Write(Request.ServerVariables["HTTP_X_COUNTRY_WEATHER_CODE"] + "\n");
Response.Write(Request.ServerVariables["HTTP_X_COUNTRY_WEATHER_NAME"] + "\n");
Response.Write(Request.ServerVariables["HTTP_X_COUNTRY_MCC"] + "\n");
Response.Write(Request.ServerVariables["HTTP_X_COUNTRY_MNC"] + "\n");
Response.Write(Request.ServerVariables["HTTP_X_COUNTRY_MOBILE_BRAND"] + "\n");
Response.Write(Request.ServerVariables["HTTP_X_COUNTRY_ELEVATION"] + "\n");
Response.Write(Request.ServerVariables["HTTP_X_COUNTRY_USAGE_TYPE"] + "\n");
Response.Write(Request.ServerVariables["HTTP_X_COUNTRY_ADDRESS_TYPE"] + "\n");
Response.Write(Request.ServerVariables["HTTP_X_COUNTRY_CATEGORY"] + "\n");
Response.Write(Request.ServerVariables["HTTP_X_COUNTRY_DISTRICT"] + "\n");
Response.Write(Request.ServerVariables["HTTP_X_COUNTRY_ASN"] + "\n");
Response.Write(Request.ServerVariables["HTTP_X_COUNTRY_AS"] + "\n");
}
<%=Request.ServerVariables("REMOTE_ADDR") & "<br>"%>
<%=Request.ServerVariables("HTTP_X_COUNTRY_SHORT") & "<br>"%>
<%=Request.ServerVariables("HTTP_X_COUNTRY_LONG") & "<br>"%>
<%=Request.ServerVariables("HTTP_X_COUNTRY_REGION") & "<br>"%>
<%=Request.ServerVariables("HTTP_X_COUNTRY_CITY") & "<br>"%>
<%=Request.ServerVariables("HTTP_X_COUNTRY_LATITUDE") & "<br>"%>
<%=Request.ServerVariables("HTTP_X_COUNTRY_LONGITUDE") & "<br>"%>
<%=Request.ServerVariables("HTTP_X_COUNTRY_ZIPCODE") & "<br>"%>
<%=Request.ServerVariables("HTTP_X_COUNTRY_TIMEZONE") & "<br>"%>
<%=Request.ServerVariables("HTTP_X_COUNTRY_ISP") & "<br>"%>
<%=Request.ServerVariables("HTTP_X_COUNTRY_DOMAIN") & "<br>"%>
<%=Request.ServerVariables("HTTP_X_COUNTRY_NETSPEED") & "<br>"%>
<%=Request.ServerVariables("HTTP_X_COUNTRY_IDD_CODE") & "<br>"%>
<%=Request.ServerVariables("HTTP_X_COUNTRY_AREA_CODE") & "<br>"%>
<%=Request.ServerVariables("HTTP_X_COUNTRY_WEATHER_CODE") & "<br>"%>
<%=Request.ServerVariables("HTTP_X_COUNTRY_WEATHER_NAME") & "<br>"%>
<%=Request.ServerVariables("HTTP_X_COUNTRY_MCC") & "<br>"%>
<%=Request.ServerVariables("HTTP_X_COUNTRY_MNC") & "<br>"%>
<%=Request.ServerVariables("HTTP_X_COUNTRY_MOBILE_BRAND") & "<br>"%>
<%=Request.ServerVariables("HTTP_X_COUNTRY_ELEVATION") & "<br>"%>
<%=Request.ServerVariables("HTTP_X_COUNTRY_USAGE_TYPE") & "<br>"%>
<%=Request.ServerVariables("HTTP_X_COUNTRY_ADDRESS_TYPE") & "<br>"%>
<%=Request.ServerVariables("HTTP_X_COUNTRY_CATEGORY") & "<br>"%>
<%=Request.ServerVariables("HTTP_X_COUNTRY_DISTRICT") & "<br>"%>
<%=Request.ServerVariables("HTTP_X_COUNTRY_ASN") & "<br>"%>
<%=Request.ServerVariables("HTTP_X_COUNTRY_AS") & "<br>"%>
<?php
echo $_SERVER['REMOTE_ADDR'] . "<br>";
echo $_SERVER['HTTP_X_COUNTRY_SHORT'] . "<br>";
echo $_SERVER['HTTP_X_COUNTRY_LONG'] . "<br>";
echo $_SERVER['HTTP_X_COUNTRY_REGION'] . "<br>";
echo $_SERVER['HTTP_X_COUNTRY_CITY'] . "<br>";
echo $_SERVER['HTTP_X_COUNTRY_LATITUDE'] . "<br>";
echo $_SERVER['HTTP_X_COUNTRY_LONGITUDE'] . "<br>";
echo $_SERVER['HTTP_X_COUNTRY_ZIPCODE'] . "<br>";
echo $_SERVER['HTTP_X_COUNTRY_TIMEZONE'] . "<br>";
echo $_SERVER['HTTP_X_COUNTRY_ISP'] . "<br>";
echo $_SERVER['HTTP_X_COUNTRY_DOMAIN'] . "<br>";
echo $_SERVER['HTTP_X_COUNTRY_NETSPEED'] . "<br>";
echo $_SERVER['HTTP_X_COUNTRY_IDD_CODE'] . "<br>";
echo $_SERVER['HTTP_X_COUNTRY_AREA_CODE'] . "<br>";
echo $_SERVER['HTTP_X_COUNTRY_WEATHER_CODE'] . "<br>";
echo $_SERVER['HTTP_X_COUNTRY_WEATHER_NAME'] . "<br>";
echo $_SERVER['HTTP_X_COUNTRY_MCC'] . "<br>";
echo $_SERVER['HTTP_X_COUNTRY_MNC'] . "<br>";
echo $_SERVER['HTTP_X_COUNTRY_MOBILE_BRAND'] . "<br>";
echo $_SERVER['HTTP_X_COUNTRY_ELEVATION'] . "<br>";
echo $_SERVER['HTTP_X_COUNTRY_USAGE_TYPE'] . "<br>";
echo $_SERVER['HTTP_X_COUNTRY_ADDRESS_TYPE'] . "<br>";
echo $_SERVER['HTTP_X_COUNTRY_CATEGORY'] . "<br>";
echo $_SERVER['HTTP_X_COUNTRY_DISTRICT'] . "<br>";
echo $_SERVER['HTTP_X_COUNTRY_ASN'] . "<br>";
echo $_SERVER['HTTP_X_COUNTRY_AS'] . "<br>";
?>