cookie array help!


#1

Ok guys, first off i’m fairly new to PHP so this might be a simple thing but I am having trouble figuring it out.

Basically, on the main page of my site I have a form that submits a variable ($ts) to another page on the site. The second page takes that variable and uses it to display data. What I am trying to do is set it up so that the page remembers the last 5 strings recieved from the $ts variable and display them in a “recently viewed” column. I am assuming I have to set up an array in the cookie to remember the last 5 variables but I am unsure how to set it up/retrieve the data from the cookie.

I’m just having trouble figuring out how to put this information into an array in a cookie without overwriting the previous entry. I would like it to start overwriting the oldest entry after the 5 entries are filled up and then recall the entries from newest to oldest.

Any help would be appreciated.

TIA


#2

No need to worry about cookies, sessions are there to help you.

[php]
session_start(); // Before any output in your code

$_SESSION[‘recent’] = array( ‘foo’, ‘bar’ );
[/php]

All you have to do is modify the array and values will be kept between requests. A single ID cookie is used to track the client, but all this is transparent to you. The data of the session itself is stored in text files on the server (by default, it can be changed). In a typical application, you probably want to keep the amount of data in there to low volumes.


#3

Ok but how would I make write to the next string in the array? The form returns the same variable each time it is submitted and I want it to remember the last 5 entries that were submitted in that form.

Also, my preference was to cookies because I want them to be remembered for a week, and my understanding is that sessions are erased once the viewer leaves the site.


#4

You can extend the session lifetime using some obscur function. The problem with cookies is that they are sent on every single request.

You can use array_push() and array_shift() to add and remove items from the array, just like any other array.