我有个B2B站 http;//www.ynshangji.com ,怎么能让会员知道自己信息在搜索引擎上效果怎样,有没有人搜索什么词进来,这个问题很头疼,涉及到很多的正则表达式和解码,找遍了百度几百页都没找到,有的一些也是头尾不及的。通过研究了一些访问统计系统和高人的代码,我做出了这个东西,希望对为这个问题困扰的朋友能起到作用。 代码放进去公用文件(数据库连接文件),在页面任何地方调用最末尾那三个 response.write 出来的参数就行。
<%
url = Request.ServerVariables("Http_Referer")
url_0=url
if instr(url,"yahoo.") then
wds="yahoo"
end if
if instr(url,"google.") then
wds="google"
end if
if instr(url,"www.baidu.com") then
wds="baidu"
end if
if instr(url,"soso.") then
wds="soso"
end if
if instr(url,"sogou.") then
wds="sogou"
end if
if wds<>"" then
on error resume next
keyregEx = "(?:yahoo.+?[\?|&]p=|openfind.+?query=|google.+?q=|lycos.+?query=|onseek.+?keyword=|search\.tom.+?word=|search\.qq\.com.+?word=|zhongsou\.com.+?word=|search\.msn\.com.+?q=|yisou\.com.+?p=|sina.+?word=|sina.+?query=|sina.+?_searchkey=|sohu.+?word=|sohu.+?key_word=|sohu.+?query=|163.+?q=|baidu.+?wd=|baidu.+?kw=|baidu.+?word=|3721\.com.+?p=|Alltheweb.+?q=|soso.+?w=|115.+?q=|youdao.+?q=|sogou.+?query=|bing.+?q=|114.+?kw=)([^&]*)"
keyword = ""
Dim regEx, Match, Matches ' 建立变量。
Set regEx = New RegExp ' 建立正则表达式。
regEx.Pattern = keyregEx ' 设置模式。
regEx.IgnoreCase = False ' 设置是否区分大小写。
regEx.Global = True ' 设置全局替换。
Set Matches = regEx.Execute(url) ' 执行搜索。
if Matches(0).SubMatches(0) <>"" then
keyword = Matches(0).SubMatches(0)
end if
UrlCodeType keyword
Function UrlCodeType(ByVal str)
Dim sl, fstr
Dim isgb, isutf8, isuni
fstr = filterUrlChar(GetUrlCode(str))
sl = UBound(Split(fstr, "%"))
isuni = CBool(InStr(str, "\u")) Or CBool(InStr(str, "%u"))
If isuni Then
css = Unescape(str)
tcss = "Unicode : " & css
Else
On Error Resume Next
css = UTF2GB(str)
tcss = "UTF-8 : " & css
If Err Or Len(css)<>Int(sl/3) Then
Err.Clear()
css = URLDecode(str)
End If
End If
'Response.Write css
css_0=css
End Function
Function fsl(ByVal str)
If str>Int(str) Then str = Int(str) + 1
fsl = str
End Function
Function filterUrlChar(ByVal str)
Dim mstr
mstr = Replace(str, "%2f", "", 1 , -1, 1)
filterUrlChar = mstr
End Function
Function GetUrlCode(ByVal str)
Dim regEx, Match, Matches ' 建立变量。
Set regEx = New RegExp ' 建立正则表达式。
regEx.Pattern = "(%[0-9A-Fa-f]{2}){2,}" ' 设置模式。
regEx.IgnoreCase = False ' 设置是否区分大小写。
regEx.Global = True ' 设置全局替换。
Set Matches = regEx.Execute(str) ' 执行搜索。
If Matches.Count>0 Then
GetUrlCode = Matches(0).Value
Else
GetUrlCode = str
End If
End Function
Function URLDecode(enStr)
dim deStr,strSpecial
dim c,i,v
deStr=""
strSpecial="!""#$%&'()*+,.-_/:;<=>?@[\]^`{|}~%"
for i=1 to len(enStr)
c=Mid(enStr,i,1)
if c="%" then
v=eval("&h"+Mid(enStr,i+1,2))
if inStr(strSpecial,chr(v))>0 then
deStr=deStr&chr(v)
i=i+2
else
v=eval("&h"+ Mid(enStr,i+1,2) + Mid(enStr,i+4,2))
deStr=deStr & chr(v)
i=i+5
end if
else
if c="+" then
deStr=deStr&" "
else
deStr=deStr&c
end if
end if
next
URLDecode=deStr
End function
function UTF2GB(UTFStr)
for Dig=1 to len(UTFStr)
if mid(UTFStr,Dig,1)="%" then
if len(UTFStr) >= Dig+8 then
GBStr=GBStr & ConvChinese(mid(UTFStr,Dig,9))
Dig=Dig+8
else
GBStr=GBStr & mid(UTFStr,Dig,1)
end if
else
GBStr=GBStr & mid(UTFStr,Dig,1)
end if
next
UTF2GB=GBStr
end function
function ConvChinese(x)
A=split(mid(x,2),"%")
i=0
j=0
for i=0 to ubound(A)
A(i)=c16to2(A(i))
next
for i=0 to ubound(A)-1
DigS=instr(A(i),"0")
Unicode=""
for j=1 to DigS-1
if j=1 then
A(i)=right(A(i),len(A(i))-DigS)
Unicode=Unicode & A(i)
else
i=i+1
A(i)=right(A(i),len(A(i))-2)
Unicode=Unicode & A(i)
end if
next
if len(c2to16(Unicode))=4 then
ConvChinese=ConvChinese & chrw(int("&H" & c2to16(Unicode)))
else
ConvChinese=ConvChinese & chr(int("&H" & c2to16(Unicode)))
end if
next
end function
function c16to2(x)
i=0
for i=1 to len(trim(x))
tempstr= c10to2(cint(int("&h" & mid(x,i,1))))
do while len(tempstr)<4
tempstr="0" & tempstr
loop
c16to2=c16to2 & tempstr
next
end function
function c10to2(x)
mysign=sgn(x)
x=abs(x)
DigS=1
do
if x<2^DigS then
exit do
else
DigS=DigS+1
end if
loop
tempnum=x
i=0
for i=DigS to 1 step-1
if tempnum>=2^(i-1) then
tempnum=tempnum-2^(i-1)
c10to2=c10to2 & "1"
else
c10to2=c10to2 & "0"
end if
next
if mysign=-1 then c10to2="-" & c10to2
end function
function c2to16(x)
i=1
for i=1 to len(x) step 4
c2to16=c2to16 & hex(c2to10(mid(x,i,4)))
next
end function
function c2to10(x)
c2to10=0
if x="0" then exit function
i=0
for i= 0 to len(x) -1
if mid(x,len(x)-i,1)="1" then c2to10=c2to10+2^(i)
next
end function
end if
css_0=LCase(css_0)
if instr(css_0,"ynshangji.") then
wds=""
css_0=""
end if
if css_0="a" then
wds=""
css_0=""
end if
if css_0="qq" then
wds=""
css_0=""
end if
end if
response.write url_0 '搜索引擎的来源网址
response.write wds '搜索引擎的关键词
response.write css_0 '识别出是什么搜索引擎
%>
注:本文发自云南电子商务研究站(http://www.xiongmaotou.com),转载请附带本说明,谢谢