Case Study: An Affiliate System With ASP - The ASP code (Page 3 of 7 )
TechBuy is driven by ASP 3.0, MSXML4, VB, COM+ and an SQL Server 2000 database. The affiliates program needed several different modules, each of which would be represented as an ASP function. These modules are:
Affiliate Signup: This function accepts the details of an affiliate and stores them in the database.
Affiliate Login: Once signed up, an affiliate can login to access his affiliate statistics as well as the page to generate links, banners, etc.
Link Generator: The TechBuy affiliate program allows its affiliates to choose from a wide range of both text and banner links for their site. Affiliates can link to individual products and categories, or they can choose from any of the 8 pre-made affiliate banners.
Click-Thru Reports: After affiliates have taken their tracking code, this function allows the affiliate to see the number of click-thrus from visitors to their site. These results can be viewed for last week, last month, or between particular dates.
Sales Reports: Allows affiliates to view how many sales have resulted from their visitorís click-thrus to the TechBuy web site.
Before we continue, it's important to know that all of the functionality for the TechBuy affiliates program is contained in one file called affiliates.asp. Each module is represented as a function and a select case statement is used in combination with a Request variable to determine which function should be called, like this:
where = Request("where")
select case where case "what" ShowWhat case "join" ShowJoin case "joinfinal" ProcessJoin case "login" ShowLogin case "processlogin" ProcessLogin case "link" if intAffId > 0 then ShowLinks else ShowJoin end if case "genlinks" if intAffId > 0 then CreateIDLinks else ShowJoin end if case "gencatlinks" if intAffId > 0 then CreateCatLinks else ShowJoin end if case "report" if intAffId > 0 then ShowReport else ShowJoin end if case "ctreport" if intAffId > 0 then ShowClickThruReport else ShowJoin end if case "sreport" if intAffId > 0 then ShowSalesReport else ShowJoin end if case else ShowStart end select
Let's now take a look at the code required to create some of the modules.
Affiliate Signup Before an affiliate can get the required linking code, he obviously needs an affiliate ID. The ShowJoin function displays a simple HTML form that gets the users contact details as well as the details of his site, etc.
This users details are collected and passed back to affiliates.asp. A hidden form variable called where is set to joinfinal and looks like this:
By sending this hidden form variable, affiliates.php will call the ProcessJoin function, which takes the affiliates details from the Request collection and spits them into the SQL Server 2000 database.
The affliates database for TechBuy contains three tables:
Affiliates: Contains the details of each affiliate including their email address and contact details.
AffiliatePayments: Tracks payments for each affiliate. Payments are made on a monthly basis.
AffilaiteHits: Tracks the click-thrus from the affiliates site to TechBuy. Each record includes the visitors IP address, affiliate ID and date.
The ProcessJoin function starts out with some simple value retrievals from the Request collection:
From here, a simple error checking routine makes sure that the user has completed the form correctly:
txtErr = ""
if txtFName = "" then txtErr = txtErr & "<li>You didn't enter your first name</li>"
if txtLName = "" then txtErr = txtErr & "<li>You didn't enter your last name</li>"
if txtPassword = "" then txtErr = txtErr & "<li>You didn't enter a password</li>"
if txtPassword <> txtVPassword AND txtPassword <> "" then txtErr = txtErr & "<li>Your passwords dont match</li>"
txtErr = txtErr & "</ul>"
if txtErr <> "<ul></ul>" then
If all fields are complete, then an insert query is executed against the database to add the affiliates details to the affiliates table.
If the INSERT query is performed successfully, then an email is composed and sent to the affiliate. This email contains their account details and the link to where they can login:
dim objMail set objMail = Server.CreateObject("CDONTS.NewMail")
'Generated from form values objMail.Body = strEmail objMail.Send
The email sent to the affiliate contains a link that points to http://www.techbuy.com.au/affiliates.asp?where=login. This causes the ShowLogin function to be called, which is what we will look at next.