File: D:/web/common/geoip/geoip-checker.asp
<%@ Language="VBScript" %>
<%
Option Explicit
'=== Helper: VBScript-compatible IIf() substitute =====================
Function IIf(condition, truePart, falsePart)
If condition Then
IIf = truePart
Else
IIf = falsePart
End If
End Function
'--- CONFIG ------------------------------------------------------------
Const CONNSTR = "DSN=MySQL_common;"
Const HISTORY_LIMIT = 10
Const adCmdText = 1
Const adParamInput = 1
Const adDouble = 5
Const FLAG_PATH = "flags/"
Const GLOBE_ICON = "flags/globe.svg"
'-----------------------------------------------------------------------
Dim ip, ipInt, newRes, oldRes, hasInput, history
'=== Helper: get client IP ============================================
Function GetClientIP()
Dim ipAddr
ipAddr = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If ipAddr = "" Then ipAddr = Request.ServerVariables("REMOTE_ADDR")
If InStr(ipAddr, ",") > 0 Then ipAddr = Trim(Split(ipAddr, ",")(0))
GetClientIP = ipAddr
End Function
'=== IPv4 dotted-decimal → integer ====================================
Function IPv4ToInt(ByVal ipAddr)
Dim parts
IPv4ToInt = -1
If Len(Trim(ipAddr)) = 0 Then Exit Function
parts = Split(ipAddr, ".")
If UBound(parts) = 3 Then
On Error Resume Next
IPv4ToInt = (CDbl(parts(0)) * 16777216) + _
(CDbl(parts(1)) * 65536) + _
(CDbl(parts(2)) * 256) + _
CDbl(parts(3))
If Err.Number <> 0 Then
Err.Clear
IPv4ToInt = -1
End If
On Error GoTo 0
End If
End Function
'=== Safe parameterized lookup ========================================
Function LookupGeoIP(ByVal ipNum, ByVal tableName)
Dim conn, cmd, rs, sql, result
result = Array(False, "", "")
If ipNum < 0 Then
LookupGeoIP = result
Exit Function
End If
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open CONNSTR
sql = "SELECT CountryISOCode, CountryName FROM " & tableName & _
" WHERE IPStartInteger <= ? AND IPEndInteger >= ? LIMIT 1"
Set cmd = Server.CreateObject("ADODB.Command")
Set cmd.ActiveConnection = conn
cmd.CommandType = adCmdText
cmd.CommandText = sql
cmd.Parameters.Append cmd.CreateParameter("@p1", adDouble, adParamInput, , CDbl(ipNum))
cmd.Parameters.Append cmd.CreateParameter("@p2", adDouble, adParamInput, , CDbl(ipNum))
Set rs = cmd.Execute()
If Not rs.EOF Then
result(0) = True
result(1) = rs("CountryISOCode") & ""
result(2) = rs("CountryName") & ""
End If
rs.Close : conn.Close
Set rs = Nothing : Set cmd = Nothing : Set conn = Nothing
LookupGeoIP = result
End Function
'=== Simple flag path resolver ========================================
Function GetFlagSrc(ByVal iso)
If iso = "" Then
GetFlagSrc = GLOBE_ICON
Else
GetFlagSrc = FLAG_PATH & LCase(iso) & ".svg"
End If
End Function
'=== History management ===============================================
If IsEmpty(Session("GeoIPHistory")) Then Session("GeoIPHistory") = Array()
history = Session("GeoIPHistory")
Sub AddToHistory(ByVal newIP)
Dim i, exists, total, startIdx, trimmed(), j
exists = False
If IsArray(history) Then
For i = 0 To UBound(history)
If history(i) = newIP Then exists = True : Exit For
Next
End If
If Not exists Then
If Not IsArray(history) Or (IsArray(history) And UBound(history) < 0) Then
ReDim history(0) : history(0) = newIP
Else
ReDim Preserve history(UBound(history) + 1)
history(UBound(history)) = newIP
End If
End If
total = IIf(IsArray(history), UBound(history) + 1, 0)
If total > HISTORY_LIMIT Then
startIdx = total - HISTORY_LIMIT
ReDim trimmed(HISTORY_LIMIT - 1)
For j = 0 To HISTORY_LIMIT - 1
trimmed(j) = history(startIdx + j)
Next
history = trimmed
End If
End Sub
'=== Log each check (with ISO + name) =================================
Sub LogGeoIPCheck(ByVal ipAddr, ByVal newRes, ByVal oldRes)
Dim conn, cmd, sql
sql = "INSERT INTO geoip_check_log " & _
"(DateTimeChecked, IPAddress, CountryISO, Country, Found, CountryOldISO, CountryOld, FoundOld) " & _
"VALUES (NOW(), ?, ?, ?, ?, ?, ?, ?)"
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open CONNSTR
Set cmd = Server.CreateObject("ADODB.Command")
Set cmd.ActiveConnection = conn
cmd.CommandType = adCmdText
cmd.CommandText = sql
cmd.Parameters.Append cmd.CreateParameter("@p1", 200, adParamInput, 45, ipAddr)
cmd.Parameters.Append cmd.CreateParameter("@p2", 200, adParamInput, 2, newRes(1)) ' Country ISO (new)
cmd.Parameters.Append cmd.CreateParameter("@p3", 200, adParamInput, 100, newRes(2)) ' Country name (new)
cmd.Parameters.Append cmd.CreateParameter("@p4", 2, adParamInput, , IIf(newRes(0),1,0))
cmd.Parameters.Append cmd.CreateParameter("@p5", 200, adParamInput, 2, oldRes(1)) ' Country ISO (old)
cmd.Parameters.Append cmd.CreateParameter("@p6", 200, adParamInput, 100, oldRes(2)) ' Country name (old)
cmd.Parameters.Append cmd.CreateParameter("@p7", 2, adParamInput, , IIf(oldRes(0),1,0))
cmd.Execute
conn.Close
Set cmd = Nothing
Set conn = Nothing
End Sub
'=== Main logic =======================================================
ip = Trim(Request("ip"))
If ip = "" Then ip = GetClientIP()
hasInput = (Request("submitted") <> "")
Dim i
If hasInput Then
ipInt = IPv4ToInt(ip)
If ipInt >= 0 Then
AddToHistory ip
Session("GeoIPHistory") = history
newRes = LookupGeoIP(ipInt, "geo_ip_country")
oldRes = LookupGeoIP(ipInt, "geo_ip_country_v1")
Call LogGeoIPCheck(ip, newRes, oldRes)
Else
newRes = Array(False, "", "")
oldRes = Array(False, "", "")
End If
End If
%>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>GeoIP Checker</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.4/css/bulma.min.css">
<style>
.flag{width:24px;height:18px;vertical-align:middle;margin-right:8px;border:1px solid #ddd}
.result-table{margin-top:1em}
.note{color:#666;font-size:0.9em;margin-top:0.5em}
</style>
<script>
function pickIP(sel){
if(sel.value){
document.getElementsByName('ip')[0].value = sel.value;
document.forms[0].submit();
}
}
</script>
</head>
<body class="section">
<div class="container">
<h1 class="title is-3">GeoIP Checker</h1>
<form method="get" action="geoip-checker.asp" class="box">
<div class="field is-grouped">
<p class="control is-expanded">
<input class="input" type="text" name="ip" value="<%=Server.HTMLEncode(ip)%>" placeholder="Enter IP address">
</p>
<p class="control">
<input type="hidden" name="submitted" value="1">
<button type="submit" class="button is-primary">Check</button>
</p>
</div>
<% If IsArray(history) And UBound(history) >= 0 Then %>
<div class="field">
<label class="label">Recent IPs</label>
<div class="control">
<div class="select">
<select onchange="pickIP(this)">
<option value="">(select previous IP)</option>
<% For i = UBound(history) To 0 Step -1
Response.Write("<option value='" & Server.HTMLEncode(history(i)) & "'>" & Server.HTMLEncode(history(i)) & "</option>")
Next
%>
</select>
</div>
</div>
<p class="note">Selecting a recent IP will auto-submit.</p>
</div>
<% End If %>
</form>
<% If hasInput Then
If ipInt < 0 Then %>
<article class="message is-danger"><div class="message-body"><strong>Invalid IP format.</strong></div></article>
<% Else %>
<div class="content">
<p><strong>IP:</strong> <%=Server.HTMLEncode(ip)%>
<strong>Integer:</strong> <%=ipInt%></p>
</div>
<table class="table is-striped is-fullwidth result-table">
<thead>
<tr><th>Version</th><th>Found</th><th>Country ISO</th><th>Country</th></tr>
</thead>
<tbody>
<tr>
<td>New (geo_ip_country)</td>
<td class="<%=IIf(newRes(0),"has-text-success","has-text-danger")%>"><%=IIf(newRes(0),"Yes","No")%></td>
<td><%=Server.HTMLEncode(newRes(1))%></td>
<td>
<img class="flag" src="<%=GetFlagSrc(newRes(1))%>" alt="<%=Server.HTMLEncode(newRes(1))%> flag">
<%=Server.HTMLEncode(newRes(2))%>
</td>
</tr>
<tr>
<td>Old (geo_ip_country_v1)</td>
<td class="<%=IIf(oldRes(0),"has-text-success","has-text-danger")%>"><%=IIf(oldRes(0),"Yes","No")%></td>
<td><%=Server.HTMLEncode(oldRes(1))%></td>
<td>
<img class="flag" src="<%=GetFlagSrc(oldRes(1))%>" alt="<%=Server.HTMLEncode(oldRes(1))%> flag">
<%=Server.HTMLEncode(oldRes(2))%>
</td>
</tr>
</tbody>
</table>
<% If newRes(1) <> oldRes(1) Or newRes(2) <> oldRes(2) Then %>
<article class="message is-warning"><div class="message-body"><strong>⚠ Difference detected between old and new results.</strong></div></article>
<% ElseIf newRes(0) And oldRes(0) Then %>
<article class="message is-success"><div class="message-body"><strong>✅ Both versions agree.</strong></div></article>
<% Else %>
<article class="message"><div class="message-body"><strong>No match found in either table.</strong></div></article>
<% End If
End If
End If %>
<p style="margin-top:1em">
<a href="geoip-check-log.asp" class="button is-light">📜 View Log History</a>
</p>
</div>
</body>
</html>