Home arrow ASP arrow Page 3 - Where'd They Come From?: Recording Referrals in ASP

Where'd They Come From?: Recording Referrals in ASP

In this article, Chris shows us how to find out where our visitors are coming from and store this information in our database.

Author Info:
By: Christopher Duell
Rating: 5 stars5 stars5 stars5 stars5 stars / 17
February 02, 2003
  1. · Where'd They Come From?: Recording Referrals in ASP
  2. · What's Involved?
  3. · How Do The URLs Get In The Table?
  4. · Conclusion

print this article

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:

DataSource = "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("ref.mdb") & ";"
Set MyConn = Server.CreateObject("ADODB.Connection")
MyConn.Open DataSource

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

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)
    InsertIntoID = TRIM(RS("Ref_ID"))
    InsertIntoID = 0

If InsertIntoID = 0 then we have ourselves a new URL:

IF InsertIntoID = 0 THEN

Insert this new URL with a count of 1:

  SQL_query = "INSERT INTO RefTable (URL, Counter) VALUES ('" & RefURL & "',1)"

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

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.
blog comments powered by Disqus

- Central Scoreboard with Flash and ASP
- Calorie Counter Using WAP and ASP
- Creating PGP-Encrypted E-Mails Using ASP
- Be My Guest in ASP
- Session Replacement in ASP
- Securing ASP Data Access Credentials Using t...
- The Not So Ordinary Address Book
- Adding and Displaying Data Easily via ASP an...
- Sending Email From a Form in ASP
- Adding Member Services in ASP
- Removing Unconfirmed Members
- Trapping HTTP 500.100 - Internal Server Error
- So Many Rows, So Little Time! - Case Study
- XDO: An XML Engine Class for Classic ASP
- Credit Card Fraud Prevention Using ASP and C...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials