> Learn Visual Basic immersed in a virtual comic book! Simple explanations and fun!
>FREE ASP SOURCE CODE: 
Free ASP Database Application! Read and write cookies, query and write to databases, master complex search routines, killer SQL, redirect jonk!

The JonShaft Cookie by, Glenn Cook
This Cookie tutorial is designed for anyone interested in learning how to control a cookie with ASP.  If you did not connect to this page from my main column page then click here for a live example of the cheesy cookie.


Return to Main Column Page

How it Works:
Green = Server-side ASP code
Purple= HTML Code
Black= Visible HTML Text
Red= My Comments

<% If Request.Cookies("JonShaft").HasKeys Then %>

<HTML>
<HEAD>
<TITLE>
A Jon Shaft Cookie. It's cheesy!</TITLE>
</HEAD>


 

' This  is my " if then" where I find out if the user already has the JonShaft Cookie on their system.  The HasKeys attribute is real handy for checking cookies which have multiple values associated with them- those values are referred to as Keys by ASP.  This cookie says, if they've got the cookie, execute the next statement.

 

Welcome Back, <%Response.Write (Request.Cookies("JonShaft")("FirstName"))%>&#32
<%Response.Write(Request.Cookies("JonShaft")("LastName"))%>!

'This  line basically says, "OK, they've got the cookie, let's Request the cookie's keys/info and write them to the page."  The Response Object allows me to spit information to the user, the Request Object allows me to extract it from the user.  Basically what we've done is said, "Check for cookie(Request), extract cookie(Request), write cookie to page(Response)"
**The &#32 tells HTML to enter a space**

 

 

<%
Else If "BadMutha" = Request("ActionType") Then

TheFirstName=Request("FirstName")
TheLastName=Request("LastName")
'The "Else in the first line says,"Ok, the "If-Then" wasn't true....But there's more ahead!"
'This section is for the form input and it creates the cookie.  You see, this single page of code serves three functions: It's for people who've been here before, people who haven't, and it makes a cookie for the people based on their form input.  You'll notice that just after the FORM METHOD html I have some ASP code which actually asks for it's own name so it can post to itself!

The "If-Then" statement checks to see if the user sent a form with the name "ActionType" which has the value equal to "BadMutha"!
It also makes two variables based on the user input to stick into the JonShaft cookie.  I call the variables TheFirstName and TheLastName appropriately.

 

 

Response.Cookies("JonShaft")("FirstName") = TheFirstName
Response.Cookies("JonShaft")("LastName") = TheLastName
Response.Cookies("JonShaft").Expires = #September 3, 2001#
Response.Cookies("JonShaft").Domain=  &_ ".www.activeserverpages.com"

Response.Cookies("JonShaft").Path = "/glenncook"
Response.Write "Thanks for your submission, "
Response.Write(Request("FirstName"))%>
!
'The Response Object is your cookie writing friend! This code actually writes the cookie to the client's system.  You'll notice that I make the The FirstName key equal to the "TheFirstName" variable which I extracted from the Request("FirstName") Querystring from the input form.(Whoooo! That was a mouthful!)

Then I tell the cookie when to expire, the domain that it should be sent to, and the path within the domain.  But the secret recipe is that little period in the domain=".www.activeserverpages.com"  Actually without that little period, no cookie!  Charles Carol helped me on this little issue which drove me nuts.   MAKE SURE THE DOT IS THERE! Also make sure the path is EXACTLY as I wrote it.

 

<%Else%> 'The "Else" code here basically says," Ok, they don't have the cookie and they didn't send any form information, send them the following code!

 

<FORM METHOD=POST ACTION="<%=Request.ServerVariables("SCRIPT_NAME")%>">
<input type="hidden" name="ActionType" value="BadMutha">

You must be new around here. Gimme your name?!<p>
FIRST NAME:<input type="text" name="FirstName" size="15"><br>
LAST NAME:&#32<input type="text" name="LastName" size="15"><p>

<input type="reset" value="Clear Form">
<input TYPE="submit" VALUE="Submit Info!">

<%End If%>
<%End If%>
</BODY>
</HTML>
'This section is your HTML input form for the new visitor!  You'll notice that I stuck a hidden input box in there.  Well basically  that's so I can get "Bad Mutha" as the Action Type but is very effective for passing stuff that the user doesn't need to see.

I also extract the name of this asp page -which I mentioned above- using the Request.ServerVariables object.  Remember: If you need some information in ASP pages, just "Request" it.

Finally, don't forget to End your If!

 

Return to Main Column Page