We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

ASP Session Object

Patricia080698
on
Medium Priority
479 Views
Last Modified: 2013-12-25
I am in the process of creating a shopping basket that will store all of the items chosen by a user (there could be different users at the same time) into the shopping basket. I want to use ASP (active server pages) session object. Unfortunatley I do not know a lot about this or about cookies.

I need to store for example :
item : brush
color : brown
price : 5.00

these values are supposed to be stored into my session object. How can I do this ?
Comment
Watch Question

Commented:
My oh my, Patricia,

just another one who likes to be dependant of cookies.
There is a neat solution neither depending on the browser nor on the user's preferences.

I am sorry that I will not post any answer anymore since experts-exchange.com closed my account (see https://www.experts-exchange.com/topics/experts-exchange/Q.10072396).

Robert

Commented:
Sessionvariables can store almost anything.
What I would do in your case is make a multidimensional array and store that in Session("Orders").
At each next order the Session var would be read into an array, that array would be changed, and stored back into Session("Orders").

The hard thing is manupilation with the multidimensional array, but that should not be too difficult, and is in fact standard programming.

The easy thing is that you know that everything is stored into one variable: Session("Orders").


Commented:
Oh and it is not necessary to understand cookies when you use Session Variables. The only thing is that users MUST accept a cookie, otherwise session variables don't work.

Author

Commented:
How do I create the session "order" and how would that store each order of a particular person that is ordering an item ? and do I have to create a table (I am using ACCESS95) "orders" for the items to be stored in? and tables. I am kind of lost...I would appreciate it a lot if you lend me a hand.
sax

Commented:
I am assuming you are aware of creating active server pages and the objects available within ASP.

You need to create an array that will be used to save the session information. Assign that array to a session variable. Later, you can retrieve the array from the session variable

In the asp script that processes the user selection, do the following:

Dim myarray()
myarray(0) = "brush";
myarray(1)="brown";
myarray(2)="5.0";

This uses a 1-dim array. If u would like to use a 2-D array, its no different.


;Store the array into a session variable, called order:

Session("order") = myarray

;this session variable order is unique to each particular user. If you would like, you can build the session variable, so it holds the user's login name, instead of "order". U do not need to create any tables. Its as simple as creating a global/static variable in a standard language - only this is available for a particular session of a unique user.

;Later in the code or in another request from the same user, you can retrieve the prev order from the session var

temparray = Session("order");

;then assign any new information, using indexes

temparray(0) = "paste";

-HTH

Commented:
Hi Patricia,

the IP address is changing although the user did not hang up...
Therefore I am using the query string again to identify the user.
If you like, I will send you the code of http://www.tueshaus.de (mailto:info@gartenkorb.com). They used the SessionObject, there were faults (because some users did not accept cookies), and now I am their new programmer.

If you want to use cookies, here is some code (maybe not the best, but showing the principles) - in addition to sax's answer:

<%
Sub StoreArticle(Name,Color,Price)
  i = CInt(Session("ItemCount")) 'default=0
  Session("ItemCount") = i + 1
  Session("ArtName")(i) = "brush"
  Session("Color")(i) = "brown"
  Session("Price")(i) = "5.00"
End Sub

Sub ListArticles()
  For i = 1 To Session("ItemCount")
    Response.Write Session("ArtName")(i)
  Next
End Sub

Sub GetArticle(ArtName)
  For i = 1 To Session("ItemCount")
    If Session("ArtName")(i) = ArtName Then
      Response.Write "Found"
    End If
  Next
End Sub
%>

Do not forget to have one special variable in every ASP page. If no Session variable is transferred to the next page, a new session will begin:
<%
Session("dummy") = "dummy"
%>

To test if the user accepts cookies there are two ways:
1.) ASP Cookies

Put this code to your starting page:
<%
Session("CookiesEnabled") = True
%>
Or put it in global.asa (create it in the root directory):
<%
Sub Session_OnStart()
  Session("CookiesEnabled") = True
End Sub
%>

To all your (other) pages (it will not work with the first page) put the code:
<%
If Session("CookiesEnabled") <> True Then
  Response.Redirect "YouMustHaveCookiesEnabledAndHereIsWhyToDoItAndHereIsHowToDoIt.asp"
End If
%>

2.) Set a permanent cookie and test it. Put this code on top of every page:
<%
If Request.Cookies("Test") = "" Then
  Response.Cookies("Test") = "Test"
End If
If Request.Cookies("Test") = "" Then
  Response.Redirect "YouMustHaveCookiesEnabledAndHereIsWhyToDoItAndHereIsHowToDoIt.asp"
End If
%>

That's it. Hope it helps. You may send questions to the email address above.

Author

Commented:
sax : I need a bit more clarification. I am begining to understand your idea. I must say that I am not that advanced in ASP but I am trying to learn. Anyways, Once I put the values in my session("order") how can I display all of this session's contents ?

I also did not understand clearly how the session is going to store for example 5 different orders of the same user.

Commented:
Patricia,

Will you be using JavaScript or VBScript in your ASP.
I think that JavaScript offers some more possibilities here, since you can easily create your own-defined objects.

But in VBScript it can also be done
Some code snippets

To make an array for 2 orders:

ReDim Orders(2,3)
Orders(1,1) = "brush"
Orders(1,2) = "brown"
Orders(1,3) = "5.00"

Orders(2,1) = "basket"
Orders(2,2) = "blue"
Orders(2,3) = "10.00"

Then this can be stored in a Seesion Variable:

Session("Orders") = Orders

If you go to the next ASP-page, you can get everything that was ordered before by:

Orders = Session("Orders")
Counting the number of orders stored:
intNumberOfOrders = Ubound(Orders)

Author

Commented:
I am trying to display the contents of my Session object but nothing seems to be working.
I put this code in my htm file where the user chooses an item:

Dim array()
myarray(0)= rs.fields(3).value & " " & val
myarray(1)= Rmodel.fields(3).value
Session("orders")= myarray

and then I put this code in the htm file which is supposed to show me the contents of my basket (but for now i am just trying to get the values that are inside my session variable):

<%
valeur = Session("orders")
%>
<br>valeur----><%= valeur %> <br>

This does not work. I know that I am missing a lot of information, so any help would be greatly appreciated. I am ready to add more points if it is all about that....thanks again.

Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
Sybe. I already have corrected this problem. So out of generosity, the points are yours.

Commented:
thank you :)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.