ASP Tutorial:Using Session, Application and Cookies

Home arrow .NET Tutorials arrow ASP.NET arrow ASP Tutorial:Using Session, Application and Cookies
ASP Tutorial:Using Session, Application and Cookies Print E-mail
Contributed by Howell   
Sunday, 11 June 2006
  • Session

This object will manage information on individual user sessions. A session object is created when a user does not have session access to a page in the application. The object persists until it times out or is explicitly abandoned. For example, if you use the free email facility of Yahoo or another provider, your email address will appear on the top of the inbox as long as you exit from that page or log out from your inbox.

The whole process is very simple. First we will assign a session variable and then we will attach the session code at the appropriate location on the page. A sample script is given below for your reference.

<% session("permission") = "yes"
session("urname") = Request("urname") %>
We will then apply the above variable, somewhere on the script like
welcome<% session("urname") %> to Mark Inc members area
I will explain this concept with a detailed example later while we discuss database access.

  • Application

You can use the application object to share information among all users. Because all users of your application share the same application object you can use two methods to prevent multiple users from modifying information at the same time. Using lock and unlock methods, you can prevent these types of collisions from happening and thereby providing reliable access to the server to all users. This method is commonly used to create counters on the web pages.

For example the following code creates a simple counter. The counter value will increment each time when the page loads. But the value will reset if you shut down your computer.

<body>
<% application.lock %>
<% application("counts") = application("counts") +1 %>
<% application.unlock %>
This page has been visited by <%= application("counts") %> Times
</body>
Request
When you clicks a form button, you are requesting the server to perform the necessary actions. This is done via the request object. For example to update the database we use to give a code like this <% rs("name") = request.form("name") %>. 
This piece of code will add the entered name to a database on the server. We will discuss more about this object later while covering database access.

  • Response

When you request some information from the server, naturally the server sends its feedback to you. This is done via the response object. This object has an important method called redirect which transfers you to the page specified. If you want your users to be forwarded to www.internet.com after a successful login from your site, you should code the script as
<% response.redirect("http://www.internet.com") %>

For example, to print your name on the top of a page, we can give a code like this
Your  name is <% response.write("David") %>.
This object is commonly used in applications, which provides access to databases and
in sites which require generation of user's data dynamically.

  • Cookies

In cookies the information is saved on the clients computer and not in the server. This is suitable for sites with a lot of visitors like job sites. A user filling up  a resume form need not re-enter all the information each time when he/she updates the page. All the previous information will be retrieved back and the user need only edit the required fields. The server stores the relevant information on the client's computer. 
This feature will not be available if the cookie is deleted, expired etc. If a user formats the hard drive after submitting, the information will no longer be available. Some cookies automatically gets deleted and will be having an expiration date. In some cases we have to manually delete the cookies. Generally, in the Windows operating system, cookies are stored in c:windowscookies folder.
The following code stores the cookie on the client's system.
<% response.cookies("whatever") = "information" %>
The following statement will return the cookie if the user request it at a later time
<% request.cookies("whatever") %>

Now let us examine an example which uses cookies to store the data. The program requests to enter your name and when the button is clicked, the value in the textbox will be stored in a cookie. However this cookie will expire when you close the browser. 


<% if request("content_length") <> 0 then
Response.Cookies("data") = request("coo")
end if
%>
<html>
<head>
<title>
Exploring cookies
</title>
</head>
<body>
Current value is
<%= Request.Cookies("data") %><br>
<form action = "http://localhost/cookie007.asp" method = "post">
<input type = "text" name = "coo"
value = '<%= Request.Cookies("data") %>'>
<input type = "submit" value = "submit">
</form>
</body>
</html>


  home              contact us

 

©2006-2008 DeveloperZone.biz   All rights reserved     powered by Mambo Designed by Siteground