如何在PHP中以cookie存储上次访问过的产品页面

时间:2017-11-22 10:07:26

标签: php session-cookies

// define the new value to add to the cookie
$ad_name = $myProductId; //comes from $_GET[]

// if the cookie exists, read it and unserialize it. If not, create a blank array
if(array_key_exists('recentviews', $_COOKIE)) {
    $cookie = $_COOKIE['recentviews'];
    $cookie = unserialize($cookie);
} else {
    $cookie = array();
}

// add the value to the array and serialize
$cookie[] = $ad_name;
$cookieString = serialize($cookie); /* changed $cookie to $cookieString */

// save the cookie
setcookie('recentviews', $cookieString , time()+3600); /* insert $cookiestring */

//prints to screen noting
foreach ($cookie as $h) { /* changed $_COOKIE['recentviews'] to $cookie */
        echo $h."-";

    }

单击查看其他产品或网址导航时,此代码无效,旧的Cookie值已被新的替换。我想保存旧值并检查是否会保存副本

Picture 1

Picture 2

0 个答案:

没有答案