asp 判断IP属于国内或国外跳转到不同页面
发布时间 | 2016/11/30 点击 | 次
其实从技术角度来判断国内IP还是国外IP是需要通过大量的IP库来判断的,像我们平时看见的再一个网站上一下就查询出来到底是属于哪个国家的而且能细化到那个地区这是需要先做一个IP库然后通过查询才能查到的,不能单纯的从IP段来实现跳转页面的功能。
我们在这里可以分析下如何通过IP段来实现跳转页面代码如下
ip=Request.ServerVariables("HTTP_X_FORWARDED_FOR")
if ip<>220.129.23 then
Response.Redirect("en/index.asp") ‘实现跳转功能
end if
上面这代码一般情况下可以使用,如果遇到IP段就必须使用字符查询函数
ip=Request.ServerVariables("HTTP_X_FORWARDED_FOR")
if InStr(content,"220.129") > 0 '字符查询国内IP段
Response.Redirect("en/index.asp")
end if
上面讲的代码都是在我们掌握了IP或者是IP段,IP库的前提使用的。如果我们没有这些判断国内和国外IP就非常难,小编就推荐你使用另外一种判断IP的代码
dim aa
aa=Request.ServerVariables("HTTP_ACCEPT_LANGUAGE") '判断字符集,
if instr(aa,"zh")=1 then ‘判断字符集是否包含zh字段,如果包含就跳转
response.Redirect "http://www.puyi.sh.cn/index.asp"
else
response.Redirect "http://puyi.sh.cn/index.asp"
end if
上面代码就是通过浏览器所用的字符集,一般国外肯定不会用中国的字符集的。
ASP获取客户端IP地址并判断是不是国内IP,从而实现不同的跳转:
方法一:
Function leleToStr(str)
Dim stream
Set stream=Server.CreateObject("ADODB.Stream")
With stream
.Type=1
.Mode=3
.Open
.Write str
.Position = 0
.Type = 2
.Charset = "gb2312"
leleToStr = .ReadText
.Close
End With
Set stream=Nothing
End Function
'获取IP所在地区
Function getIPAddress()
on error resume next
dim xmlhttp,url
url = "http://ip.qq.com"
set xmlhttp = server.CreateObject("Msxml2.XMLHTTP")
xmlhttp.open "get",url,false
xmlhttp.send
'开始截取字符
dim StartStr,EndStr,html
html = leleToStr(xmlhttp.responseBody)
StartStr = Instr(html,"所在地为:")
EndStr = Instr(html,"如果该IP")
html = mid(html,StartStr,EndStr-StartStr)
'再次获取
StartStr = Instr(html,"")
StartStr = StartStr + 6
EndStr = Instr(html,"")
html = mid(html,StartStr,EndStr-StartStr)
getIPAddress = html
if err then err.clear:html = ""
set xmlhttp = nothing
End Function
if Instr(getIPAddress(),"中国")>0 then Response.Redirect("index.php")
else Response.Redirect("e_index.php")
end if