65.9K
CodeProject 正在变化。 阅读更多。
Home

GeoIP – 您的互联网访客来自哪里?

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.82/5 (17投票s)

2002 年 11 月 29 日

1分钟阅读

viewsIcon

281481

downloadIcon

3256

您的互联网访客来自哪里?

引言

使用 GeoIP,你可以检测你的访客来自哪里。你可以利用这些信息来提供个性化信息(见下图),减少信用卡欺诈,分析 Web 服务器日志(我用它来做 SNIFF),定位横幅广告,....(参见 MaxMind 的解决方案 - http://www.maxmind.com/


点击图片进行测试

注意

如果你可以在你的 Web 服务器上安装一个组件,你应该从 MaxMind 下载 GeoIP 组件。由于我的提供商不允许我安装组件,所以我编写了 GeoIP.asp。

先决条件

更新于 2003 年 9 月 1 日,新的 URL)你需要 GeoIP 数据库 - http://www.maxmind.com/app/standard,并且你需要 ADO 2.5 或更高版本,因为 GeoIP.asp 使用 ADO Stream 对象来读取 GeoIP 数据库。

安装

  1. 从 MaxMind 下载 GeoIP.dat 数据库
    http://www.maxmind.com/app/standard
  2. 将 GeoIP.asp、example.asp 和 GeoIP.dat 放在你的 Web 服务器上的一个目录中。
  3. 通过访问 example.asp 页面来测试安装
    http://yourserver/path/example.asp

方法和属性

GeoIP.asp 暴露了这些公共方法和属性

属性

  • GeoIPDataBase - 设置 GeoIP.dat 的路径和文件名
  • ErrNum - 返回错误编号(如果 <> 0,则表示有错误)

方法

  • lookupCountryName - 查找 IP 的国家/地区名称
    • 示例:strCountryName = oGeoIP.lookupCountryName(strIP)
  • lookupCountryCode - 查找 IP 的国家/地区代码
    • 示例:strCountryCode = oGeoIP.lookupCountryCode(strIP)

使用代码

使用这段代码非常简单

<!--#include file="GeoIP.asp"-->
<%
Dim oGeoIP,strErrMsg
Dim strIP,strCountryName,strCountryCode

Set oGeoIP = New CountryLookup
oGeoIP.GeoIPDataBase = Server.MapPath("GeoIP.dat")
If oGeoIP.ErrNum(strErrMsg) <> 0 Then
	Response.Write(strErrMsg)
Else
	strIP = request.ServerVariables("REMOTE_ADDR")
	strCountryName = oGeoIP.lookupCountryName(strIP)
	strCountryCode = oGeoIP.lookupCountryCode(strIP)
End If
Set oGeoIP = Nothing
%>

祝你编码愉快,Per

© . All rights reserved.