Solved

ASP Session Object

Posted on 1998-09-02
12
460 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 ?
0
Comment
Question by:Patricia080698
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 1

Expert Comment

by:soeding
ID: 1828484
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 http://www.experts-exchange.com/topics/experts-exchange/Q.10072396).

Robert
0
 
LVL 28

Expert Comment

by:sybe
ID: 1828485
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").


0
 
LVL 28

Expert Comment

by:sybe
ID: 1828486
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.

0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 1

Author Comment

by:Patricia080698
ID: 1828487
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.
0
 

Expert Comment

by:sax
ID: 1828488
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

0
 
LVL 1

Expert Comment

by:soeding
ID: 1828489
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.

0
 
LVL 1

Author Comment

by:Patricia080698
ID: 1828490
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.
0
 
LVL 28

Expert Comment

by:sybe
ID: 1828491
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)

0
 
LVL 1

Author Comment

by:Patricia080698
ID: 1828492
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.

0
 
LVL 28

Accepted Solution

by:
sybe earned 80 total points
ID: 1828493
<%
valeur = Session("orders")
%>
<br>valeur---->
<%
i = 1
Do While i <= Ubound(valeur)
  Response.write valeur(i) & "<br>"
  i = i + 1
Loop
%>

 <br>
0
 
LVL 1

Author Comment

by:Patricia080698
ID: 1828494
Sybe. I already have corrected this problem. So out of generosity, the points are yours.
0
 
LVL 28

Expert Comment

by:sybe
ID: 1828495
thank you :)
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
AWK: How to strip all double quotes from a file? 7 44
powershell switch statement 7 47
Powershell GUI 11 95
CSV file copy field 1 to field 2 2 63
Introduction:   Welcome to my first article ever. To begin with, the reason I write this article.  I participated in a question on Experts Exchange about the start command in Windows and there were some discussion about the usage. The discussio…
Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question