Nacho37 has asked for the wisdom of the Perl Monks concerning the following question:
As a new initiate into the monestary, I feel somewhat timid in posting so soon, expecting knowledge so rapidly after giving little in return yet.. I hope that I may be favored with a reply...
I am writing a CGI mileage calculator for my employer between several fixed locations. Each time thru, the trip is given four variables to remember ($date, $source, $destination, $miles). These are stored in an array (@table), and output in an html table for the user to see. @table is stored in a cookie (trip_history), then recalled when the script is called again as the user re-submits and adds four new $date, $source, $destination, $miles. These four values are placed at the beginning of @table with unshift(). When the html table is regenerated again, the first value in the array $table(0) is the list stored in the cookie! I have the cookie formatted as a comma-delimited list, but the first value reads like "1, HHS, GHS, 6.7, 2, GHS, MHS, 4.2" instead of $table(0) being 1, $table(1)=HHS, $table(2)=GHS, and so on.
How do I put an array into a cookie, then retrieve it still as an array and modify it yet again and store it as a cookie and retrieve it again...?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
(Ovid) Re: Storing an array in a cookie...
by Ovid (Cardinal) on Feb 27, 2002 at 17:45 UTC | |
What you're trying to do is maintain state in an inherently stateless protocl (HTTP). To get around this, typically the user is given a a sessionID and this is tied to state information that is stored in a database. That's a more robust solution. For something quick 'n dirty, you could use Storable to serialize your data structure, store it in a cookie, and later retrieve it and deserialize it. Here's a small code sample to show you how this is done (minus using a cookie):
For the snippet above, you would store $serialized in the cookie. I've never actually tried this, but it should work. However, remember that this is not a good long-term solution. Amongst other things, cookies accept a max of 4K of data and will truncate anything over that. If you rely on this technique too much, you'll get bit. Further, this increases bandwidth (slightly) and setting too many cookies (by overusing this technique) will also cause problems. Good luck! Cheers, Join the Perlmonks Setiathome Group or just click on the the link and check out our stats. | [reply] [d/l] |
|
•Re: Storing an array in a cookie...
by merlyn (Sage) on Feb 27, 2002 at 17:49 UTC | |
You should read my ranting article about how to use cookies properly. | [reply] |
|
Re: Storing an array in a cookie...
by Speedy (Monk) on Feb 27, 2002 at 19:53 UTC | |
Firstly, depending on how you encode things, a user could read the cookie and actually change the results, adding more miles or more trips than were originally entered. If you don't have a record somewhere else, whom is to reliably say them nay? Secondly, as pointed out above, cookies are browser- and client-dependent. What do you plan if cookies are turned off, or the user enters the data at work one time and at home the second? Your script will not have access to both computers at once, so both for an audit trail and for data preservation, you must store the results in some structure at your Web server. Cookies are useful for quick identification and the display of more-or-less public information. But if money is riding on the data entry, you want to be more sure that the proper person is entering the data. If so, you may have to go to a password scheme or some other method of authentication before you actually allow a user to enter or view personal data. Consider how amazon.com works. You can see recommendations for purchases based on your cookie setting alone, but the minute you try to see information that should be private, like status of orders, entering an order, or even a history of past orders, you have to have a password to at least at some level verify you are who you say you are. If in fact cash payments (affecting business expenses and taxes) are involved, you will want a more robust security and storage solution. Live in the moment | [reply] |
|
Re: Storing an array in a cookie...
by drinkd (Pilgrim) on Feb 28, 2002 at 00:48 UTC | |
When I put something in the memory of my calculator on my desk, it is not a system-generated price, or secret, or whatever, but just a number I already typed in once and don't want to type in again (at least for the next few hours/days). I don't expect this memory to last forever, or get put in a database, or follow me to the calculator at home, I just want it to be there when I show up tomorrow morning without having to leave my browser on all night. It looks like what you are doing is just storing values already entered by the user so they don't have to type them in again. You don't care if the user edits the cookie, or if the values follow her to a different computer. Here is the code snippet I wrote for a type of internet calculator that uses a similar kluge to yours. A 2-D array of atoms with upper and lower limits is stored in a string using a space to delimit in one dimension and a . to delimit in the other dimension. The calculator will determine all molecular formulas containing these elements within these limits that have a given exact mass. The data can be slow to enter and successive forms will only require slight changes to the input data, so it will be kept in a cookie for 3 days so I can leave for the night without having to re-enter the array in the morning.
In case you're interested, this will test to see if the form has been submitted with new input data, failing that it will check for the data in a cookie, and failing that will set the cookie to a default value. I still haven't answered your question, but perhaps one of our more enlightened bretheren can shed some light on the matter (is storable the best way?). drinkd | [reply] [d/l] |
by Ryszard (Priest) on Feb 28, 2002 at 04:28 UTC | |
I especially disagree in this case becuase if the data in the cookie is used for cash reimbursement is it particularly poor design IMO. In my experience with end users, sales reps on the road etc are not particularly technical, and can do amazing things to their machines (deleting directories etc), hence losing their data and the cash they would have got from it. Having a persistant data store need not be difficult. It can be as easy as a flat file with DBI (or not) or be as complex as a many table, lots of data, normalised RDBMS information system. I'm on the wagon of using the cookie for a sess_id then cross referencing it to a user and associated data. ++ for being brave enuff to kick the wagon and offer an alternative backed up by argument. | [reply] |
by Nacho37 (Initiate) on Mar 04, 2002 at 22:16 UTC | |
Maybe someday I will incorporate authentication, stored databases of user mileage, higher security and the rest, but for now all I need to do is get the data to flow properly from query to query. | [reply] |
|
Re: Storing an array in a cookie...
by Nacho37 (Initiate) on Mar 05, 2002 at 22:31 UTC | |
Here is the code for the cgi program. Now, at the line separated by asterisks from the rest of the code, I have an error. My httpd error log gives the following explanation: "Can't use an undefined value as an ARRAY reference at /var/www/cgi-bin/perl/distance-test.cgi line 39." Ovid was kind enough to show me an explanation here once, but when I attempted to implement his suggestion, I am evidently short some info. I guess I need help using "thaw".
| [reply] [d/l] |