Log in

View Full Version : asp if cookie



rich1234
03-08-2008, 03:03 PM
Hi, would anyone know how to write a cookie based if clause to this.
I need a cookie to remember a user and if they click a link more than once it is not recorded.
this is the code bit that adds the record, I need it to say if peson has followed that link before dont add 1 to the database.






' If it's EOF then it's the first hit of the day and we need
' to add a new record o/w we simply update the existing hit
' count of the record by adding one to it.
If rsLinkTracker.EOF Then
rsLinkTracker.AddNew

rsLinkTracker.Fields("link_id").Value = iRedirectId
rsLinkTracker.Fields("hit_date").Value = Date()
rsLinkTracker.Fields("hit_count").Value = 1
Else
rsLinkTracker.Fields("hit_count").Value = _
rsLinkTracker.Fields("hit_count").Value + 1
End If


Any pointers as to how to write it and a cookie would be a great help
thanks richard

dsagrera
03-09-2008, 11:08 PM
If you add a Session Cookie after the INSERT, then the user could not click the link again... something like this:


If rsLinkTracker.EOF Then
If Session ("hit_count") = "" then
rsLinkTracker.AddNew
rsLinkTracker.Fields("link_id").Value = iRedirectId
rsLinkTracker.Fields("hit_date").Value = Date()
rsLinkTracker.Fields("hit_count").Value = 1
Session("hit_count") = "Y"
End If
Else
If Session ("hit_count") = "" then
rsLinkTracker.Fields("hit_count").Value = _
rsLinkTracker.Fields("hit_count").Value + 1
End If
End If

So If the user never clicked the link, and the hit count of that day is null, then the INSERT occurrs and the session cookie is saved. The session cookie timeout is 90 minutes, but you can change it from IIS or the Session Timeout property. The only way the user can "cheat" the code is waiting 90 minutes or closing every browser window each time he wants to record a hit. If you really mind someone would do that, then you can change stop him by saving a Cookie (not a Session one) and setting the Expires option to a date in the future, like 1 year from now. The only way the user can cheat it is by deleting the cookies every time he wants to cheat the counter. You can stop him by saving the IP of the last record together with the session cookies and the cookies.

dsagrera
03-09-2008, 11:10 PM
I forgot to save the Session Cookie after the UPDATE, sorry about that, here's the fixed code:


If rsLinkTracker.EOF Then
If Session ("hit_count") = "" then
rsLinkTracker.AddNew
rsLinkTracker.Fields("link_id").Value = iRedirectId
rsLinkTracker.Fields("hit_date").Value = Date()
rsLinkTracker.Fields("hit_count").Value = 1
Session("hit_count") = "Y"
End If
Else
If Session ("hit_count") = "" then
rsLinkTracker.Fields("hit_count").Value = _
rsLinkTracker.Fields("hit_count").Value + 1
Session("hit_count") = "Y"
End If
End If