Cookies 101 by Sean Vaccariello

 

Contributed by
Rating: 4 stars4 stars4 stars4 stars4 stars / 5
October 22, 2000
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

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!

blog comments powered by Disqus
ASP CODE ARTICLES

- ASP Forms
- ASP: The Beginning
- Getting Remote Files With ASP Continued
- Inbox and Outbox Manipulation in ASP
- Relational DropDownList Using VB.NET
- Ad Tracking URL Hits
- Use ViewState to display one record per page...
- Send Email using ASP.NET formatted in HTML
- ASP File Explorer
- ASP/XML Interview questions by Srivatsan Sri...
- Pressing RETURN won't submit the form
- This shows how you get the TEXT of a combo r...
- Group Data by Adrian Forbes
- Multiple checkbox select sample
- Multiple checkbox select with all values sam...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials