TruckAndTools.Com developed and managed by Murray Wennerlund.

HTTPOnly Set-Cookie ASP Classic

Here I'll share with you my method of using HTTP cookies. This site uses HTTPOnly cookies so let's add a TESTCOOKIE to the mix for you to see how I do things.

Keys and Values I will be working with.
TESTCookie=Key1=Value1Key2=Value2Key3=Value3Key4=Value4ASP=CodeDev=Murray

ASP CLASSIC CODE:
Response.AddHeader "Set-Cookie", "TESTCookie=Key1=Value1Key2=Value2Key3=Value3Key4=Value4ASP=CodeDev=Murray; expires=Sat, 16 Nov 2024 03:24:56 GMT; domain=truckandtools.com; path=/; HTTPOnly"


HTTPOnly: Request your HTTP Cookie

We use the Request.ServerVariables("HTTP_COOKIE") in this example.

It looks like this:


HTTPOnly Set-Cookie Cookie Name

We now need to extract our cookie from the full HTTP Cookie: It will look like this:
TESTCookie=Key1=Value1Key2=Value2Key3=Value3Key4=Value4ASP=CodeDev=Murray
The ASP CLassic code to do this part.
strCookie0 = Request.ServerVariables("HTTP_COOKIE")
If InStr(strCookie0,"TESTCookie=") Then
chttpLen = Len("TESTCookie=")
j = InStrRev(strCookie0, "TESTCookie=")
if j > 0 Then
strCookieTEMP = Mid(strCookie0, j+chttpLen)
end if
j = InStr(strCookieTEMP, ";")
if j > 0 Then
strCookieTEMP = Left(strCookieTEMP, j-1)
End If
strCookie0 = strCookieTEMP
End If

HTTPOnly Set-Cookie Your Key and Values

Key1=Value1Key2=Value2Key3=Value3Key4=Value4ASP=CodeDev=Murray

Now we need to extract the Keys and Values from our HTTP Cookie. I like to use a simple function call. Really can't loop because of how the cookie is saved. We really need to check what's in the string.

Next up is how creative you want to be.

Known Value Length:
If InStr(strCookie0,"key1=") Then
j = InStrRev(strCookie0, "key1=")
if j > 0 Then
strCookieTEMP = Mid(strCookie0, j+5)
end if
j = 9
if j > 0 Then
strCookieTEMP = Left(strCookieTEMP, j-0)
End If
strCookie1 = strCookieTEMP
End If

Code with a known delimiter / tag. If InStr(strCookie0,"key1=") Then
j = InStrRev(strCookie0, "key1=")
if j > 0 Then
strCookieTEMP = Mid(strCookie0, j+5)
end if
j = InStr(strCookieTEMP, "key2=")
if j > 0 Then
strCookieTEMP = Left(strCookieTEMP, j-5)
End If
strCookie4 = strCookieTEMP
End If

You should be able to see the pattern of extracting your values from the HTTP Cookie.


HTTPOnly Set-Cookie Update Values and even add Keys

Let's add new values and update current ones as if we are really doing some work here.

Let's get all of our Values from our current keys. I wont cheat and just type them in. I will run the code as in the example below.

Let's put it all together now.

If InStr(strCookie0,"TESTCookie=") Then
chttpLen = Len("TESTCookie=")
j = InStrRev(strCookie0, "TESTCookie=")
if j > 0 Then
strCookieTEMP = Mid(strCookie0, j+chttpLen)
end if
j = InStr(strCookieTEMP, ";")
if j > 0 Then
strCookieTEMP = Left(strCookieTEMP, j-1)
End If
strCookie0 = strCookieTEMP
End If

Returns

Next we extract our keys and values. I'll show you the first key then update it and delete it.

strCookie0 = Request.ServerVariables("HTTP_COOKIE")
If InStr(strCookie0,"TESTCookie=") Then
chttpLen = Len("TESTCookie=")
j = InStrRev(strCookie0, "TESTCookie=")
if j > 0 Then
strCookieTEMP = Mid(strCookie0, j+chttpLen)
end if
j = InStr(strCookieTEMP, ";")
if j > 0 Then
strCookieTEMP = Left(strCookieTEMP, j-1)
End If
strCookie0 = strCookieTEMP
End If
If InStr(strCookie0,"Key1=") Then
j = InStrRev(strCookie0, "Key1=")
if j > 0 Then
strCookieTEMP = Mid(strCookie0, j+5)
end if
j = InStr(strCookieTEMP, "Key2=")
if j > 0 Then
strCookieTEMP = Left(strCookieTEMP, j-1)
End If
strCookie4 = strCookieTEMP
End If

Returns


HTTPOnly Set-Cookie Update Value Key1

Yes, key1 is different from Key1, please keep up.
Now, let's change the value of Key1. to Key1 is King of the Hill Key.

What would happen if we just updated Key1 without touching the other keys.

strGMTDateRFC22 = CookieServerUTC("d",Now(),6,"GMT")
Response.AddHeader "Set-Cookie", "TESTCookie=Key1=Key1 is King of the Hill KeyKey2=Value2Key3=Value3Key4=Value4ASP=CodeDev=Murray; expires="&strGMTDateRFC22&"; domain="&strHostByName&"; path=/; HTTPOnly"
strCookie0 = Request.ServerVariables("HTTP_COOKIE")
If InStr(strCookie0,"TESTCookie=") Then
chttpLen = Len("TESTCookie=")
j = InStrRev(strCookie0, "TESTCookie=")
if j > 0 Then
strCookieTEMP = Mid(strCookie0, j+chttpLen)
end if
j = InStr(strCookieTEMP, ";")
if j > 0 Then
strCookieTEMP = Left(strCookieTEMP, j-1)
End If
strCookie0 = strCookieTEMP
End If
If InStr(strCookie0,"Key1=") Then
j = InStrRev(strCookie0, "Key1=")
if j > 0 Then
strCookieTEMP = Mid(strCookie0, j+5)
end if
j = InStr(strCookieTEMP, "Key2=")
if j > 0 Then
strCookieTEMP = Left(strCookieTEMP, j-1)
End If
strCookie4 = strCookieTEMP
End If

Returns

Do we have any values and keys after our update otther than the key we updated? Let's look....

strCookie0 = Request.ServerVariables("HTTP_COOKIE")
If InStr(strCookie0,"TESTCookie=") Then
chttpLen = Len("TESTCookie=")
j = InStrRev(strCookie0, "TESTCookie=")
if j > 0 Then
strCookieTEMP = Mid(strCookie0, j+chttpLen)
end if
j = InStr(strCookieTEMP, ";")
if j > 0 Then
strCookieTEMP = Left(strCookieTEMP, j-1)
End If
strCookie0 = strCookieTEMP
End If

Returns

Can we add more keys with values? Yes,


So there you have it, your HTTPOnly Cookies all setup and ready to be used in your membership and eCommerce site. If you need help, ask me.

Results of a Perfect Project. From one generation to the next, we know if your project is designed perfectly you will have perfect results no matter if it's 1928, 1995 or 2024.