I have a simple function that do this:
1) User view some image and image_id is stored in session array
2) Then user can see the last 10 viewed images
Currently my function is this:
function lastSeen($image_id){
if(!isset($_SESSION['lastSeen'])){
$_SESSION['lastSeen'] = array($image_id);
}else{
$tmpSession = array_unique($_SESSION['lastSeen']);
if(count($tmpSession) > 9){
$tmpSession = array_slice($tmpSession,1);
}
$tmpSession[] = $image_id;
$_SESSION['lastSeen'] = array_unique($tmpSession);
}
return true;
}
This function works but the problem is that, if a user view one image more than one time, then in session are saved only 9 items.
Can anybody help me solve this problem? Maybe the whole function needs to be rewrite it…
You probably want to remove extraneous entries as the very last thing, and also only if it’s more than 10 entries: