php實現歷史瀏覽記錄


其實原理很簡單,就是利用cookie,實現記錄,其中需要注意的點就是,設置一下,你需要保存的cookie長度,記錄時間,下面是ci框架的基本實現

如有更加好的思路實現,歡迎qq(1245049149)聯系。

    /**
     * @desc    設置cookie瀏覽記錄
     * @date    2018-04-15 16:48:22
     * @param   [string $type記錄瀏覽類型【as 查看記錄表1;ps 查看記錄表2】;int $id主鍵id]
     * @author  1245049149@qq.com
     * @return  [type]
     */
    public function set_cookie_history($type,$id){

        //設置初始數據
        $set_limit = 5; //瀏覽記錄的容量限制

        //初始數據過濾
        if(!in_array($type,['as','ps'])){
            return false;
        }

        //獲取cookie記錄
        $string = $type.$id;
        $history_array = unserialize($_COOKIE['cookie_history']);
        if(!$history_array)
            $history_array = [];

        //瀏覽記錄存在
        if(in_array($string,$history_array)){
            unset($history_array[array_search($string , $history_array)]); //刪除存在
            array_unshift($history_array,$string);//重新放在第一個

        //瀏覽記錄不存在
        }else{

            //沒有超過記錄的容量限制,直接放在第一個
            if(count($history_array)<$set_limit){
                array_unshift($history_array,$string);

            //超過記錄的容量限制,刪除最后一個,然后放在第一個
            }else{
                array_pop($history_array);
                array_unshift($history_array,$string);
            }
        }

        //將瀏覽數組序列化后寫入cookie
        $expire_time = 3600 * 24 * 30; //過期時間
        $cookie_domain = $this->config->item('cookie_domain');
        $history_array = serialize($history_array);
        setcookie('cookie_history', $history_array, time()+$expire_time, '/', $cookie_domain);

    }

  上面是實現cookie的記錄功能,下面是進行讀取cookie記錄方法:

    /**
     * @desc    獲取cookie瀏覽記錄
     * @date    2018-04-15 17:42:51
     * @param   [type]
     * @author  1245049149@qq.com
     * @return  [array $return_data]
     */
    public function get_cookie_history(){
        //設置初始返回數據
        $return_data = [];

        //獲取cookie記錄
        $history_array = unserialize($_COOKIE['cookie_history']);
        if(!$history_array)
            return $return_data;

        if($history_array){
            foreach($history_array as $k=>$v){

                //切割判斷是否是as類型
                $as_temp = explode('as',$v);
                if($as_temp && $as_temp[1]){
                    //這里寫,你要查詢的sql語句
                    $sql = "select field1,field2 from table_test1 where id={$as_temp[1]}";
                    $res = $this->db->query($sql)->row_array();
                    if($res)
                        $return_data[] = ['type' => 'as','data' => $res];
                }

                //切割判斷是否是ps類型
                $ps_temp = explode('ps',$v);
                if($ps_temp && $ps_temp[1]){
                    //這里寫,你要查詢的sql語句
                    $sql = "select field1,field2 from table_test2 where id={$as_temp[1]}";
                    $res = $this->db->query($sql)->row_array();
                    if($res)
                        $return_data[] = ['type' => 'ps','data' => $res];
                }
            }
            return $return_data;
        }

        //非法獲取數據,直接返回
        return $return_data;
    }

  歡迎留言討論


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM