1(818)262-8552
Or Email
contact@invertedsoftware.com

Avoid Cookie Stuffing in your affiliate program


By Gal Ratner

If you run or write affiliate programs, you might be aware of "Cookie Stuffing".
Affiliates that are loading your links into small images in their pages in order to send an affiliate code cookie to the client browser and credit themselves with sales that may or may not accrue sometime in the future by a customer that is unaware that an affiliate cookie for Amazon.com has been planted on their computer by a different site.
This is a major revenue loss for many online businesses.
If you are coding an ASP.NET affiliate program, here is a little C# code snippet to help detect Cookie Stuffing
The idea behind the code is when your linked page detects an affiliate code URL, it sends a small JavaScript to the client in order to check if the page is actually the top frame.
If the page is linked from an iframe or a frame, the script redirects the page to itself with an added parameter. If the parameter is detected, the affiliate cookie is not dropped.
Simple yet effective.



<%@ Application Language="C#" %>

<script runat="server">

    void Application_PreRequestHandlerExecute(object sender, EventArgs e)
    {
        string AffiliateCode = Request.QueryString["AffiliateCode"];

        if (!string.IsNullOrEmpty(AffiliateCode))
        {
            if (Session[AffiliateCode] == null)
            {
                CheckFrame(AffiliateCode);
                Session[AffiliateCode] = "Checked";
                return;
            }
            
            if (!Request.ServerVariables["QUERY_STRING"].EndsWith("Frame=True"))
                DropAffiliateCookie(Request.QueryString["AffiliateCode"]);
            Session[AffiliateCode] = null;
        }
    }

    public void CheckFrame(string AffiliateCode)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();

        sb.Append(@"<script language='javascript'>");
        sb.Append(@"if (top.location != location)");
        sb.Append(@"location.href = location + '&Frame=True';"); 
        sb.Append(@"<");
        sb.Append(@"/script>");
        
        Response.Write(sb.ToString());
    }

    public void DropAffiliateCookie(string AffiliateCode)
    {
        HttpCookie AffiliateCookie = new HttpCookie("AffiliateCode", AffiliateCode);
        AffiliateCookie.Expires = DateTime.Now.AddMonths(1);
        Response.Cookies.Add(AffiliateCookie);
    }
        
</script>


You can place the code in your global.asax page and get as creative as you like with the script.
If you have a more advanced JavaScript, please sent it to me and I will add it to this article.
Happy Coding!



About Us | Contact Us | Standards of Business Conduct | Employment
InvertedSoftware.com All rights reserved