标签:io 使用 ar for 数据 art 问题 cti on
使用cookie
function _setHistory($data)   
{    
    if(!$data || !is_array($data))    
    {    
        return false;    
    }    
    //判断cookie类里面是否有浏览记录    
    if($_COOKIE["_productHistory"])    
    {    
        $history = unserialize(stripslashes($_COOKIE[‘_productHistory‘])); 
        array_unshift($history, $data); //在浏览记录顶部加入   
        /* 去除重复记录 */    
        $rows = array();    
        foreach ($history as $v)    
        {    
            if(in_array($v, $rows))    
            {    
                continue;    
            }    
            $rows[] = $v;    
        }    
        /* 如果记录数量多余5则去除 */    
        while (count($rows) > 6)    
        {    
            array_pop($rows); //弹出    
        }    
        setcookie(‘_productHistory‘,serialize($rows),time() + 3600 * 24 * 30,‘/‘);    
    }    
    else    
    {    
        $history = array($data);    
        setcookie(‘_productHistory‘,$history,time() + 3600 * 24 * 30,‘/‘);    
    } 
    return ;   
}
本人是经过了很长时间搞定的,开始在window下没问题,到linux环境就不能反序列化,最后才发现是有特殊字符导致的。
注:   
serialize()–将数组转换为字符串,并保持数据结构不变    
addslashes()–函数在指定的预定义字符前添加反斜杠。这些预定义字符是:    
单引号 (‘)    
双引号 (“)    
反斜杠 (\)    
NULL    
stripslashes() 函数是 addslashes() 的反向操作,即:删除由 addslashes() 函数添加的反斜杠。    
unserialize()  对单一的已序列化的变量进行操作,将其转换回 PHP 的值。返回的是转换之后的值,可为 integer、float、string、array 或 object。如果传递的字符串不可解序列化,则返回 FALSE。
$_COOKIE[‘DC_ORDERS‘]=serialize($_SESSION[‘dingcan_ShoppingCart_data‘]);
$cookie_info=unserialize(stripslashes($_COOKIE[‘DC_ORDERS‘]));
标签:io 使用 ar for 数据 art 问题 cti on
原文地址:http://www.cnblogs.com/meetcomet/p/3973169.html