<!-- #include file="conn.asp"-->
<%
Response.Expires = 0

If Application.Contents(CacheName & "_isStart")=0 then
Call OutPut
else
Call StartCount
Call OutPut
End If

set hx=nothing

Sub StartCount
ConnectionDatabase

Dim ip,vpage
' IP
ip=hx.CheckStr(Request.ServerVariables("Remote_Addr"),15)
' 被访问页面
vpage=hx.checkstr(Request.ServerVariables("HTTP_REFERER"),250)
if right(vpage,1)="/" then vpage=left(vpage,len(vpage)-1)

if vpage="" then
	Exit Sub
end if

if hx.IsVisited(ip) then
	CountPage "PageView",vpage
else
	CountPage "Visitor",vpage
		
Dim Referer,RefSite,User_Agent,Client_id,width,vhour
Dim rs,sql
Dim q

' 来路
Referer=hx.CheckStr(Request("referer"),250)
'if right(Referer,1)="/" then Referer=left(Referer,len(Referer)-1)
If Referer<>"" Then
	RefSite=Mid(Referer,8)
	RefSite="http://"&Mid(RefSite,1,instr(RefSite,"/"))
	RefSite=hx.CheckStr(RefSite,100)
else
	Referer=0
	RefSite=0	
end if

' 客户端软件
User_Agent=hx.checkstr(Request.ServerVariables("HTTP_USER_AGENT"),250)

' 操作系统
if instr(User_Agent,"Win 9x 4.90") then
	Client_id=107
elseif instr(User_Agent,"Windows 98") then
	Client_id=101
elseif instr(User_Agent,"Windows NT 5.1") then
	Client_id=102
elseif instr(User_Agent,"Windows NT 5.0") then
	Client_id=104
elseif instr(User_Agent,"Windows NT 5.2") then
	Client_id=105
elseif instr(User_Agent,"Windows NT") then
	Client_id=103
elseif instr(User_Agent,"unix")  or instr(User_Agent,"Linux") or instr(User_Agent,"SunOS") or instr(User_Agent,"BSD") then
	Client_id=106
else
	Client_id=108
end if

' 浏览器
if instr(User_Agent,"MSIE 6") then
	Client_id=Client_id & "," & 201 
elseif instr(User_Agent,"MSIE 5") then
	Client_id=Client_id & "," & 202
elseif instr(User_Agent,"MSIE 4") then
	Client_id=Client_id & "," & 203
elseif instr(User_Agent,"Netscape") then
	Client_id=Client_id & "," & 204
elseif instr(User_Agent,"Opera") then
	Client_id=Client_id & "," & 206
else
	Client_id=Client_id & "," & 207
end if


'屏幕宽度
width=Request("screenwidth")

if width="640" then
	Client_id=Client_id & "," & 301 
elseif width="800" then	
	Client_id=Client_id & "," & 302
elseif width="1024" then	
	Client_id=Client_id & "," & 303
elseif width="1152" then	
	Client_id=Client_id & "," & 304
elseif width="1280" then	
	Client_id=Client_id & "," & 305
elseif width="1600" then	
	Client_id=Client_id & "," & 306
else	
	Client_id=Client_id & "," & 307
end if

Client_id="(" & Client_id & ")"

vHour=hour(now())

	'将记录加入到Visitor表中
	
	hx.execute("update CC_V set Ip='"&ip&"',Referer='"&Referer&"',Page='"&vpage&"',User_Agent='"&User_Agent&"',Dateandtime="&SqlNowString&" where id=(select top 1 id from CC_V order by Dateandtime asc,id asc)")
	'处理Client信息
	hx.execute("update CC_C set CC=CC+1 where id in "& Client_id)

	'处理Referer信息
		Set rs = Server.CreateObject("ADODB.Recordset")
		sql="select CR,Referer,Q,RefSite,vtime from CC_R where Referer='"&Referer&"'"
		rs.open sql,conn,1,2
		if rs.eof then	
			rs.addnew
			rs(1)=Referer
			q=hx.GetSearchKeyword(Referer)	
			if q<>"" then
				rs(2)=q
			end if
			rs(3)=RefSite
			rs.update
		else
			rs(0)=rs(0)+1
			rs(4)=now()
			rs.update
		end if
		rs.close

	'处理日统计信息
		If IsSqlDataBase = 1 Then
		hx.Execute("update CC_D set Visitor=Visitor+1 where CDate='"&hx.vDate&"'")
		else
		hx.Execute("update CC_D set Visitor=Visitor+1 where CDate=date()")		
		end if
	'处理小时信息
		hx.execute "update CC_H set CTH=CTH+1,CCH=CCH+1,vtime="&SqlNowString&" where Hour=" & hx.vHour
	set rs=nothing
end if
End Sub


Sub OutPut	
	dim outstr		
	'根据要求输出
	dim style,scount
	style=Request("style")
	select case style
	case "icon"   '显示小图标
		outstr= "<a href='"&hx.baseurl&"show.asp' target='_blank'><img src='"&hx.baseurl&"cc_icon.gif' border='0' alt='"&WebName&" CuteCounter'></a>"
	case "t1"   '文本显示总访问量
		scount=hx.execute("select Sum(Visitor) from CC_D")(0)
		outstr= "<a href='"&hx.baseurl&"show.asp' target='_blank' alt='"&WebName&" CuteCounter'>"&scount&"</a>"
	case "t2"   '文本显示总浏览量
		scount=hx.execute("select Sum(PageView) from CC_D")(0)
		outstr= "<a href='"&hx.baseurl&"show.asp' target='_blank' alt='"&WebName&" CuteCounter'>"&scount&"</a>"
	case "p1"   '图片显示总访问量
		scount=hx.execute("select Sum(Visitor) from CC_D")(0)
		outstr= "<a href='"&hx.baseurl&"show.asp' target='_blank' alt='"&WebName&" CuteCounter'>"&ShowPic(scount)&"</a>"
	case "p2"   '图片显示总浏览量
		scount=hx.execute("select Sum(PageView) from CC_D")(0)
		outstr= "<a href='"&hx.baseurl&"show.asp' target='_blank' alt='"&WebName&" CuteCounter'>"&ShowPic(scount)&"</a>"
	case "no"	'什么都不显示
		outstr= ""
	case else	'默认显示小图标
		outstr= "<a href='"&hx.baseurl&"show.asp' target='_blank'><img src='"&hx.baseurl&"cc_icon.gif' border='0' alt='"&WebName&" CuteCounter'></a>"
	end select
	response.write "document.write("& chr(34) & outstr & chr(34) &")"
End Sub

Function ShowPic(scount)
	Dim i
	scount = CStr(scount)
	For i = 1 to Len(scount)
		ShowPic = ShowPic & "<IMG SRC="&hx.baseurl&"images/1/" & Mid(scount, i, 1) & ".gif border=0>"
	Next
End Function

'处理vpage信息
Sub CountPage(str,vpage)
	dim rs,sql
	sql="select "&str&",Page,vtime from CC_P where Page='"&vpage&"'"
	set rs=hx.GetRs(sql,1,2)
		if rs.eof then
			rs.addnew
			rs(0)=1
			rs(1)=vpage
		else
			rs(0)=rs(0) + 1
			rs(2)=now()
		end if
		rs.update
		rs.close
	set rs=nothing
End Sub
%>