In the first part of this series I outlined the idea behind the cookie based affiliate or discount code system being developed. In this post I’ll detail how we are using it and give some examples for consuming the code in a few programming languages.
We’re starting with this javascript file to initiate a code cookie:
Examples of it’s installation and use can be found in Part I.
In our scenario, we have a landing page where a customer will navigate in order to receive some sort of incentive. When the landing page is loaded, a discount code related to the campaign is stored as a cookie using the SaveCode(‘Code’); method in our .js file. Now that the cookie has been stored, it can be retrieved later on when it comes time to process some type of transaction, in this case, signing up for a trial account for our service.
After the customer visits the landing page where the discount is applied, they hopefully continue on to sign up for a trial account. In the back end of our application we have created a Discount table that stores various discount codes and their details that can be associated with an account’s subscription. When the customer creates this new subscription, the application checks to see if any discount codes have been applied by way of the cookie object we set previously. If no cookie is present, the customer is processed as a normal, non-discount subscriber. If a cookie is present, the code is referenced against the Discount table in the database to pull up the details of that discount and apply them.
To cookie can be retrieved in 2 ways:
- Read the cookie back using client-side code (JavaScript, VBScript, etc.)
- Read the cookie back using server-side code (C#, PHP, Java, etc.)
You can easily retrieve the cookie on the client side by calling the GetCode(); function in our JavaScript file, this will return the stored value as a string.
Retrieving the cookie value on the server side depends on which language and how you are passing the value but the principal is generally the same. Here are a few examples of cookie retrieval:
ASP.NET C#
string discountCode = "";
HttpCookie cookie = Request.Cookies["MyCookieName"];
if (cookie != null)
{
discountCode = cookie.Value;
}
PHP
<?php
$discountCode = "";
if (isset($_COOKIE["MyCookieName"]))
{
$discountCode = $_COOKIE["MyCookieName"];
}
?>
Java
Cookie[] cookies = request.getCookies();
string discountCode = "";
for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++)
{
Cookie cookie1 = cookies[loopIndex];
if (cookie1.getName().equals("MyCookieName"))
{
discountCode = cookie1.getValue();
}
}
Once you’ve got your discount code value, it’s up to your system requirements how you use it.
Matthew Mombrea
Latest posts by Matthew Mombrea (see all)
- Hacked? Here’s How To Remove The Dreaded Google Malware Warning - March 21, 2013
- How to sort alphanumeric values with jQuery and C# - October 25, 2012
- Global AJAX error handling with jQuery - September 7, 2012
- How to create multiple 301 redirect urls in ASP.NET MVC - March 29, 2012
- Internet Explorer Aborting AJAX Requests : FIXED - March 19, 2012

Recent Comments
“#5: The only thing it says to me is "write an encrypted file." #4: "Let's order takeout" of course! #3: I think the upper left symbol is a scale…”
“Thanks it worked..…”
“Tiffany, thanks for sharing this. I am working on the cover image of my page right now and i will try to make use of your notes.…”