I’d like to create a UDF that I can call that will emulate the following lines of code. The goal is to kill a user’s session (various login data) while retaining their shopping cart data. The code works, but I haven’t been able to get a UDF version of it to work yet. Any tips?
<!--- move cart to temporary variable --->
<cfset temp_cart = arrayToList(session.cart)>
<!--- kill entire session --->
<cfset foo = structclear(session)>
<!--- re-initialize cart --->
<cfset session.cart = ArrayNew(1)>
<!--- move temporary cart back to new session cart --->
<cfset session.cart = listToArray(temp_cart)>
Not even sure where to begin, but here’s my messed up UDF so far:
function LogOut()
{
temp_cart = arrayToList(session.cart);
foo = structclear(session);
session.cart = ArrayNew(1);
session.cart = listToArray(temp_cart);
}
Just whilst we clarify what the problem is here, there’s some changes you could make to that UDF to improve the code quality:
That should blow away all your session variables except the cart.
(I’ve just seen your new comment about the “routines cannot be declared more than once”)
OK, so the problem is not your function, it’s how you’re loading it in. Where are you delcaring this function? You are obviously declaring it in a file that is being used more than once in the same request. Can you not put this function in your Application.cfc or Application.cfm? Or in a UDF library that you make sure you only include once per request?
What version of CF are you on?