Fire, smoke blocking and air sealing between floors in a home.
- Fri. Dec. 20, 2024
- /
- 93
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=Thu, 9 Jan 2025 19:53:37 GMT; domain=truckandtools.com; path=/; HTTPOnly"
Request.ServerVariables("HTTP_COOKIE")
in this example. It looks like this:
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
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.
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
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 2025.