php cookie使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36607076/article/details/81300679

php cookie使用

1.存cookie

$recentDisplay = isset($_COOKIE['recentDisplay']) ? unserialize($_COOKIE['recentDisplay']) : array();
        // 把刚刚浏览的这件商品的ID放到这个数组中的最前面
        array_unshift($recentDisplay, $this->param['goods_id']);
        // 去复
        $recentDisplay = array_unique($recentDisplay);
        // 如果超过12个就只保留前12个
        if(count($recentDisplay) > 12){
            $recentDisplay = array_slice($recentDisplay, 0, 12);
        }
        // 把处理好的数组保存回COOKIE中
        $aMonth = 30 * 86400;
        $data = serialize($recentDisplay);
        setcookie('recentDisplay', $data, time() + $aMonth, '/');
        var_dump($recentDisplay);
    2.获取cookie
 $recentDisplay = isset($_COOKIE['recentDisplay']) ? unserialize($_COOKIE['recentDisplay']) : array();
 //对$recentDisplay进行判断,再进行后面的逻辑编写

猜你喜欢

转载自blog.csdn.net/qq_36607076/article/details/81300679