Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9229603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:36:46+00:00 2026-06-18T05:36:46+00:00

I am trying to create a recently viewed feature to a website. The idea

  • 0

I am trying to create a recently viewed feature to a website. The idea is that you have a box in the right nav that shows your last 3 products viewed. You don’t need to be logged it and it’s no problem if the user clears cookies, it just starts over.

From what I’ve researched, the best way to do this is through a cookie array (as opposed to setting 5 cookies or keep adding cookies or doing something in mysql).

I’m having 2 problems:

  1. First, the array keeps adding values, I want it to cap it at 3 values and from there drop the oldest, then add the newest. So, if you visited 7 product page IDs in this order: 100,200,300,400,500,600,150, the cookie should store the values (500,600,150). The first is the oldest of the 3, the last is the newest.
  2. Second, I’m unclear of how to extract the array into something usable. The array is of ID numbers that I guess I need to query against the DB.

When I put this on the page:

COOKIE:  <?php echo $cookie; ?>

I get this:

COOKIE: a:7:i:0;s:3:"100";i:1;s:3:"200";i:2;s:3:"300";i:3;s:3:"400";i:4;s:3:"500";i:5;s:3:"600";i:6;s:3:"150";}

This is my code:

//set product id
$product_id = [//some stuff here sets the product id]
// if the cookie exists, read it and unserialize it. If not, create a blank array
if(array_key_exists('recentviews', $_COOKIE)) {
    $cookie = $_COOKIE['recentviews'];
    $cookie = unserialize($cookie);
} else {
    $cookie = array();
}

// add the value to the array and serialize
$cookie[] = $product_id;
$cookie = serialize($cookie);

// save the cookie
setcookie('recentviews', $cookie, time()+3600);

How do I first get the cookie to hold 3 values and drop the oldest?
What is the best way to extract those IDs into something I can put into a query?….str_replace?

This brings up another question, which is should I just put the URL, anchor text, and a couple of attributes of the product into the cookie and not look it up with php/mysql?

As always, thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:36:48+00:00Added an answer on June 18, 2026 at 5:36 am

    Here was the answer I ended up figuring out myssef:

    // if the cookie exists, read it and unserialize it. If not, create a blank array
        if(array_key_exists('recentviews', $_COOKIE)) {
            $cookie = $_COOKIE['recentviews'];
            $cookie = unserialize($cookie);
                } else {$cookie = array();}
    
        // grab the values from the original array to use as needed
        $recent3 = $cookie[0];
        $recent2 = $cookie[1];
        $recent1 = $cookie[2];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently i've been trying to create a wait function that waits for 25 ms
I have recently been trying to create an updater for my program. The updater
Recently, I have been trying to accomplish creating a full-width jQuery slider that would
I have recently being trying to create a project which has several levels of
recently I been trying to create an android app that uses JSON Objects to
I've recently been trying to learn me a Haskell, and I'd like to create
I'm trying to create some unit tests for an application I've recently inherited. Currently
I think I may be trying something that isn't possible. I was recently tasked
Just a small Prototypical Inheritance question Recently I was trying to create a custom
I'm trying to create a Chrome extension that will pop up a new window

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.