Cookies 101 Sean Vaccariello www.TheSecureBox.com Cookies are a very useful; they can store usernames/password, preferences, last visits, etc. I will focus on storing information a user may type in at a typical website. So let’s get started… First we will create a standard form that will contain a name, e-mail, and country. The code for this is as follows: <form method="POST" action="process.asp"> <p>Name: <input type="text" name="name" size="20"></p> <p>E-mail: <input type="text" name="email" size="20"></p> <p>Country: <input type="text" name="country" size="20"></p> <p><input type="submit" value="Submit" name="B1"> <input type="reset" value="Reset" name="B2"></p> </form> |
*Note – this will post the information to an asp page called “process.asp”, you can change this to point anywhere you want.
Next we will need to create the “process.asp” page. This page will use the Request object to get the information that the user submitted. We will start off by declaring all of our variables like so: <% Dim reqName Dim reqEmail Dim reqCountry |
Then we’ll start requesting the form inputs: reqName = Request.Form("name") reqEmail = Request.Form("email") reqCountry = Request.Form("country") |
And now for the main part, this is where the server puts the cookie(s) on the client’s machine. Response.Cookies("pref")("name") = reqName Response.Cookies("pref")("email") = reqEmail Response.Cookies("pref")("country") = reqCountry Response.Cookies("pref").Expires = Now + 365 |
This will store the users name, e-mail, and country in the “pref” cookie on the computer. We also used the “expires” method to expire the cookie 365 days from the time the cookie was put on the computer; you can change this to anything you want. *Note – if you want the cookie to expire immediately then you can do something like this: Response.Cookies("pref").Expires = Now – 1000 %> |
And lastly we want to display the cookie using the request object like so: <html> <head> <title>Cookies 101</title> </head> <body> Name: <%= Request.Cookies("pref")("name") %> <BR> E-Mail: <%= Request.Cookies("pref")("email") %> <BR> Country: <%= Request.Cookies("pref")("country") %> <![endif]> </body> </html> |
And that’s it! |