Shopping Cart << // Active tracking example (C) 1997/1998 NetProfessional. by Robert Woodhead In this example, ID is the reference # of the shopping cart ID. It may be passed to a script in the URL or in a hidden form argument, and is used to keep track of a visitor's cart. CART is the name of the cookie containing the cart ID. The cookie is used to hook the user back up to his shopping cart when he returns to the site. The algorithm is as follows: 1) if the referrer is from outside the site, don't trust the ID 2) if no ID but CART is there, then if the database has a matching record, we set ID to the contents of CART 3) if still no ID, create a new record in the DB, and set the cookie to match First, we clear the two variables if they are undefined. if !defined(ID) then ID = ""; end if; if !defined(CART) then CART = ""; end if; Before we do any lookups with the ID, we need to make sure that they're coming from a page on our server (based on the http referer field), otherwise they're following a bookmark or link from somewhere else, and we need to clear the ID. if find(referer,"myserver.com") = 0 then // surfing in? Assume ID must be bad. ID = ""; end if; In the following section, "VSHIP" is the name of the Verona database, and "CART" is a field in that database. If there's no ID but the CART is set, then maybe we can link up if the shopper's old cart is still in the DB. if (ID = "") and (CART != "") then if vCountMatching("VSHIP","CART","=",CART) > 0 then ID = CART; end if; end if; If we didn't use a cookie or for other reasons the cart doesn't exist, then clear the ID and start fresh. if (ID != "") then if vCountMatching("VSHIP","CART","=",ID) = 0 then ID = ""; end if; end if; If there's no ID, then we need to create a new shopping cart. I open the Database for exclusive access (I'm paranoid), generate a long string of random digits, and verify its uniqueness (it's not gonna happen before the end of time, but as I said, I'm paranoid). if (ID = "") then acquireSem("CART"); repeat while ID = "" ID = "C" & random(1000,9999) & random(1000,9999) & random(1000,9999) & random(1000,9999); if vCountMatching("VSHIP","CART","=",ID) > 0 then ID = ""; end if; end repeat; Next, we initialize the VSHIP record to reserve the cart id (the actual record is longer than shown in this example). The vAddRecord command adds a record to the database, initializing the fields. Note the array structure of the declaration: this is a list of 3 elements, each containing 2 elements. vAddRecord("VSHIP", [["CART" ID] ["NAME" ""] ["MAIL" ""]]); Then, we try to set a cookie, named "CART", with contents based on the variable ID, valid for the domain "myserver.com", with a path of "/", to expire in about 8 days (700,000 seconds, in the proper format). setCookie("CART", ID, "myserver.com", "/", formatRFC1123(secsToTime(timeToSecs(now())+700000))); In the real system, some code to expire old carts would go here. But we'll leave that as an exercise for the reader for now. releaseSem("CART"); end if; At this point, we go back to normal HTML (with the ">>"). The ID variable will be available for use by any other WebSiphon code in the script. We can also embed SiphonScript expressions in the HTML using braces. >> Hi there! Your shopping cart ID is {ID}
Here is a link that encodes it : << Just for the heck of it, let's do this in WebSiphon. \ is the escape character. print "Click Here"; >>

And here is a form that does the same thing: