ASP Cookies


Cookie Name Cookie Value Action

 

 

 

 

The Code

<%
' Just in case you wanna change the name of this page, you won't need
' to worry about updating the form or any links.
Dim NameOfThisPage, CutTo
NameOfThisPage = Request.Servervariables("PATH_INFO")
NameOfThisPage = StrReverse(NameOfThisPage)
If Instr(NameOfThisPage,"/") > 0 Then
	CutTo = Instr(NameOfThisPage,"/")
	NameOfThisPage = Left(NameOfThisPage,(CutTo - 1))
End If
NameOfThisPage = StrReverse(NameOfThisPage)

' This script handles both the adding and deleting of cookies

Dim strCookieName  ' The name of the cookie we're processing
Dim strCookieValue ' The value to be assigned to it

' Get the values passed in by the other script
strCookieName  = CStr(Request.QueryString("name"))
strCookieValue = CStr(Request.QueryString("value"))

' If no name is passed in then we don't do anything
If strCookieName <> "" Then

	' Set the value of the cookie
	Response.Cookies(strCookieName) = strCookieValue

	' If the value is nothing then we expire the cookie
	' by setting it's expiration date to yesturday,
	' otherwise we set it to expire tomorrow.
	If strCookieValue <> "" Then
		Response.Cookies(strCookieName).Expires = Date() + 1
	Else
		Response.Cookies(strCookieName).Expires = Date() - 1
	End If
	Response.Buffer = True
	Response.Redirect NameOfThisPage
End If
%>

Implimentation

<!-- Regurgitate Cookies -->
<%
Dim Item
For Each Item in Request.Cookies
  response.write "<br>" & Item & "=" & Request.Cookies(Item) & "<A HREF=" & NameOfThisPage  & "?name=" & Server.URLEncode(Item) & ">Delete this cookie!</A>"
Next
%>



<!-- Cookie adding form -->
<FORM ACTION="<%=NameOfThisPage%>" METHOD="get">
<INPUT TYPE="text" NAME="name">
<INPUT TYPE="text" NAME="value">
<INPUT TYPE="submit" VALUE="Add Cookie!">
</FORM>