Admittedly, it had been a while since I had worked on this one particular app. Several years ago, I was presented with a client that needed an application to handle orders for their route salespeople. In the heart of North Carolina, this company wanted their route drivers to be carrying iPads into stores to enter their orders. A lot less mess and paperwork that would have to be handled, stored, etc. Actually it was a great idea.The owner of the company is a rather forward thinking man and saw this to be a great cost saving measure. Heck, they’ve probably saved MORE than the cost of the application from just not having to purchase all those triplicate tickets/order pads. Enough said. The kicker for the project is that it needed to be able to stash a lot of values from a web browser interface.
They contacted me a couple months ago about needing a fix or two so they could download each of the route driver data pieces separately as opposed to a large bulk download of all orders at the end of the day. They have multiple drivers with each of them finishing at various times throughout the day so they wanted to be able to process their driver earlier if he finished earlier.
I had been using Session values to pull off the data insertions and stash values in the database. Session vals to me are somewhat easier to work with as you can write them at any time – which is not the same with cookie values. Cookie vals have to be written before any other data is output to the browser. That can be all fine and dandy, but when I created this app, I had originally had envisioned a simple app (so I was told) to just enter values. No what they really wanted was a full-blown inventory and ordering system. When one goes from one page to the next in a linear fashion, it’s easy to predict where things can and will happen. When you have something more intricate, more variables need to taken into account. This was one of them.
I used my existing code to use the session values when doing the data insert, but got a call that things were breaking on occasion. That is something I hate to hear – “on occasion”. Difficult to troubleshoot “on occasion” problems. I went into the SQL database and did a quick query through the sql manager interface and sure as shooting, some of the values that had been stashed in session values had been dropped. With a tablet, I have discovered that they tend to NOT login and log out. They just close the tablet which can lead to a session value being dropped. They still can input a value, but when the value is called from session (and it’s not there), a NULL gets inserted. Cookies to the rescue.
Getting and Setting a cookie
Setting your classic asp Cookie
We are just going to write a single value cookie in this example and then add an expiration on it. I’m just setting this one to expire basically at midnight on a daily basis.
Response.cookies("yourcookieid") = "yourvalue" Response.cookies("yourcookieid").expires = DateAdd( "d", 1, Date )
Getting your classic asp Cookie value
myVal = Request.cookies("yourcookieid")
Getting fancy with The Big Cookie
So now let’s say that you have a number of values and you want to keep it sort of simple by grouping cookie vals. As opposed to having 25 different cookie values down the line on the same level, let’s say you actually want to group them out just a bit. Being a programmer at heart, I like nice tidy things. Maybe we have user information and route information (similar to the above project)
Response.cookies("uservals")("username") = "someusername" Response.cookies("uservals")("firstName") = "someFirstName" Response.cookies("uservals")("lastName") = "someLastName" Response.cookies("routeVals")("routeNum") = "routeNumberValue" Response.cookies("routeVals")("routeTruck") = "TruckID"
Now you can get them out the more traditional way as in:
thisUser = Request.cookies("uservals")("username")
but this will bring them all out quickly…
dim x,y for each x in Request.Cookies response.write("<p>") if Request.Cookies(x).HasKeys then for each y in Request.Cookies(x) response.write(x & ":" & y & "=" & Request.Cookies(x)(y)) response.write("<br>") next else Response.Write(x & "=" & Request.Cookies(x) & "<br>") end if response.write "</p>" next
Use a standard function/subroutine where it makes sense
When you start getting into serious cookie storage and retrieval, I highly recommend subroutines or functions in an include file to grab those vals. In my testing, I have a specific subroutine that I use to “showCookies”. There are also Firefox addons that will allow you to see cookie values at any particular time as well. Would be great if it were a live thing, but the one that I have used most recently just requires a “Show page info” and you can check out the cookie value.
As far as improving cookie reading and code execution time, a “getCookieVals” routine right off the bat on page load that grabs all the cookie values and assigns them to the variables that you want to be available throughout a page. It’s considerably faster to grab your data right off the bat, assign it to vals and then use THOSE in your code than it is to call for each cookie value as needed.
Cookies MUST Be Written Before any other output
As mentioned above, the one big thing to remember is that a cookie value must be pushed out and written before any other data is sent to browser, thus it requires a page change. Session values do NOT have this problem and can be written/set at any point in a page execution.
Why was this an issue for me? JSON. More on that in another article. Classic ASP and JSON. Eeeesh.