Cookies Writing Reading in ASPCookies are used to maintain state of the visitor, it is maintained in client computer through the web browser. We can drop cookies with required information in client computer by using ASP code. This helps in better user experience in using the website.
For example while logging to our email account there is an option which says remember me in this computer ( or Stay signed in ). These options once enabled then you need not login each time you visit the particular website. This is done by the site ( script ) by keeping login information stored as cookies in user computer. So this is important to note that this feature is not to be used while using public computers or while checking mails in browsing centers where same computers are used by many. However we can disable the feature in browser so it will not accept cookies from any site. In today modern browsers it is by default set to accept cookies as they became part of regular features and for better user experience.
It is browsers responsibility to not allow cookies data of other sites to read by the particular web site.
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.
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 "
We can also use Keys to store cookies like this.
Response.Cookies("User")("Name")=" Ronald "
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 expire date or time is given the cookies will expire as soon as the session ends or the browser is closed.
To add expire date we can add code like this .
Response.Cookies("Name").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("visit").Expires = Date +1
Response.Cookies(“Name”).Expires =#February 20, 2010#
We can also set the expire date to past like this
Response.Cookies("Name").Expires = Date -1
This will expire the cookies once the user browser window is closed.
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.
If we have used keys then we can read like this
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.
Develop a script which will identify the return visitors who have visited within one week time and redirect them to a special welcome page.
Check the Demo of Cookies at plus2net.net