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

iDay = DatePart("w", strDate)-1

UserSessionId=request.Cookies("TotStat")("UserId")
if(UserSessionId="" or isempty(UserSessionId)) then
	UserSessionId=Session("UserId")	
end if

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'更新在线用户
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
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='"&Session.SessionID&"'",conn,3,3
	if Rs.eof then
		Rs.addnew
		Rs("f_pageurl")=pageUrl
		Rs("f_from")=strReferrer
		Rs("f_timevisite")=strDateTime
		Rs("f_sessionid")=Session.SessionID
		Rs("f_ip")=GetIp()
		Rs.update
	else
		Rs.MoveFirst
		Rs("f_pageurl")=pageUrl
		Rs("f_from")=strReferrer
		Rs("f_timevisite")=strDateTime
		Rs("f_sessionid")=Session.SessionID
		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 CONVERT(varchar(10),f_date,121) = '"&strDate&"'"
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")=iDay
	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
	conn.execute("update t_hour set hits=hits+1 where id="&DATEPART("h", strTime))
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 CONVERT(varchar(10),f_date,121) = '"&strDate&"' 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 CONVERT(varchar(10),f_date,121) = '"&strDate&"' 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 CONVERT(varchar(10),f_date,121) = '"&strDate&"' 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 CONVERT(varchar(10),f_date,121) = '"&strDate&"' 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.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
	sessionidStr=Session.SessionID
	Response.Cookies("TotStat").Path ="/"
	Response.Cookies("TotStat").Expires = DateAdd("h", cTimeOffset+24, Now())
	Response.Cookies("TotStat")("UserId")=sessionidStr	
	Session("UserId")=sessionidStr
	Session.Timeout = 300
end if
conn.close
set conn=nothing
%>