Posted Post

Posted People, Posted Life.

Download StatCounter Log as CSV Function

Today I introduce you a very powerful asp/vbscript function: kDownloadStatCounterLog

Input:

Output:

An CSV text string, you can use CSV2Array function change it to an array

What is StatCounter

A free and reliable invisible web tracker, highly configurable hit counter and real-time detailed web stats. Insert a simple piece of our code on your web page or blog and you will be able to analyze and monitor all the visitors to your website in real-time!

Function Code

Function kDownloadStatCounterLog(vUser, vPass, vProjectId)
	Dim oHttp, vStr, vCookie, vHost, vHost2
	kDownloadStatCounterLog = ""
	If Application("aCookieStatCounter-" & vUser & "-Host")<>"" Then
		vHost = Application("aCookieStatCounter-" & vUser & "-Host")
		vCookie = Application("aCookieStatCounter-" & vUser & "-PHPSESSID")
		vStr = kUrlGet("http://" & vHost & ".statcounter.com/project/standard/csv/download_log_file.php?project_id=" & vProjectId, "http://" & vHost & ".statcounter.com/project/standard/download_log.php?project_id=" & vProjectId, "PHPSESSID=" & vCookie)
		If inStr(LCase(vStr), "you are not logged in.") = 0 Then
			kDownloadStatCounterLog = vStr
			Exit Function
		Else
			Application("aCookieStatCounter-" & vUser & "-Host") = ""
			Application("aCookieStatCounter-" & vUser & "-PHPSESSID") = ""
		End If
	End If
	vStr = kUrlGet("http://www.statcounter.com/", "", "")
	vHost2 = kGetString(vStr, "<form action=""http://", ".statcounter.com/project/")
	'Response.Write "<pre>" & vHost2 & ":"
	If Not kCheckRegExp("^my\d+$", vHost2) Then vHost2 = "my2"
	'Response.Write vHost2 & "</pre>"
	vCookie = ""
	vStr = kUrlGet("http://" & vHost2 & ".statcounter.com/project/", "http://" & vHost2 & ".statcounter.com/project/", vCookie)
	'Response.Write "<pre>" & Server.HTMLEncode(vCookie) & "</pre>"
	vCookie = LCase(vCookie)
	vCookie = kGetString(vCookie, "set-cookie: phpsessid=", ";")
	If Len(vCookie) = 32 Then
		vStr = "PHPSESSID=" & vCookie & "&form_user=" & vUser & "&form_pass=" & vPass
		Set oHttp = CreateObject("WinHTTP.WinHTTPRequest.5.1")
		oHttp.setTimeouts 10000, 10000, 120000, 30000
		oHttp.open "POST", "http://" & vHost2 & ".statcounter.com/project/"
		oHttp.setRequestHeader "User-Agent", Request.serverVariables("HTTP_USER_AGENT")
		oHttp.setRequestHeader "Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"
		oHttp.setRequestHeader "Accept-Language", "en-us, en; q=0.8"
		oHttp.setRequestHeader "Connection", "Keep-Alive"
		oHttp.setRequestHeader "Pragma", "no-cache"
		oHTTP.setRequestHeader "Referer", "http://" & vHost2 & ".statcounter.com/project/"
		oHTTP.setRequestHeader "Cookie", "PHPSESSID=" & vCookie
		oHttp.setRequestHeader "Content-Length", Len(vStr)
		oHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
		oHttp.Option(6) = False
		oHttp.send vStr
		If oHttp.Status = 200 Then
			vStr = Byte2Str(oHttp.responseBody, "UTF-8")
			Set oHttp = Nothing
			If inStr(LCase(vStr), "invalid username or password")>0 Then
				Response.Write "<h1 class=""red"">ERROR :D ownloadStatCounterLog</h1><p class=""red"">Invalid Username or Password (" & vUser & ":" & vProjectId & ")</p>"
				Response.End
			Else
				Response.Write "<li><b class=""red"">ERROR :D ownloadStatCounterLog: Unknown</b></li>"
			End If
		ElseIf oHttp.Status = 302 Then
			vStr = LCase(oHttp.GetAllResponseHeaders())
			'Response.Write "<pre>" & Server.HTMLEncode(vStr) & "</pre>"
			vStr = Replace(vStr, vbLf, vbCr)
			vStr = Trim(kGetString(vStr, "location: http://", vbCr))
			If vStr<>"" Then
				vHost = kGetString(vStr, "", ".")
				vStr = "http://" & vStr
				'Response.Write "<pre>" & vStr & "</pre>"
				vCookie = "login=deleted"
				oHttp.open "GET", vStr, False
				oHttp.setRequestHeader "User-Agent", Request.serverVariables("HTTP_USER_AGENT")
				oHttp.setRequestHeader "Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"
				oHttp.setRequestHeader "Accept-Language", "en-us, en; q=0.8"
				oHttp.setRequestHeader "Connection", "Keep-Alive"
				oHttp.setRequestHeader "Pragma", "no-cache"
				oHTTP.setRequestHeader "Referer", "http://" & vHost2 & ".statcounter.com/"
				oHttp.Option(6) = False
				oHttp.send
				vCookie = LCase(oHttp.GetAllResponseHeaders())
				'Response.Write "<pre>" & Server.HTMLEncode(vCookie) & "</pre>"
				vCookie = kGetString(vCookie, "set-cookie: phpsessid=", ";")
				If Len(vCookie) = 32 Then
					vStr = kUrlGet("http://" & vHost & ".statcounter.com/project/standard/csv/download_log_file.php?project_id=" & vProjectId, "http://" & vHost & ".statcounter.com/project/standard/download_log.php?project_id=" & vProjectId, "PHPSESSID=" & vCookie)
					If inStr(LCase(vStr), "you are not logged in.") = 0 Then
						kDownloadStatCounterLog = vStr
						Application("aCookieStatCounter-" & vUser & "-Host") = vHost
						Application("aCookieStatCounter-" & vUser & "-PHPSESSID") = vCookie
					Else
						Response.Write "<li><b class=""red"">ERROR :D ownloadStatCounterLog: Cookie Lost (" & vHost & ")</b></li>"
					End If
				Else
					Response.Write "<li><b class=""red"">ERROR :D ownloadStatCounterLog: Cannot Get Cookie (" & vHost & ")</b></li>"
				End If
			Else
				Response.Write "<li><b class=""red"">ERROR :D ownloadStatCounterLog: Cannot Get Location</b></li>"
			End If
		Else
			Response.Write "<li><b class=""red"">ERROR :D ownloadStatCounterLog: " & oHttp.Status & "</b></li>"
		End If
		Set oHttp = Nothing
	Else
		Response.Write "<li><b class=""red"">ERROR :D ownloadStatCounterLog: Cannot Get Cookie</b></li>"
	End If
End Function

No comments yet »

Your comment

HTML-Tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>