如何记住上次访问的项目
How to remember last visited items
我一直在试验这段代码。 $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;
}
没用。
我尝试用 var_dump 检查我的数组。据说它是NULL。所以在开始时删除了 if 子句,这样代码肯定会执行,所以至少会话将在零索引上设置 $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');
}
使用此代码,数组就像具有给定限制的队列。元素一个一个地进入队列,直到数组中的元素数量达到限制。然后删除队列中的第一个元素,并将新元素添加到队列的末尾。这一直持续到会话被销毁。
我一直在试验这段代码。 $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;
}
没用。
我尝试用 var_dump 检查我的数组。据说它是NULL。所以在开始时删除了 if 子句,这样代码肯定会执行,所以至少会话将在零索引上设置 $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');
}
使用此代码,数组就像具有给定限制的队列。元素一个一个地进入队列,直到数组中的元素数量达到限制。然后删除队列中的第一个元素,并将新元素添加到队列的末尾。这一直持续到会话被销毁。