Setting cookies

// Store cookie with length of time
setcookie( 'recently_viewed',implode( ',', $viewed_products ), time()+(86400 * 30), '/' );
//Run query to get records for recently viewed products and exclude current viewed item
$listOfViewed = implode( ',',$viewed_products);
$query = "SELECT `prod_id` , `prod_model`, `prod_mainImage`, `prod_name`
FROM products WHERE `prod_id` IN ($listOfViewed) AND `prod_id` != '{$currentProduct}' ";
$recentProducts = $wpdb->get_results($query, ARRAY_A);
//Create output for list of recent products. I have also included the image.
$output = "";
foreach($recentProducts as $prod)
{
$imgSrc = "/thumbnails/{$prod['prod_mainImage']}'";
$output .= "
$output .= "<img title="{$prod[" src="{$imgSrc}" alt="{$_name']}";
$output .= "{$prod['prod_name']}";
$output .= "{$prod['prod_model']}";
}

Now we output the content. You can add DIVs and other HTML markup to display the items. And that is how you can use cookies to display recently viewed items or products on your site.

echo "Recently Viewed"; 
{$output}";
}