link tracker with cookie to stop repeat clicking

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fran7
    New Member
    • Jul 2006
    • 229

    link tracker with cookie to stop repeat clicking

    Hi, I have this very nice link tracker script that works a treat.
    The only thing is it doesnt restrict repeat clicking.

    Is there a simple method to add a cookie feature to this so that people cannot vote twice. I prefer cookies over ip recording. Any pointers would be a great help.


    Code:
    %@ Language=VBScript %>
    <%
    Option Explicit              ' Never code without it!
    Response.Buffer = True       ' Make sure we can redirect later
    
    ' The constants I use in this script...
    ' pulled directly from adovbs.inc
    Const adOpenForwardOnly = 0
    Const adOpenDynamic = 2
    Const adLockReadOnly = 1
    Const adLockPessimistic = 2
    Const adCmdText = &H0001
    
    ' This needs to be ' for SQL Server and # for Access
    Const DATE_DELIMITER = "#"
    
    Dim strName                  ' Friendlyname of the redirect
    Dim strId                  ' id the redirect
    Dim cnnLinkTracker           ' ADO objects
    Dim rsLinkTracker
    Dim strSQL                   ' SQL command building area
    Dim iRedirectId              ' id of the redirect for logging process
    
    ' Get name of location to go to
    ' I simply call this name so to call the script it would look
    ' something like this:
    ' http://server/linktracker.asp?id=asp101
    ' where asp101 is the friendly id from the database
    strId = Request.QueryString("id")
    
    ' Make it DB friendly just in case a ' got in somehow
    strId = Replace(strId, "'", "''")
    
    ' Create a new connection
    Set cnnLinkTracker = Server.CreateObject("ADODB.Connection")
    
    ' Your connection string goes here!
    ' This one expects an Access database in the same place as this script
    cnnLinkTracker.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
    	Server.MapPath("/xxxxxxxxxxxx"), "", ""
    
    ' Create a recordset
    Set rsLinkTracker = Server.CreateObject("ADODB.Recordset")
    
    ' Get the record we're looking for by passing it the name
    ' we got from the QueryString.  I'm prebuilding the SQL command
    ' to make it easier to debug if we need to.
    strSQL = "SELECT PostCard, Headline  FROM tblGreet " & _
    	"WHERE PostCardID=" & strId & ";"
    ' Quick and dirty debugging when something goes wrong.
    'Response.Write strSQL
    
    ' Send the command to the database to get the appropriate records
    rsLinkTracker.Open strSQL, cnnLinkTracker, _
    	adOpenForwardOnly, adLockReadOnly, adCmdText
    
    ' If we got back any results then we know where to send them
    ' o/w I just send them to our home page for lack of a better
    ' place to send them.
    If Not rsLinkTracker.EOF Then
    	' Get redirect Id # from recordset
    	iRedirectId = rsLinkTracker.Fields("PostCard").Value
    
    	' Get location to send the user to
    	strName = rsLinkTracker.Fields("Headline").Value
    	
    	' We've got all the info we need so close our recordset
    	rsLinkTracker.Close
    	
    	' I now recycle the recordset for the logging process.
    	' Lots of people would argue with me about this, but I
    	' know I'm doing it and this is my code so if you don't
    	' like it feel free to change it, but I'm not going to!
    	
    	' Start logging process
    	
    	' Build out SQL String ahead of time.
    	' This should get us the record containing the information
    	' for the selected link for today's date if one exists.
    	strSQL = "SELECT link_id, hit_date, hit_count " & _
    		"FROM tblLinkTrackerLog " & _
    		"WHERE link_id = " & iRedirectId & " " & _
    		"AND hit_date = " & DATE_DELIMITER & Date() & DATE_DELIMITER
    	
    	' Standard debugging step when something goes wrong!
    	'Response.Write strSQL
    	
    	' Send the command.
    	rsLinkTracker.Open strSQL, cnnLinkTracker, _
    		adOpenDynamic, adLockPessimistic, adCmdText
    	
    	' 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
    
    	' Save changes to the data
    	rsLinkTracker.Update
    Else
    	' If no match send em to our home page
    	strName = "/"
    End If
    
    ' Close our recordset object
    rsLinkTracker.Close
    Set rsLinkTracker = Nothing
    
    ' Kill our connection
    cnnLinkTracker.Close
    Set cnnLinkTracker = Nothing
    
    ' Send them on their merry way using the location we got
    ' from the database!
    Response.Redirect "http://" & strName
    %>

    Thanks
    Richard
  • fran7
    New Member
    • Jul 2006
    • 229

    #2
    cookie if clause

    Hi, I got this cookie if clause done but it doesnt seem to work. I mean it doesnt stop someone clicking 100 times, it will record all 100. It is meant to, with the use of the cookie to stop after the first. Can anyone see why? Any help would be great.
    Thanks
    Richard


    Code:
     ' Send the command.
     rsLinkTracker.Open strSQL, cnnLinkTracker, _
      adOpenDynamic, adLockPessimistic, adCmdText
    	
    	'Establish cookie for unique hit Counting
    	
      Response.Cookies("Visited").Expires=date+1 
      strVisited = Request.Cookies("Visited")
     
     ' 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
    	
    	Response.Cookies("Visited") = "yes"
    	' Save changes to the data
       rsLinkTracker.Update
     Else
       If strVisited = "" Then
    		Response.Cookies("Visited") = "yes"
    		'Increment the visitor counter number by 1
    		rsLinkTracker.Fields("hit_count").Value + 1
    		' Save changes to the data
        rsLinkTracker.Update
    	End If	 
     End If
    Last edited by fran7; Mar 11 '08, 09:34 AM. Reason: error

    Comment

    • jhardman
      Recognized Expert Specialist
      • Jan 2007
      • 3405

      #3
      How do you check when you record votes whether they have the cookie?

      Jared

      Comment

      Working...