Creating Recently Viewed Items List And Wishlist

Hello, I found a post on this topic for ASP but I couldn’t find out to do this in PHP. I would like to be able to display a list of the five most recently viewed items for each customer on my site. How could I go about doing this? Would I need to create a separate table in the MySQL database for each visitor? How could I code it to store the URL and title for each page that is viewed?

Also, I would like to create a wishlist feature. Would I also need to create a new table in MySQL for each visitor to accomplish this?

I am relatively new to PHP (and blonde), so please speak S-L-O-W-L-Y, Thanks!

Have a great day

Is this for a website you have built, or are you using a CMS (WordPress, Mambo etc.)?


This is for a site that I am building. It is PHP/MySQL. It is a fairly simple site that does price comparison shopping on a small amount of items. If the visitor first looks at Product A, then goes to Product B, I would like to show a area that says,

Your Most Recently Viewed Items:
Product A(including its link)

and so on, up to 4 or 5 items that were viewed.

As for the wishlist, I just need it to allow the customer to add to or remove items from their wishlist as they choose.

I would eventually love to be able to do something like is done on the right at


WARNING: untested code…
This will only set the last viewed product once, but you could fiddle around with this to spit out an array, limited to, say 5 recent items.

  // I would actually write a class to deal with this,
 // but these functions will hopefully give you an idea or two :-)
  function setRecentlyViewed ( $item, $url ) {
  	$_SESSION['items'] = array( $item, $url );
  function getRecentlyViewed () {
  	$str = '<h2>Your Most Recently Viewed Item:</h2>';
  	if ( $_SESSION['items'] > 0 ) {
  		$str .= '<ul>';
  		$str .= '<li><a href="'.$_SESSION['items'][1].'">'.$_SESSION['items'][0].'</a></li>';
  		$str .= '</ul>';
  	} else {
  		$str .= 'Nothing selected yet!';
  	return $str;
  // on each product page, set "setRecentlyViewed" like so:
  setRecentlyViewed( $row['itemName'], htmlspecialchars($_SERVER['REQUEST_URI']) );
  // Display the results like so
  echo getRecentlyViewed();


Gee, thanks Poncho for taking the time to write that, I really do appreciate it. Hopefully, I will be able to get it to work!

Thanks again