如何记住上次访问的项目

时间:2015-02-12 15:58:13

标签: php arrays session queue

我一直在试验这段代码。 $ srv检查自己的地址。因此,如果我们在/_test.php?id=45中,代码将会执行。这是header.php>中的代码。 header.php包含在每个站点上。

if ( $srv == '/_test.php')
{   
    $aid = $_GET['id'];
    $numLastViewed = 5 ; 
    $lastViewedLocation = $_SESSION['lastViewed'];
    //check if first element of array exist
    if (isset($lastViewedLocation[0])) {
    //look for last set index
        for($i = 1;$i < $numLastViewed; ++$i){
            //when found > save it to var
            if(isset($lastViewedLocation[$i]))
            {
                $takenIndex = $i;                   
            }           
        }
        //check if array is full 
        if($takenIndex < $numLastViewed)
        {   //push element on end of array 
            array_push($lastViewedLocation, $aid);
            $_SESSION['lastViewed'] = $lastViewedLocation;
        }else
        {//delete first item and add current item to end of array
            $sub = array_shift($lastViewedLocation);
            array_push($lastViewedLocation, $aid);
            $_SESSION['lastViewed'] = $lastViewedLocation;
        }

    }else
    {   //if first element doesn't exist > save it to session
        array_push($lastViewedLocation, $aid);
        $_SESSION['lastViewed'] = $lastViewedLocation;  
    }       
}

在此之后我尝试将会话数组保存到变量并在其他文件的屏幕上打印。使用以下代码:

$lastViewedLocation = $_SESSION['lastViewed'];
            foreach($lastViewedLocation as $lastViewedAtt) {
            echo $lastViewedAtt;
            }

它不起作用。

1 个答案:

答案 0 :(得分:0)

我尝试用var_dump检查我的数组。它说它是NULL。所以在开头删除了if子句,因此代码肯定会执行,因此至少会在零索引上使用$ aid设置会话。但它仍然是NULL。

在这里我得出的结论是$ aid没有通过,所以我把代码从header.php转到_test.php文件,代码实际上在那里工作。但我做了一些调整&gt;当数组填充5个元素时,代码没有停止。所以我调整了一下。以下是工作版本。谢谢大家的意见。

                            if (isset($_SESSION['lastViewed'])) {
                                $lastViewedLocation = $_SESSION['lastViewed'];
                                $numLastViewed = 5;     
                                $takenIndex = count($lastViewedLocation);       
                                //check if array is full 
                                if($takenIndex < $numLastViewed)
                                {   //push element on end of array 
                                    array_push($lastViewedLocation, $aid);
                                    $_SESSION['lastViewed'] = $lastViewedLocation;
                                }else
                                {//delete first item and add current item to end of array
                                    $sub = array_shift($lastViewedLocation);
                                    array_push($lastViewedLocation, $aid);
                                    $_SESSION['lastViewed'] = $lastViewedLocation;
                                }

                            }else
                            {   //if first element doesn't exist > save it to session
                                $_SESSION['lastViewed']= array(0 => '$aid');

                            }

使用此代码数组就像具有给定限制的队列一样。元素逐个排队,直到数组中的元素数量达到限制。然后删除队列中的第一个元素,并将新元素添加到队列的末尾。这一直持续到会议被破坏为止。