Where'd They Come From?: Recording Referrals in ASP - How Do The URLs Get In The Table? (Page 3 of 4 )
This is the final step to creating our project; we must create a page that contains the code that will record where the user came from, i.e. the site that had the link that they clicked on. First we will find where the user came from using the request method, pretty simple really:
RefURL = Request.ServerVariables("HTTP_REFERER")
We connect to the database using the DSNless connection like in the last file:
Now we must check if the person was referred by a page or not (i.e. they could have come from a bookmark or just typed in the URL), so that we don’t end up with an error:
IF Len(RefURL) = 0 THEN
This asks if the length of the variable RefURL is 0, which would mean that there was no referrer and the user has typed in the address or used a bookmark.
In the case that the person was not referred by a page we set the variable InsertIntoID to 1:
InsertIntoID = 1 ELSE
If the person WAS referred by a page, then we check if the URL already is in the database, if it is then we will get the URL's Ref_ID value, if not we will set InsertIntoID = 0:
SQL_query = "SELECT Ref_ID FROM RefTable WHERE URL = '" & RefURL & "'" Set RS = MyConn.Execute(SQL_query) IF NOT RS.EOF THEN InsertIntoID = TRIM(RS("Ref_ID")) ELSE InsertIntoID = 0 END IF END IF
If InsertIntoID = 0 then we have ourselves a new URL:
If the InsertIntoID was not 0, then we must update the existing URL by adding 1 to the count:
SQL_query = "UPDATE RefTable SET Counter = Counter + 1 WHERE Ref_ID =" & InsertIntoID MyConn.Execute(SQL_query) END IF
MyConn.Close Set MyConn = nothing %>
How Do I Use It?
To put this to use, all you have to do is call the file ref.asp in your default page, or whatever other page you want to record referrals. This is done by using Server Side Includes:
<!--#INCLUDE FILE="Ref.asp"-->
This simply gets all the code from the file Ref.asp and places it in the file that you include it in, very handy for the head tag, or the top of your page, this way you only change one file and every file that calls upon the changed file will be altered.
To view the results of your referral counter, simply go to the file referrals.asp page, and it will be displayed for you.