Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ASP Session Object

Posted on 1998-09-02
12
Medium Priority
?
467 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
  • 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
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
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 160 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Suggested Courses

886 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