by   January 08 2015   
The ASP Classic Cookie Code is some of the easiest to overlook when building your ASP ... Wait a minute, you're not still building in ASP Classic are you? Well, let's start with the Cookie Code that will be used over and over within your ASP Classic website. Be sure to offer different visitors different settings. I disapprove of cookies that are set for more than 14 days and really don't return to sites with unlimited time on cookies. Cookies are to enhance the interaction with your website first!

The ASP Classic Cookie Code

If you're here reading this it means you haven't search articles published back in 2002. 

Some of the best code was created between 1999 and 2002 so if you're searching for specific Cookie setups for older servers you might need to adjust your search. 

But, if you're looking to setup ASP Classic code on your server 2008 to 2012 or something you'll find this code works great. 

Here's my favoite Cookie List: 

Set4HrCookie() This will set a temp cookie for visitors expiring in 4 hours.

SetKeysCookie() This will add to your values to your current cookie.

SetKeysCookieNoExpireDate() This will set your Cookie for unlimited time. 

SetKeysCookieHttpOnly() This will set your Session Cookie to HTTPOnly.

KillThatCookie() This will set your cookie to expire now.

Simple setup, 5 cookie functions that you should setup in a included page so it can be shared with your website. 

1 Detailed: 

The sub is called like this: 

Set4HrCookie strCookieName,"KEY", strForKey, strDomainNameHere

Sub Set4HrCookie(strCookieName,strCookieKey,strCookieValue,strCookieDomain) Response.Cookies(strCookieName).Expires = DateAdd("n",240, Now()) Response.Cookies(strCookieName).Domain = strCookieDomain Response.Cookies(strCookieName).Path = "/"
Response.Cookies(strCookieName)(strCookieKey) = strCookieValue Response.Cookies(strCookieName).Secure = FALSE

End Sub

'# Note: this is the 4 hour cookie. 

2 Detailed:

SetKeysCookie  strCookieName,"KEY", strForKey, strDomainNameHere

Sub SetKeysCookie(strCookieName,strCookieKey,strCookieValue,strCookieDomain)
Response.Cookies(strCookieName).Expires = DateAdd("d",14, Now())
Response.Cookies(strCookieName).Domain = strCookieDomain
Response.Cookies(strCookieName).Path = "/"
Response.Cookies(strCookieName)(strCookieKey) = strCookieValue
Response.Cookies(strCookieName).Secure = FALSE
End Sub

'# Note: This is the 14 day cookie standard time period.

3 Detailed: 

SetKeysCookieNoExpireDate  strCookieName,"KEY", strForKey, strDomainNameHere

Sub SetKeysCookieNoExpireDate(strCookieName,strCookieKey,strCookieValue,strCookieDomain)
Response.Cookies(strCookieName).Domain = strCookieDomain
Response.Cookies(strCookieName).Path = "/"
Response.Cookies(strCookieName)(strCookieKey) = strCookieValue
Response.Cookies(strCookieName).Secure = FALSE
End Sub

'# Note: This type of cookie with KEY is for you to set "Remember Password" Please do not save a password in your cookie, you can be more creative then that.

4 Detailed:

SetKeysCookieHttpOnly strCookieName,"KEY", strForKey, strDomainNameHere

Sub SetKeysCookieHttpOnly(strCookieName,strCookieKey,strCookieValue,strCookieDomain)
strGMTDateRFC22 = CookieServerUTC("d",1,5,"GMT") ' 1 Day set in char enc dec page
Response.AddHeader "Set-Cookie", strCookieName & "=" & strCookieKey & "=" & strCookieValue & "; expires=" & strGMTDateRFC22 & "; domain="& strCookieDomain &"; path=/; HttpOnly"
End Sub

'# Note: The GMT time is important so much so I had to test it over and over to only find hundreds of articles about this same topic. You much encode the date correctly or it just doesn't work.

Here's the Sub for the CookieServerUTC() the included function within this function was the creative programming from another ASP Classic programmer that I just can't remember right now. (Odd, I remember everything...)

Function CookieServerUTC(strD,strT,strOSet,strZ)
If Len(strOSet) = 0 Then
strOSet = 5
End If
If Len(strD) = 0 Then
strD = "d"
End If
If Len(strT) = 0 Then
strT = 1
End If
strTGMT = DateAdd("h", strOSet,Now())
strCookieD = DateAdd(strD,strT,CDate(strTGMT))
CookieServerUTC = fncFmtDate(strCookieD, "%a, %d %b %Y %H:%N:%S "&strZ&"")

End Function

Function fncFmtDate(strDate, strFormat)
'fncFmtDate(DateAdd("h", +5,Now()), "%a, %d %b %H:%N:%S GMT")&"
'fncFmtDate(DateAdd("m", +5,Now()), "%B")
'# Standard Month, Day Year display no time
'fncFmtDate(DateAdd("h", +2,Now()), "%b %d %Y ")
''Response.Write ("<BR>D = " & strDate)
''Response.Write ("<BR>f = " & strFormat)
'' STANDARD SITE DATE: fncFmtDate(DateAdd("h", +2,Now()), "%A %b %d %Y %H:%N:%S CMT")
' Accepts strDate as a valid date/time,
' strFormat as the output template.
' The function finds each item in the
' template and replaces it with the
' relevant information extracted from strDate
' MnWorks 24 hour clock fncFmtDate(DateAdd("n", +15,Now()), "%H:%N:%S ")
' MnWorks AM/PM Clock fncFmtDate(DateAdd("n", +15,Now()), "%h:%N %P ")
' Template items (example)
' %m Month as a decimal (02)
' %B Full month name (February)
' %b Abbreviated month name (Feb )
' %d Day of the month (23)
' %O Ordinal of day of month (eg st or rd or nd)
' %j Day of the year (54)
' %Y Year with century (1998)
' %y Year without century (98)
' %w Weekday as integer (0 is Sunday)
' %a Abbreviated day name (Fri)
' %A Weekday Name (Friday)
' %H Hour in 24 hour format (24)
' %h Hour in 12 hour format (12)
' %N Minute as an integer (01)
' %n Minute as optional if minute <> 0
' %S Second as an integer (55)
' %P AM/PM Indicator (PM)

if (DatePart("m", strDate) < 10) then
twoDigMonth = "0" & DatePart("m", strDate)
else
twoDigMonth = DatePart("m", strDate)
end if

if (DatePart("d", strDate) < 10) then
twoDigDay = "0" & DatePart("d", strDate)
else
twoDigDay = DatePart("d", strDate)
end if

' Insert Month Numbers
strFormat = Replace(strFormat, "%m", _
DatePart("m", strDate), 1, -1, vbBinaryCompare)

' Insert Month Numbers
strFormat = Replace(strFormat, "%M", _
twoDigMonth, 1, -1, vbBinaryCompare)

' Insert non-Abbreviated Month Names
strFormat = Replace(strFormat, "%B", _
MonthName(DatePart("m", strDate), _
False), 1, -1, vbBinaryCompare)

' Insert Abbreviated Month Names
strFormat = Replace(strFormat, "%b", _
MonthName(DatePart("m", strDate), _
True), 1, -1, vbBinaryCompare)

' Insert Day Of Month
strFormat = Replace(strFormat, "%d", _
DatePart("d",strDate), 1, _
-1, vbBinaryCompare)

' Insert Day Of Month
strFormat = Replace(strFormat, "%D", _
twoDigDay, 1, _
-1, vbBinaryCompare)

' Insert Day of Month Ordinal (eg st, th, or rd)
strFormat = Replace(strFormat, "%O", _
fncGetDayOrdinal(Day(strDate)), _
1, -1, vbBinaryCompare)

' Insert Day of Year
strFormat = Replace(strFormat, "%j", _
DatePart("y",strDate), 1, _
-1, vbBinaryCompare)

' Insert Long Year (4 digit)
strFormat = Replace(strFormat, "%Y", _
DatePart("yyyy",strDate), 1, _
-1, vbBinaryCompare)

' Insert Short Year (2 digit)
strFormat = Replace(strFormat, "%y", _
Right(DatePart("yyyy",strDate),2), _
1, -1, vbBinaryCompare)

' Insert Weekday as Integer (eg 0 = Sunday)
strFormat = Replace(strFormat, "%w", _
DatePart("w",strDate,1), 1, _
-1, vbBinaryCompare)

' Insert Abbreviated Weekday Name (eg Sun)
strFormat = Replace(strFormat, "%a", _
WeekDayName(DatePart("w",strDate,1),True), 1, _
-1, vbBinaryCompare)

' Insert non-Abbreviated Weekday Name
strFormat = Replace(strFormat, "%A", _
WeekDayName(DatePart("w",strDate,1),False), 1, _
-1, vbBinaryCompare)

' Insert Hour in 24hr format
str24HourPart = DatePart("h",strDate)
If Len(str24HourPart) < 2 then str24HourPart = "0" & _
str24HourPart
strFormat = Replace(strFormat, "%H", str24HourPart, 1, _
-1, vbBinaryCompare)

' Insert Hour in 12hr format
int12HourPart = DatePart("h",strDate) Mod 12
If int12HourPart = 0 then int12HourPart = 12
strFormat = Replace(strFormat, "%h", int12HourPart, 1, _
-1, vbBinaryCompare)

' Insert Minutes
strMinutePart = DatePart("n",strDate)
If Len(strMinutePart) < 2 then _
strMinutePart = "0" & strMinutePart
strFormat = Replace(strFormat, "%N", strMinutePart, _
1, -1, vbBinaryCompare)

' Insert Optional Minutes
If CInt(strMinutePart) = 0 then
strFormat = Replace(strFormat, "%n", "", 1, _
-1, vbBinaryCompare)
Else
If CInt(strMinutePart) < 10 then _
strMinutePart = "0" & strMinutePart
strMinutePart = ":" & strMinutePart
strFormat = Replace(strFormat, "%n", strMinutePart, _
1, -1, vbBinaryCompare)
End if

' Insert Seconds
strSecondPart = DatePart("s",strDate)
If Len(strSecondPart) < 2 then _
strSecondPart = "0" & strSecondPart
strFormat = Replace(strFormat, "%S", strSecondPart, 1, _
-1, vbBinaryCompare)

' Insert AM/PM indicator
If DatePart("h",strDate) >= 12 then
strAMPM = "PM"
Else
strAMPM = "AM"
End If

strFormat = Replace(strFormat, "%P", strAMPM, 1, _
-1, vbBinaryCompare)

fncFmtDate = strFormat

End Function ' fncFmtDate

 

5 Detailed:  

KillThatCookie strCookieName, strDomainNameHere

Sub KillThatCookie(strCookieName,strCookieDomain)
Response.Cookies(strCookieName).Domain = strCookieDomain
Response.Cookies(strCookieName).Path = "/"
Response.Cookies(strCookieName).Expires = Now() 
Response.Cookies(strCookieName) = ""
End Sub

 

That's about all for Cookie Setting in ASP Classic. 

As always Code shared is Code Shared so don't worry about using it. If I documented all the tips and tricks it would number in the thousands. Just remember that ASP Programmers share code that is simple and code Ideas but .... And this is the facts.... 

We will not write your code for you. 

Have a good Cookie Session Setting Experience!

The ASP Classic Cookie Code is some of the easiest to overlook when building your ASP ... Wait a minute, you're not still building in ASP Classic are you? Well, let's start with the Cookie Code that will be used over and over within your ASP Classic website. Be sure to offer different visitors different settings. I disapprove of cookies that are set for more than 14 days and really don't return to sites with unlimited time on cookies. Cookies are to enhance the interaction with your website first!