Home arrow ASP arrow Page 2 - 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 - What's Involved?
(Page 2 of 4 )

To be able to understand what is going to be happening in this tutorial, you will need to have a basic grasp of ASP, but since this is an ASP tutorial, I will take for granted that you have some level of ASP knowledge. That, really, is all that is required, other than a database program; I used MS Access, on Windows XP. A simple text editor like notepad will suffice for the coding, that is all I have ever used, and I’ve done just fine.

Lets Begin!

Generally I like to begin my projects with a template or at least have each page already created, even if they are empty. So we will create our files now, in the same directory as your index.asp file (or equivalent) create an ASP page name referrals.asp, another ASP page named ref.asp, and a database named ref.mdb, we are now ready to begin.

First we will play about with our database and set names. Create a new table in the database and name it reftable, in this table we require columns named ‘Ref_ID’ (this should be your key) this should have the Data Type of AutoNumber, ‘URL’ with Data Type of Memo, and finally ‘Counter’ with Data Type of Number.

To begin with, we must set a control URL, so that we don’t end up with an error when trying to run the code for the first time (we will get an error stating that the database is empty, this can be avoided by adding a little extra code, but I find this method faster and easier). Simply enter “Control” in the URL column, and 0 in the counter column. The database is now set, we should not need to touch this again.

The Coding

Open the referrals.asp page we created earlier; this is the page that is actually displayed to the user (or Administrator, whoever you allow access) so make it look all pretty if you wish, but for now lets just concentrate on making it function properly.

In the body of your file, create a simple table like the one below that I use:

<TR BGCOLOR="#000000">
<TD> </TD>
<TD><FONT FACE="Arial, Helvetica" SIZE=2 COLOR="#FFFFFF"><B>Referring URL:</B></FONT></TD>
<TD> </TD>
<TD><FONT FACE="Arial, Helvetica" SIZE=2 COLOR="#FFFFFF"><B>Count:</B></FONT></TD>
<TD> </TD>

The above creates the table and appropriate headings; Referring URL and Count. Below we will display the details from the database, first we need to connect to the database...

This step is a little more advanced so we will make it a little easier by making it a DSNless connection (this saves us time required to make a DSN connection, then coding that to look at the correct database, then getting our code right to use this DSN connection...too much effort I believe, for the same end result!)

Any of you who have dabbled in ASP would have come across a database connection before, so we will fly through the connection, and not worry too much on what is involved.

We connect to the database using this DSNless connection:

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

We select all the rows ordered by the counter with the highest count first. (If you want the lowest count first, just replace DESC with ASC.)

SQL_query = "SELECT * FROM RefTable ORDER BY Counter DESC"
Set RS = MyConn.Execute(SQL_query)

Then we loop through every row and write it out:


The next little bit of code lets us get different background color for every other row, just to make it easier to see what count is with what URL, and also to make it more appealing to the eye:

  IF BGColor = "#F7F7E7" THEN
    BGColor = "#E7E7D6"
    BGColor = "#F7F7E7"
  Db_URL = RS("URL")

Now we write the details from the database, one row at a time to the table.

  <TR BGCOLOR="<%=BGColor%>">
  <TD> </TD>
  <TD><FONT FACE="Arial, Helvetica" SIZE=2><A HREF="<%=Db_URL%>"><%=Db_URL%></A></FONT></TD>
  <TD> </TD>
  <TD ALIGN=RIGHT><FONT FACE="Arial, Helvetica" SIZE=2><%=RS("Counter")%></FONT></TD>
  <TD> </TD>

The below should be fairly familiar to you, it is simply the end of a while loop, it’ll keep writing the above code until there are no more URLs left in the database.

Set RS = nothing
Set MyConn = nothing

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