Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Session Timing and Locking Out

Status
Not open for further replies.

nalbiso

Programmer
Aug 31, 2000
71
US
Hi,

I am having a problem with my shopping cart session. It times out when it is supposed to. But when the page is reloaded and the user reenters data, the system keeps giving me the following error:

An error occurred while evaluating the expression:


#session.cart[2]#



Error near line 24, column 41.
--------------------------------------------------------------------------------

The element at position 2 in dimension 2 of object "session.cart" cannot be found. That dimension of the object is empty. Please, modify the index expression.


The error occurred while processing an element with a general identifier of (#session.cart[2]#), occupying document position (24:25) to (24:44).


Date/Time: 08/05/02 12:54:08
Browser: Mozilla/4.0 (compatible; MSIE 5.01; Windows 98)
Remote Address: 127.0.0.1
HTTP Referrer: ------------------------------------------------------------

I think that it is just locking up the application because if I come back the next day, the session will work fine but throw out the same error when it times out.

I have the following code in my application.cfm:

------------------------------------------------------------
<!--- set up session management --->
<cfapplication name=&quot;shopping_cart&quot;
sessionmanagement=&quot;yes&quot;
sessiontimeout=&quot;#CreateTimeSpan(0,0,10,0)#&quot;
applicationtimeout=&quot;#CreateTimeSpan(0,0,10,0)#&quot;>
<!--- check to see if the cart has been defined if it has not
set it up --->
<cfif NOT IsDefined(&quot;session.cart&quot;)>

<!--- create a two dimensional array to store the items in --->
<cfset session.cart = ArrayNew(2)>

<!--- create a variable to hold the total cost of all items in cart
and set it to 0 --->
<cfset session.total_cost = 0>

</cfif>

------------------------------------------------------------

If anyone can tell me what's wrong with this thing or how to kill the session completely (if that is what I need to do) I would greatly appreciate the help.

Thanks!
 
If the session expires, and the next page requested tries to evaulate #SESSION.cart[2]#, you should get the error you specified. What's happening is that the code in your Application.cfm page is catching the fact that the session does not exist anymore, and is initializing the #SESSION.cart# array, but it is not putting any values in the array. Perhaps you're expecting ArrayNew(2) to create a new array with two elements, but that's not correct -- it creates a new, empty, two-dimensional array.

As to why it doesn't happen this way the following day, my suspicion is that you're coming back into the site the second day and you're not hitting the page that's trying to reference the second element of the #SESSION.cart# array. Try this: reproduce the error as you described it at the end of your work day. Then, leave the browser open and try again the next day -- I believe you will still have the same error because the page you are requesting is trying to access an array element that doesn't exist.
 
Thank you for responding.

But what should I do to fix this?
 
You'd have to post the code that is actually throwing the error. Then we can advise how to fix it.
 
When I load my View Items page, the error occurs, but I think that the error is happening with either the application or add item pages:

This is view Items:
---------------------------------------------------------------------------------------------------
<!doctype html public &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>

<html>
<head>
<title>View Items</title>
</head>

<body> <!--- check to see if there are items in the cart --->
<cfif arraylen(session.cart) eq 0>
<font size=&quot;1&quot; face=&quot;Arial, Helvetica&quot;>There are no items to display</font>
<cfelse>

<!--- loop over the array and out put each item --->
<cfset i = 1>
<cfloop from=&quot;1&quot; to=&quot;#arraylen(session.cart)#&quot; index=&quot;count&quot;>
<cfoutput>
<form name=&quot;add_item&quot; method=&quot;post&quot; action=&quot;remove_update_item.cfm&quot;>
<table cellpadding=&quot;5&quot; cellspacing=&quot;1&quot; border=&quot;1&quot; width=&quot;600&quot;>
<td bgcolor=&quot;yellow&quot;><font face=&quot;Arial, Helvetica, sans-serif&quot; size=&quot;2&quot;><b>Quantity:</b></font></td>
<td bgcolor=&quot;Yellow&quot;><font face=&quot;Arial, Helvetica, sans-serif&quot; size=&quot;2&quot;><b>Type of Vehicle:</b></font></td>
<td bgcolor=&quot;Yellow&quot;><font face=&quot;Arial, Helvetica, sans-serif&quot; size=&quot;2&quot;><b>Price:</b></font></td>
<tr>
<td bgcolor=&quot;##dae1e9&quot; width=&quot;25&quot; align=&quot;center&quot;><font size=&quot;2&quot;
face=&quot;Arial, Helvetica&quot;>#session.cart[2]#</font></td>
<td bgcolor=&quot;##dae1e9&quot; width=&quot;300&quot;><font size=&quot;2&quot;
face=&quot;Arial, Helvetica&quot;>#session.cart[4]#</font></td>
<td bgcolor=&quot;##dae1e9&quot; width=&quot;50&quot;>
<font size=&quot;2&quot;
face=&quot;Arial, Helvetica&quot;>#dollarformat(session.cart[3])#</font>
</td>
<td bgcolor=&quot;gray&quot; width=&quot;125&quot; align=&quot;center&quot;><input type=&quot;submit&quot;
value=&quot;Remove&quot;></td>
<td bgcolor=&quot;gray&quot; width=&quot;25&quot;><input type=&quot;text&quot; size=&quot;2&quot;
name=&quot;item_quantity_2&quot; value=&quot;0&quot;></td>
<td bgcolor=&quot;gray&quot; width=&quot;75&quot;><input type=&quot;submit&quot;
value=&quot;Update&quot;></td>
</tr>
</table>
<input type=&quot;text&quot; name=&quot;item_quantity&quot; value=&quot;#session.cart[2]#&quot;>
<input type=&quot;text&quot; name=&quot;item_id&quot; value=&quot;#session.cart[1]#&quot;>
<input type=&quot;text&quot; name=&quot;item_cost&quot; value=&quot;#session.cart[3]#&quot;>
<input type=&quot;text&quot; name=&quot;array_position&quot; value=&quot;#i#&quot;>
<br></cfoutput>
<br>

</form>



<cfset i = i + 1>
</cfloop>
<br><br>

<cfoutput>
<font size=&quot;2&quot; face=&quot;Arial, Helvetica&quot;><b>
Total Cost: #dollarformat(session.total_cost)#</b></font>
</cfoutput>
</cfif>
<br>
<br>
<cfoutput>
<font face=&quot;Arial, Helvetica, Sans-serif&quot; size=&quot;2&quot;><a href=&quot;estform.cfm&quot;>Add another vehicle type to this estimate.</a></font>
<br><font face=&quot;Arial, Helvetica, Sans-serif&quot; size=&quot;2&quot;><a href=&quot;chkout.cfm?total=#dollarformat(session.total_cost)#&quot;>Complete field trip request order.</a></font></cfoutput>
</body>
</html>



This is Add Item:
---------------------------------------------------------------------------------------------------
<!--- get the length of the array and add one to it --->
<cfset position = ArrayLen(session.cart) + 1>

<!--- add the item to the array ---> <cfset session.cart[position][1] = &quot;#form.Type#&quot;>
<cfset session.cart[position][2] = &quot;#form.noveh#&quot;>
<cfset session.cart[position][3] = &quot;#form.totalestcost#&quot;>
<cfset session.cart[position][4] = &quot;#form.Description#&quot;>


<!--- set the total cost of all items --->
<cfset current_cost = form.totalestcost>
<cfset session.total_cost = session.total_cost + current_cost>

<!--- send the user to the view page --->
<cflocation url=&quot;view_items.cfm&quot;>
 
Don't create the shopping cart array in your CFAPPLICATION tag, create it when you add the first item to it. Maintain a per-session cookie (no EXPIRES, so the browser won't save it). Store in this whether the cart existed last time you output a page. When you get the incoming message, check the cookie and the existence of the cart. If the cookie says the cart should be there and the cart isn't there, the session has timed out - apologise to the user for the inconvenience and explain why timeouts protect him / her and your site.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top