<!-- #INCLUDE FILE = "conn.asp" -->
<!-- #INCLUDE FILE = "date.asp" -->
<!-- #INCLUDE FILE = "function.asp" -->
<!-- #INCLUDE FILE = "getips.asp" -->
<%
DIM Rs,strReferrer,strURL,pageUrl,fromip,sessionidStr,UserSessionId,CurSessionId
fromip=GetIp()
strReferrer=replaceBadChar(request.QueryString("ref"))
strURL=replaceBadChar(Request.ServerVariables("URL"))
pageUrl=replaceBadChar(trim(request.QueryString("page")))
If strURL = "" Then
	strURL = "(unknown)"
End If

UserSessionId=request.Cookies("TotStat")("UserId")
if(UserSessionId="" or isempty(UserSessionId)) then
	UserSessionId=Session("UserId")	
end if
CurSessionId=Session.SessionID
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'更新在线用户
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
if statonlineuser=true then
	if isempty(application(cache_prefix&"_onlineaborttime")) then
		application.Lock()
		application(cache_prefix&"_onlineaborttime")=DateAdd("s",onlineuseraborttime,now)
		application.UnLock()
	end if
	if(CDate(application(cache_prefix&"_onlineaborttime"))<now) then
		If databaseType = 1 Then	
			sql="delete from t_onlineuser where dateDiff('s',f_timevisite,'"&now()&"')>"&onlineusertime
		else
			sql="delete from t_onlineuser where dateDiff(s,f_timevisite,'"&now()&"')>"&onlineusertime
		end if
		conn.execute(sql)
		application(cache_prefix&"_onlineaborttime")=DateAdd("s",onlineuseraborttime,now)
	end if
	Set Rs=Server.CreateObject("ADODB.Recordset")
	Rs.open "select * from t_onlineuser where f_sessionid='"&CurSessionId&"'",conn,3,3
	if Rs.eof then
		Rs.addnew
		Rs("f_pageurl")=pageUrl
		Rs("f_from")=strReferrer
		Rs("f_timevisite")=strDateTime
		Rs("f_sessionid")=CurSessionId
		Rs("f_ip")=GetIp()
		Rs.update
	else
		Rs.MoveFirst
		Rs("f_pageurl")=pageUrl
		Rs("f_from")=strReferrer
		Rs("f_timevisite")=strDateTime
		Rs("f_sessionid")=CurSessionId
		Rs("f_ip")=GetIp()
		Rs.update
	end if
	Rs.close
	set Rs=nothing
end if
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
'更新当天统计
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
if(UserSessionId="" or len(UserSessionId)<1) then
dim sql_day
If databaseType = 1 Then
	sql_day = "SELECT * FROM t_daily WHERE f_date=#"&strDate&"#"
Else
	sql_day = "SELECT * FROM t_daily WHERE datediff(d,f_date,'"&strDate&"') =0"
End If

Set Rs=Server.CreateObject("ADODB.Recordset")
Rs.open sql_day,conn,3,3
if Rs.eof then
	Rs.addnew
	Rs("f_date")=strDate
	Rs("f_time")=strTime
	Rs("f_week")=iWeek
	Rs("f_hits")=1
	Rs.update
else
	Rs.MoveFirst
	Rs("f_hits")=Rs("f_hits")+1
	Rs.update
end if

Rs.close
set Rs=nothing
end if
''''''''''''''''''''''''''''''''''''''''''''''''''''''
'更新小时统计
''''''''''''''''''''''''''''''''''''''''''''''''''''''
if(UserSessionId="" or isempty(UserSessionId)) then
	dim sql_hour
	If databaseType = 1 Then
		sql_hour = "SELECT * FROM t_hour WHERE f_date=#"&strDate&"#"
	Else
		sql_hour = "SELECT * FROM t_hour WHERE datediff(d,f_date,'"&strDate&"') =0"
	End If
	
	Set Rs=Server.CreateObject("ADODB.Recordset")
	Rs.open sql_hour,conn,3,3
	if Rs.eof then
		Rs.addnew
		Rs("f_date")=strDate		
		Rs("f_h"&iHour)=1
		Rs.update
	else
		Rs.MoveFirst
		Rs("f_h"&iHour)=Rs("f_h"&iHour)+1
		Rs.update
	end if
	Rs.close
	set Rs=nothing	
end if
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
'更新PV总量
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
if isempty(application("statUpdateTime")) then
	application.Lock()
	application("statUpdateTime")=DateAdd("s",cachetime,now)
	application.UnLock()
end if
if(UserSessionId="" or isempty(UserSessionId)) then
	conn.execute("update t_global set f_totalip=f_totalip+1 where id=1")
end if
if(application("statNum")<>"") then
	application.Lock()
	application("statNum")=application("statNum")+1
	application.UnLock()
else
	set Rs=conn.execute("select f_totalpv from t_global")
	if not Rs.eof and not Rs.bof then
		application.Lock()
		application("statNum")=Rs(0)+1
		application.UnLock()
	end if
	Rs.close()
	set Rs=nothing
end if
if(CDate(application("statUpdateTime"))<now) then
	conn.execute("update t_global set f_totalpv="&application("statNum")&" where id=1")
	application.Lock()
	application("statUpdateTime")=DateAdd("s",cachetime,now)
	application.UnLock()
end if
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
'页面访问统计
''''''''''''''''''''''''''''''''''''''''''''''''''''''
if statpage=true then
dim sql_page
If databaseType = 1 Then
	sql_page = "SELECT * FROM t_page WHERE f_date=#"&strDate&"# and f_pageurl='"&pageUrl&"'"
Else
	sql_page = "SELECT * FROM t_page WHERE datediff(d,f_date,'"&strDate&"') =0  and f_pageurl='"&pageUrl&"'"
End If
Set Rs=Server.CreateObject("ADODB.Recordset")
Rs.open sql_page,conn,3,3
if Rs.eof then
	Rs.addnew
	Rs("f_pageurl")=pageUrl
	Rs("f_date")=strDate
	Rs("f_time")=strTime
	Rs("f_hits")=1
	Rs.update
else
	Rs.MoveFirst
	Rs("f_hits")=Rs("f_hits")+1
	Rs.update
end if
Rs.close
set Rs=nothing
end if
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
'来路访问统计
''''''''''''''''''''''''''''''''''''''''''''''''''''''
if statfrom=true then
	if(UserSessionId="" or isempty(UserSessionId)) then
	dim sql_from
	If databaseType = 1 Then
		sql_from = "SELECT * FROM t_from WHERE f_date=#"&strDate&"# and f_fromurl='"&strReferrer&"'"
	Else
		sql_from = "SELECT * FROM t_from WHERE datediff(d,f_date,'"&strDate&"') =0  and f_fromurl='"&strReferrer&"'"
	End If
	Set Rs=Server.CreateObject("ADODB.Recordset")
	Rs.open sql_from,conn,3,3
	if Rs.eof then
		Rs.addnew
		Rs("f_fromurl")=strReferrer
		Rs("f_date")=strDate	
		Rs("f_hits")=1
		Rs.update
	else
		Rs.MoveFirst
		Rs("f_hits")=Rs("f_hits")+1
		Rs.update
	end if
	Rs.close
	set Rs=nothing
	end if
end if
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
'更新关健字统计
''''''''''''''''''''''''''''''''''''''''''''''''''''''
if statkeyword=true then
	dim sql_key,strkeyword
	strkeyword=findKeystr(strReferrer)

	if(strkeyword<>"" and len(strkeyword)>0) then	
		If databaseType = 1 Then
			sql_key = "SELECT * FROM t_keyword WHERE f_date=#"&strDate&"# and f_keyword='"&strkeyword&"'"
		Else
			sql_key = "SELECT * FROM t_keyword WHERE datediff(d,f_date,'"&strDate&"') =0  and f_keyword='"&strkeyword&"'"
		End If	
		Set Rs=Server.CreateObject("ADODB.Recordset")
		Rs.open sql_key,conn,3,3
		if Rs.eof then
			Rs.addnew
			Rs("f_keyword")=strkeyword
			Rs("f_fromurl")=strReferrer
			Rs("f_date")=strDate	
			Rs("f_hits")=1
			Rs.update
		else
			Rs.MoveFirst
			Rs("f_hits")=Rs("f_hits")+1
			Rs.update
		end if	
		Rs.close
		set Rs=nothing
	end if
end if
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'IP统计
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
if statip=true then
	dim sql_fromip
	If databaseType = 1 Then
		sql_fromip = "SELECT * FROM t_ip WHERE f_date=#"&strDate&"# and f_ip='"&fromip&"'"
	Else
		sql_fromip = "SELECT * FROM t_ip WHERE datediff(d,f_date,'"&strDate&"') =0  and f_ip='"&fromip&"'"
	End If
	Set Rs=Server.CreateObject("ADODB.Recordset")
	Rs.open sql_fromip,conn,3,3
	if Rs.eof then
		Rs.addnew
		Rs("f_ip")=fromip
		Rs("f_date")=strDate	
		Rs("f_hits")=1
		Rs("f_address")=naddress(fromip)
		Rs.update
	else
		Rs.MoveFirst
		Rs("f_hits")=Rs("f_hits")+1
		Rs.update
	end if
	Rs.close
	set Rs=nothing
end if

'唯一用户设置
if(UserSessionId<>"") then
else	
	Response.Cookies("TotStat").Path ="/"
	Response.Cookies("TotStat").Expires = DateAdd("h", cTimeOffset+24, Now())
	Response.Cookies("TotStat")("UserId")=CurSessionId	
	Session("UserId")=CurSessionId
	Session.Timeout = 300
end if
conn.close
set conn=nothing
%>