For this project I wanted to store lots of values in cookies. I wrote my code and started testing. When I tried testing with lots of entries to be saved I found that I was losing values. It seems that you can only store about twenty different values in cookies and once you go past that the oldest ones are automatically removed. I solved this by coming up with a way to store arrays of values.
In trying to text the page on various browsers on my own computer, I noticed that while the code worked okay on Internet Explorer and Netscape (both 4 and 6) that it didn't seem to work on Opera. Fortunately, I have a web server installed on my own computer and when I accessed the page via the server instead of directly, the cookies did work after all.
The first step is to link the script into your page by placing the following code in the head section of your page:
The page now has access to use all of the functions within the cookie toolbox. Let's start with the basic cookie functions.
set_cookie(name, value, expires);
This function allows you to create a cookie called name and store value in it. All cookies have an expiry date to stop them hanging around forever. You specify the expiry date for this cookie in the expires field. If you don't specify an expiry date then the function will use the current date and time and the cookie will expire at the end of your visitor's browser session.
value = get_cookie(name);
This function allows you to retrieve the content of a previously saved cookie called name into a field called value.
This function deletes the cookie called name.
As I mentioned at the start of this discussion, I came up with a way of storing arrays in cookies and the following functions will allow you to do the same.
myarray = init_array();
This initializes an array called myarray that will be used with the cookie array functions. You will load the values that you want to save in your cookie into this array before saving the cookie and when you retrieve the cookie the array will be cleared and then loaded with the values from the cookie. Provided that you are only dealing with one cookie at a time, you will only need one array as it can be reused. Note that you need to initialize your array before you call any of the following functions.
set_array(name, myarray, expires);
This function works the same as the set_cookie function except that it first converts the array into a single value field before storing it in the cookie. Note that this function uses the caret (^) as a field separator in this conversion so you will need to ensure that your array entries do not contain that character.
This function will retrieve the cookie called name then convert the value field back into the array myarray.
del_entry(name, myarray, pos, expires);
This function allows you to delete an array entry from within the cookie called name. The content of the cookie will be retrieved into myarray and then the cookie will be recreated without the pos entry. A new expiry date of expires will be set.
size = next_entry(myarray);
The array that the toolbox uses dynamically grows and shrinks to meet your current requirements. This function lets you know that the myarray[size] entry is the first empty entry. That's the one you'll want to use when adding more entries into the array.
You can use this to turn the alerts off again at the end of the section of code that you are debugging so that subsequent calls to the cookie functions will no longer produce alerts.
This function can be used to place a raw copy of all of your cookies at the current location on your page so that you can see exactly what you currently have stored in cookies. This function can only be used in the body section of your page.
So there you are, a complete set of cookie functions that you can use with your site. If you require more detailed instructions on how to use the cookie toolbox then read Using the Cookie Toolbox.
This article written by Stephen Chapman, Felgall Pty Ltd.