Learn about ASP Session Objects (11.2 ASP):

asp 11.2

Just for revision, the issue discussed in previous post was when to end the session. For example you are using an application for past 20 minutes. Then you stop using it for like an hour. In the meantime this application is using loads of resources on the server for no use. So to reduce the resources, if theserver shuts the session like after the 5 minutes( you have not been using the application). Then you would have to start again if by any chance you come back sooner than an hour.  Therefore to find the right time to end the session is quite difficult and unpredictable. So, to avoid this issue we can store few variables in the session object. By using ASP we can store various variables in the sessions. Following example will set the variable name to “RABIA” and age to “22”. <% Session(“name”)=”RABIA” Session(“age”)=22 %> These or any values stored in session variables can be accessed from any page in an application. Following will print the values of variables:

<% Response.Write(Session(“name”)) Response.Write(Session(“age”)) %> The above code will give following output: RABIA 22 The user preferences are very significant. They can be stored in the session variables. Later on, these preferences can be used to decide what page should be send to user according to his/her preference. Following is the example which shows the text version of page and message that is going to be displayed if the screen resolution is high: <% If Session(“screenres”)=”high” Then %> Displaying the multimedia version due to high resolution <%Else%> Displaying the text version due to low resolution <%End If%> Removing the variables: All session variables are inside the content collection. Any of these variables can be removed by using “Remove method”. Following example will delete the session variable name if the age is greater than 92: <% If Session.Contents(“age”)>92 then Session.Contents.Remove(“name”) End If %> Removing each variable one by one can be troublesome. So to avoid this “RemoveAll” method can be used as shown in the following example where all variables are removed:

<% Session.Contents.RemoveAll() %>

Similarly displaying the variables one by one can be troublesome, so to avoid this using loop to see all the contents collection is possible as shown in following example: <% Session(“name”)=”RABIA” Session(“age”)=22 dim m For Each m in Session.Contents Response.Write(m & “<br />”) Next %>

This will give the following output: Name Age For instance you do not know the number of variables in the session object then you can use the following example to loop through the content collection: <% dim m,s s=Session.Contents.Count Response.Write(“Session variables: ” & s) For m=1 to s Response.Write(Session.Contents(i)) Next %> This will print all the variables as follows: Session variables: 2 RABIA 22 To see that all values are stored in session objects, loop through the staticobject collection is possible. Following is the example that shows how it can be done: <% dim m For Each m in Session.StaticObjects Response.Write(m) Next %>

 

For more visit next posts in this category.To go back to Contents of this tutorial: ASP Active Server Side Scripting Contents

email

6 Responses to Learn about ASP Session Objects (11.2 ASP):

  1. Madison says:

    This post will help the internet users for setting up new website or even a blog from start to end.

  2. I simply want to say I’m new to blogging and certainly savored this blog. Most likely I’m want to bookmark your blog post . You really come with awesome writings. Cheers for revealing your blog site.

  3. Aw, this was an exceptionally nice post. Taking the time and actual effort to generate a great article… but what can I say… I put things off a lot and don’t seem to get nearly anything done.

  4. penny stock says:

    What is your opinion of personal blogs and online journaling?

    • admin says:

      They are an efficient way in which a writer can get to the audience and share his / her thoughts as well.

  5. Very efficiently written story. It will be helpful to anybody who utilizes it, including me. Keep up the good work – can’r wait to read more posts.

Leave a Reply