SQL PHP HTML ASP JavaScript articles and free scripts to download

Cookies Writing Reading in ASP

Cookies 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.

Related Tutorial
Cookies in PHP
Another example is use of cookies by web tracking software. Once a visitor visited a site cookies with all relevant information is kept in the client computer. When same computer ( client ) opens the same site again the script can identify the visitor and add the information to its tracking system.

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.

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 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
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

This will expire the cookies once the user browser window is closed.

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.

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


Google+

Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked



Join Our Email List
Email:  
For Email Newsletters you can trust
HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2014 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer