News:

MyKidsDiary.in :: Capture your kids magical moment and create your Online Private Diary for your kids

Main Menu

Learn ASP Lesson 4 - Session Variables and Cookies

Started by sams.raghu, Jun 14, 2008, 03:32 PM

Previous topic - Next topic

sams.raghu

Learn ASP Lesson 4 - Session Variables and Cookies

What is a session variable?

The variables that we have created so far only exist on the page that they are declared. Session variables are the same as normal variables except that they can be used by other pages. Session variables only exist for a certain amount of time which is 10 minutes by default.


Using session variables

You must use the Session object to read and write session variables. Here is an example of how to set the value of a new session variable on one page and how to use it on another page:

page1.asp
Session("MySessionVariable") = Request("txtName")

page2.asp
Response.Write Session("MySessionVariable")

What is a cookie?

Cookies are similar to session variables because they store data that can be used by other pages. The difference is that cookies are stored on the client's computer and session variables are stored on the server. Cookies also last a lot longer that session variables.

Using cookies

The Response.Cookies object is used to create and set the value of a cookie.

Response.Cookies("Name") = "John Smith"

The Request.Cookies object is used to read the value of a cookie.

Response.Write Request.Cookies("Name")

You can also group values under another name such as having first name and last name in the name group.

Response.Cookies("Name")("FirstName") = "John"
Response.Cookies("Name")("LastName") = "Smith"

You can set how long a cookie will be kept on the client's computer before it expires using the Expires property of the cookie. You must add the amount of days you want it to last to the current date when you do this.

Response.Cookies("Name") = "John Smith"
Response.Cookies("Name").Expires = Date + 30