【发布时间】: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 时也没有回声