Search code examples
sql-serverasp-classic

Check geolocation database using Classic ASP


I have an IP address range database for determining country, but need a function written in Classic ASP that will find the range that the user's IP address fits into.

For example the database rows are like:

102.23.25.0   |  102.24.0.0   |  US

So if the IP address is 102.23.25.203 how can I find a match?


Solution

  • Using the MaxMind CSV database I imported it into MS-SQL. Then wrote a simple Classic ASP routine to get the Country code.

    strCheckIPnumber = Request.ServerVariables("REMOTE_ADDR")
    
    if strCheckIPnumber <> "" then
        strGeoArray = Split(strCheckIPnumber, ".")
        For i=0 to UBound(strGeoArray)
        Next            
        strGeo1 = strGeoArray(0)
        strGeo2 = strGeoArray(1)
        strGeo3 = strGeoArray(2)            
        strGeo4 = strGeoArray(3)
    
        strGeoNumber = strGeo1 * 16777216 + strGeo2 * 65536 + strGeo3 * 256 + strGeo4
    
        'response.write"GeoNumber = " & strGeoNumber & "<br>"
    
        SQLGeo = "SELECT Country FROM GeoLocation Where GeoLocation.StartNum <= '" & strGeoNumber & "' and GeoLocation.EndNum >= '" & strGeoNumber & "' "
        Set rsGeo = dbConnection.Execute(SQLGeo)    
        if NOT rsGeo.EOF then       
            strGeoCountry = rsGeo("Country").value
        end if
        rsGeo.Close
        Set rsGeo = Nothing
    
        response.write"GeoCountry = " & strGeoCountry & "<br>"
    
    end if