php 应用COOKIE建造浏览记录

    添加时间:2013-8-9 点击量:

    文件1 cookieHistory.class.php



    <?php
    
    /
    应用COOKIE 建造网站浏览记录
    by threemore
    /


    class HistoryCookie {
    var ¥times =; //记录COOKIE保存时候
    var ¥cookiename = History_cookie; //COOKIE名称
    var ¥counts = 5;

    function __construct(¥name=,¥times = ,¥counts) {
    if(!empty(¥times)) ¥this->times = time()+¥times;
    if(!empty(¥name)) ¥this->cookiename = ¥name;
    if(!empty(¥counts)) ¥this->counts = ¥counts;
    }

    //保存记录到COOKIE中
    public function getData(¥data) {
    ¥historydate = array();
    ¥historydate[] = ¥data;
    //unset(¥_COOKIE[¥this->cookiename]);
    if(isset(¥_COOKIE[¥this->cookiename])) {

    ¥new_history = stripslashes(¥_COOKIE[¥this->cookiename]);

    ¥new = unserialize(¥new_history);
    if(count(¥new) > (¥this->counts-1)) return unserialize(stripslashes(¥_COOKIE[¥this->cookiename]));
    foreach (¥new as ¥key => ¥value) {
    if(!in_array(¥value,¥historydate)) {
    ¥historydate[] =¥value;
    }
    }
    ¥savedate = serialize(¥historydate);
    setcookie(¥this->cookiename,¥savedate,time()+¥this->times);
    }else {
    ¥savedate= serialize(¥historydate);

    setcookie(¥this->cookiename,¥savedate,¥this->times);

    }
    return unserialize(stripslashes(¥_COOKIE[¥this->cookiename]));
    }

    //烧毁汗青记录
    public function Destroy() {
    unset(¥_COOKIE[¥this->cookiename]);
    }

    }

    ?>


    文件二 history.php



    <?php
    

    require_once cookieHistory.class.php;
    ob_start();//打开缓冲区
    ¥history = new HistoryCookie(cookiename,10000);



    ¥data[id] = ¥_GET[id];
    ¥data[name] = ¥_GET[name];



    ¥cookiedate = ¥history->getData(¥data);



    echo <pre>;
    print_r(¥cookiedate);
    ?>


      法度流程:



    1. 引入类库文件

    2. 初始化类库 (可以填写三个参数 以此是cookie的名称,保存的时候,保存到条数)

    3. 打开缓冲区 ob_start(); (也可以直接跳过,若是发明有报错就加上此代码,放置在文件的一开端)

    4. 接管须要保存的数据

    5. 保存之cookie中

    彼此相爱,却不要让爱成了束缚:不如让它成为涌动的大海,两岸乃是你们的灵魂。互斟满杯,却不要同饮一杯。相赠面包,却不要共食一个。一起歌舞欢喜,却依然各自独立,相互交心,却不是让对方收藏。因为唯有生命之手,方能收容你们的心。站在一起却不要过于靠近。—— 纪伯伦《先知》
    分享到: