Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hit Counter Issues - Cookies

Status
Not open for further replies.

iisman

Technical User
Mar 6, 2005
85
CA
Hello

I am using a hit counter on about 200 different pages. I began using it with onnly 10 pages.

It counts your first hit on a page, sets a cookie, then ignores subsequent hits on same page for a specified time if you have that cookie.

The problem is, with a cookie max of 20 per domain, that clearly leave me with a dilemma. the counter will work as desired for the first 20 pages the user hits, then after that, it will increment the count on every hit etc.

I am at a loss for a solution.

recap:

Need a counter script that counts first hit to a page, but not subsequent hits in a given 'session'.

If the user nav's to another page, it WILL increment that pages count, even though user has cookie for another page. (cookies currently named as the page url (strFilename = Request.ServerVariables("SCRIPT_NAME")
)

this must work for a large number of pages.

I also must be able to tell the server "its me" from any browser/computer, since I dont want my hits counted,

Any discussion would be appreciated. I do not necessarily want someone to write me a script, but rather a nudge in the right direction/logic to make this work.

Thanks in advance.
 
use a value in the same cookie

... ("cookie_name")("Value_name") = ...

on how not to count your visits you need to flag your visits, pick something unique;

Fixed IP;
a word set in the UA string (depends on the browser you use)
visit a particular page first.

are three that spring to mind


Chris.

Indifference will be the downfall of mankind, but who cares?
Woo Hoo! the cobblers kids get new shoes.
People Counting Systems

So long, and thanks for all the fish.
 
Thanks for the response.

I already have a page that im using with the old method of counting shown below.

it has been suggested that I use server variables, and am rusty in that area, and am unsure on how to impliment them for use with it. I already have this script as 'includes" on over 100 pages, and DO NOT want to have to change all of them.

Any help wtih this is appreciated.

Code:
<%= RetrieveAndIncrementCount() %>

<%
Function RetrieveAndIncrementCount()

	Const adOpenKeyset = 1
	Const adLockPessimistic = 2
	Const adCmdText = &H0001

	Dim strFilename
	Dim strSQL
	Dim rsCounter
	Dim iCount
	Dim cookieName
	
	Dim twohrs

	strFilename = Request.ServerVariables("SCRIPT_NAME")
	strSQL = "SELECT page_name, lasthit, hit_count FROM hit_count WHERE page_name='" & strFilename & "';"

	Set rsCounter = Server.CreateObject("ADODB.Recordset")
	
	rsCounter.Open strSQL, _
	"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("admin/robarspages.mdb") & ";", _
	adOpenKeyset, adLockPessimistic, adCmdText
	

	If rsCounter.EOF Then
	rsCounter.AddNew

	iCount = 0

	rsCounter.Fields("page_name").Value = strFilename
	Else
	rsCounter.MoveFirst

	iCount = rsCounter.Fields("hit_count").Value
	End If
	
	Set cookieName = Request.ServerVariables("SCRIPT_NAME")
	If Request.Cookies(""& cookiename &"")("recentvisitor") <> "yes" Then 
	iCount = iCount + 1
	
	Response.Cookies(""& cookiename &"")("recentvisitor") = "yes"
	rsCounter.Fields("hit_count").Value = iCount
	rsCounter.Fields("lasthit").Value = now()
	rsCounter.Update
	end if
	
		
	if Request.Cookies(""& cookiename &"")("recentvisitor") = "yes" then
	
	twohrs = DateAdd("h", 1, Now) 
	Response.Cookies(""& cookiename &"").Expires = twohrs 
	End If 

	rsCounter.Close
	Set rsCounter = Nothing

	RetrieveAndIncrementCount = iCount
				
End Function
%>

Thanks in advance
 
I already have this script as 'includes" on over 100 pages, and DO NOT want to have to change all of them.
Ok don't get this, if it's an include it only needs changing the once.

switch it around. make the cookie name "recent_visitor" and store the page name in the value, be aware that cookies are limited to a max size of 4k and only 20 are allowed per server.

I personally use a session value (session("page_name)) for my unique hit counter flag rather than cookies and it scales from a site with 6 pages up to a db driven site with a couple of thousand currently.

Chris.

Indifference will be the downfall of mankind, but who cares?
Woo Hoo! the cobblers kids get new shoes.
People Counting Systems

So long, and thanks for all the fish.
 
Chris,

THanks for the response.

I have been beginning to see that server variables are the way to go.

Is there an easy way to take that code as it is above, remove the portion that sets cookies, and 'rig it up' to use session variables?

I am not sure how to go about that, . THanks in advance for any help.
 
just replace any cookie setting code [response.cookies(...)] with session("name") and cookie retrieval code [request.cookies(...)] with variable = session("name")

leave out the expires code as well. Sessions expire when the browser is closed or on timeout (20 mins default) if the user leaves or doesn't make any request to the server.


Chris.

Indifference will be the downfall of mankind, but who cares?
Woo Hoo! the cobblers kids get new shoes.
People Counting Systems

So long, and thanks for all the fish.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top