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! |
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More ASP Code Articles More By aspfree developerWorks - FREE Tools! | You probably have thousands of lines of COBOL code loaded with business intelligence and being used to run your business, along with an army of developers maintaining these applications. Learn how to prepare your applications and developers so you can keep that competitive edge and move to a service-oriented architecture with the IBM Rational Enterprise Modernization solutions. Replay is available for 9 months. FREE! Go There Now!
| | | | CakePHP is a stable production-ready, rapid-development aid for building Web sites in PHP. This "Cook up Web sites fast with CakePHP" series shows you how to build an online product catalog using CakePHP. FREE! Go There Now!
| | | | Learn how you can extend modern application lifecycle management to IBM System z through the IBM Rational Software Delivery Platform (SDP). The Did you say mainframe? e-kit includes podcasts, webcasts, tutorials, white and red papers, demos, and articles designed to help ease the challenges of modernizing your enterprise. This complimentary kit for mainframe developers is a practical, how-to guide for making the most of an existing development environment, including the skills and infrastructure already in place at an established enterprise. FREE! Go There Now!
| | | | Download the Rational Application Developer (RAD) v7.5 open beta code and start developing applications for the JEE5 standard which features EJB3.0, JPA, JSF 1.2, JSP 2.1 and Servlet 2.5 standards. When you use this beta you will see how you can increase developer productivity for already existing applications with improved support for refactoring, as well as adding new features to existing applications. In addition, the beta provides tooling for JD Edwards, Oracle, SAP, Siebel and PeopleSoft to improve the developer productivity with these enterprise systems. FREE! Go There Now!
| | | | Learn how to implement a build management system that uses and extends your existing automation technologies. This tutorial shows, step-by-step, how to install and configure IBM Rational Build Forge to manage builds for Jakarta Tomcat from source code. FREE! Go There Now!
| | | | Because access to government information continues to be an area of concern for many U.S. citizens with disabilities, the U.S. government enacted Section 508 of the Rehabilitation Act in 2001 to ensure that government agencies create accessible Web content, enabling all citizens to access the information they need. A fully accessible Web site makes Web content accessible to all individuals, including those with disabilities, who may be accessing Web content via a variety of user agents. Common user agents include standard Web browsers, text-only browsers, assistive devices and mobile devices such as cell phones or personal digital assistants (PDAs). FREE! Go There Now!
| | | | Get a free trial download of IBM Lotus Forms V3.0 (formerly Workplace Forms), which provides a zero-footprint eForms solution to help you automate and move forms-based business processes off the desktop and onto the Web. With Lotus Forms, you can extend applications beyond the firewall by creating a single electronic form document ready for use in both thick and Web 2.0 thin client format. FREE! Go There Now!
| | | | Visit IBM developerWorks to try the IBM SOA Sandbox for connectivity. The SOA Sandbox for connectivity provides a trial environment with the tooling and components to help you explore how to effectively connect your infrastructure and integrate all of the people, processes and information in your company. Use the hosted sandbox to explore SOA techniques that streamline connecting existing IT assets together, as well as learn how to connect them to new business logic. FREE! Go There Now!
| | | | IBM Lotus Notes 8 provides a wide range of developers the ability to provide customized, integrated user interfaces via composite applications and via custom sidebar and toolbar plug-ins. This webcast provides you with tips and techniques to use with out-of-the-box capabilities of Lotus Notes 8, and survey how you can share useful components within your own company and within a larger community. FREE! Go There Now!
| | | | Join this webcast to learn how IBM Rational's Functional Testing solution enables you to implement automation your way, at your pace, with your existing staff. In this webcast, you’ll learn how you can eliminate redundancy of manual test scripts, reduce errors, and increase test coverage through test automation. After this presentation you will understand how IBM Rational Functional Testing solution can streamline your manual testing and make test automation easily attainable. FREE! Go There Now!
| | | | All FREE IBM® developerWorks Tools! | |