Cookies Writing Reading in ASP

Cookies let a Classic ASP application store a small amount of state in the visitor's browser. ASP writes cookies through Response.Cookies and reads them through Request.Cookies.



A cookie can support preferences or a signed-in experience, but do not store passwords or other sensitive credentials directly in a cookie. For authentication, prefer an opaque token or server-side session design, and apply appropriate transport/security attributes in the deployment environment.



Cookies may also support analytics or preference tracking. Their use should follow applicable privacy requirements and user-consent rules for the deployment.



Browsers scope cookies by domain/path rules, but application code must still avoid placing secrets in client-readable state.



We will learn how to create cookies , how to read the data stored in cookies and how to set the expire date for the cookies.

Creating Cookies

In ASP we can create cookies by using response header. These small bit of information is stored in client computer by using response.cookies
Response.Cookies("Name")=" Ronald "
Response.Cookies("visit")= Now
We can also use Keys to store cookies like this.
Response.Cookies("User")("Name")=" Ronald "
Response.Cookies("User")("visit")= Now

Expire time & date for cookies.

As we create the cookies with some data , we can add expire date to the cookies. Cookies will be deleted from client computer after the expire period.

If no persistent expiry is set, the cookie normally behaves as a session cookie and is removed according to the browser's session-lifetime behavior.

To add expire date we can add code like this .
Response.Cookies("Name").Expires = Date +1
Response.Cookies("visit").Expires = Date +1
The above code will set the expire date to next day ( or one day advance ) . We can also increase expire date by another 15 days or to any future date as per the requirement. Same way we can also set expire dates to future fixed dates like this by using date formats.
Response.Cookies("Name").Expires =#February 20, 2010#
Response.Cookies("Name").Expires =DateSerial(2012,1,27)
We can also set the expire date to past like this
Response.Cookies("Name").Expires = Date -1
Setting an expiry date in the past instructs the browser to expire/delete that cookie.

Reading Cookies

We can use Request Object to read the data stored in cookies. The client browser will only allow to read cookies kept by the same website. Here is the code.
Response.Write(Request.Cookies("Name")) 
Response.Write(Request.Cookies("visit")) 
If we have used keys then we can read like this
Response.Write(Request.Cookies("User")("Name")) 
Response.Write(Request.Cookies("User")("visit")) 
Storing small bit of information by cookies helps as server resources are not used and data is kept at client side. This is helpful particularly for sites with large number of visitors. But problem is client may select not to accept cookies. Still cookies are popular in today's browser and it adds value to user experience.
Referrer IP address
ASP Home






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer