简体   繁体   中英

PHP: Store last viewed items in session array

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...

If you wouldn't mind that the order of images doesn't change, when the user views one image twice, then I would go with this simple solution. Just add line like this:

if (in_array($image_id, $tmpSession)) return true;

So your function would look like this:

function lastSeen($image_id){
    if(!isset($_SESSION['lastSeen'])){
        $_SESSION['lastSeen'] = array($image_id);
    } elseif (in_array($image_id, $_SESSION['lastSeen'])) {
       return true;
    } 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;
}

You probably want to remove extraneous entries as the very last thing, and also only if it's more than 10 entries:

else {
   $tmpSession = $_SESSION['lastSeen'];
   $tmpSession[] = $image_id;  
   $tmpSession = array_unique($tmpSession);

   if(count($tmpSession) > 10){
       $tmpSession = array_slice($tmpSession,1);
   }
   $_SESSION['lastSeen'] = $tmpSession;
}

Another implementation of your function could be:

function lastSeen($image_id){
     if(!isset($_SESSION['lastSeen']) || !is_array($_SESSION['lastSeen'])){
          $_SESSION['lastSeen'] = array();
     }

     if(!in_array($image_id, $_SESSION['lastSeen']){
          array_push($_SESSION['lastSeen'], $image_id);
     }

     if(sizeof($_SESSION['lastSeen']) > 10){
          array_shift($_SESSION['lastSeen']);
     }
}

If you want a function that stores the last visited image as the last item in the array, then just replace:

$tmpSession = array_slice($tmpSession,1);

with the following line:

if (in_array($image_id,$tmpSession))
    array_splice($tmpSession, array_search($image_id, $tmpSession), 1);
else
    $tmpSession = array_slice($tmpSession, 1);

The result function would look like:

function lastSeen($image_id) {
if (!isset($_SESSION['lastSeen'])) {
    $_SESSION['lastSeen'] = array($image_id);
} else {
    $tmpSession = array_unique($_SESSION['lastSeen']);
    if (count($tmpSession) > 9){
        if (in_array($image_id,$tmpSession))
            array_splice($tmpSession, array_search($image_id, $tmpSession), 1);
        else
            $tmpSession = array_slice($tmpSession, 1);
    }
    $tmpSession[] = $image_id;
    $_SESSION['lastSeen'] = array_unique($tmpSession);
}
return true;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM