<%@ 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 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?name=asp101 ' where asp101 is the friendly name from the database strName = Request.QueryString("name") ' Make it DB friendly just in case a ' got in somehow strName = Replace(strName, "'", "''") ' 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 "DBQ=" & Server.MapPath("database/linktracker.mdb") & ";Driver={Microsoft Access Driver (*.mdb)};" ' 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 id, location FROM tblLinkTracker " & _ "WHERE name='" & strName & "';" ' 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("id").Value ' Get location to send the user to strName = rsLinkTracker.Fields("location").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 strName %>