PHP将更新的数组添加到当前会话

时间:2014-04-23 08:55:42

标签: php arrays session

好的,所以我的目标是将当前的配置文件ID($ for)添加到我的会话中,目的是让系统了解在当前会话中查看了哪些配置文件,从而添加到数组中。然后它将检查数组是否存在,并且不会在加载时显示弹出窗口,因为这只需要发生一次。

$for = $_GET['id'];

//GET PREVIOUSLY VIEWED IDS
$prevViewed = $_SESSION['viewedID'];

//THEN ADD THIS PAGE ID TO PREVIOUSLY VIEWED ARRAY
$prevViewed = $for;

//UPDATE SESSION WITH NEW ARRAY INC CURRENT PAGE
$_SESSION['viewedID'] = $prevViewed;

print_r($_SESSION['viewedID']);

2 个答案:

答案 0 :(得分:0)

这样的事情怎么样:

// Make sure that SESSION has the viewedID array
if (!isset($_SESSION['viewedID'])) $_SESSION['viewedID'] = array();

// Store this page in the array
$_SESSION['viewedID'][$for] = true;

然后,您可以检查此页面是否已被查看:

if (isset($_SESSION['viewedID'][$for]))
// it has been viewed
else
// it hasn't

答案 1 :(得分:0)

我喜欢它,看起来它会成为一个梦想。但是,我现在已经解决了这个问题:

将已查看过的个人资料添加到数组...

//ADD THIS PAGE ID TO PREVIOUSLY VIEWED ARRAY
$prevViewed = $for;

//UPDATE SESSION WITH NEW ARRAY INC CURRENT PAGE
$_SESSION['viewedID'][] = $prevViewed;

检查阵列中的当前配置文件ID ...

//CHECK IF CURRENT ID IS NOT IN ARRAY
if(isset($_SESSION['viewedID'])) {

    if (!in_array($for, $_SESSION['viewedID'])) { 
      //PERFORM ACTION IF NOT IN ARRAY
    } else {
      //PERFORM ACTION IF IS IN ARRAY
    }

}

希望这有助于某人。