【问题标题】:How to remember last visited items如何记住上次访问的项目
【发布时间】:2015-02-12 15:58:13
【问题描述】:

我一直在试验这段代码。 $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;
            }

它不起作用。

【问题讨论】:

  • 你可能没有在任何地方打电话给session_start()。你需要在每个脚本中使用 $_SESSION...
  • 哦,我在每个文件的开头都知道了..每个文件都包含标题,所以它也会影响标题
  • 那你应该解释一下如何这不起作用?
  • 什么不起作用?错误是什么?
  • 开始时没有回声,当我访问几个不同的 id 时也没有回声

标签: php arrays session queue


【解决方案1】:

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

在这里我得出的结论是 $aid 没有通过,所以我将代码从 header.php 到 _test.php 文件,代码实际工作的地方。但是我做了一些调整 > 当数组填充了 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');

                            }

使用此代码数组就像具有给定限制的队列一样。元素一个一个地进入队列,直到数组中的元素数量达到限制。然后队列中的第一个元素被删除,新元素被添加到队列的末尾。这一直持续到会话被销毁。

【讨论】:

    猜你喜欢
    • 2023-03-08
    • 2020-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多