store whole $visited array into the session, here is what i mean:
//get the `visited` (array) from session, or return default empty array, if it doesnt exist
$visited = request()->session()->pull('visited', []);
//dump it, so you can see it
dump($visited);
//push your visited product to the end of the array (just random int in our example)
array_push($visited, rand(1, 1000));
//get the last 5 items of the end of the array
$visited = array_slice($visited, -5);
//save the updated $visited array to the session
session(['visited' => $visited]);