溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

php如何實(shí)現(xiàn)瀏覽記錄

發(fā)布時(shí)間:2021-11-02 10:03:23 來(lái)源:億速云 閱讀:101 作者:iii 欄目:編程語(yǔ)言

本篇內(nèi)容介紹了“php如何實(shí)現(xiàn)瀏覽記錄”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

php實(shí)現(xiàn)瀏覽記錄的方法:1、設(shè)置初始數(shù)據(jù);2、獲取cookie記錄;3、判斷瀏覽記錄是否存在;4、將瀏覽數(shù)組序列化后寫(xiě)入cookie;5、讀取cookie記錄即可。

php如何實(shí)現(xiàn)瀏覽記錄

本文操作環(huán)境:Windows7系統(tǒng)、PHP7.1版、DELL G3電腦

php怎么實(shí)現(xiàn)瀏覽記錄?

php實(shí)現(xiàn)歷史瀏覽記錄

其實(shí)原理很簡(jiǎn)單,就是利用cookie,實(shí)現(xiàn)記錄,其中需要注意的點(diǎn)就是,設(shè)置一下,你需要保存的cookie長(zhǎng)度,記錄時(shí)間,下面是ci框架的基本實(shí)現(xiàn)

如有更加好的思路實(shí)現(xiàn),歡迎評(píng)論討論。

/**
 * @desc    設(shè)置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){
 
    //設(shè)置初始數(shù)據(jù)
    $set_limit = 5; //瀏覽記錄的容量限制
 
    //初始數(shù)據(jù)過(guò)濾
    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);//重新放在第一個(gè)
 
    //瀏覽記錄不存在
    }else{
 
        //沒(méi)有超過(guò)記錄的容量限制,直接放在第一個(gè)
        if(count($history_array)<$set_limit){
            array_unshift($history_array,$string);
 
        //超過(guò)記錄的容量限制,刪除最后一個(gè),然后放在第一個(gè)
        }else{
            array_pop($history_array);
            array_unshift($history_array,$string);
        }
    }
 
    //將瀏覽數(shù)組序列化后寫(xiě)入cookie
    $expire_time = 3600 * 24 * 30; //過(guò)期時(shí)間
    $cookie_domain = $this->config->item('cookie_domain');
    $history_array = serialize($history_array);
    setcookie('cookie_history', $history_array, time()+$expire_time, '/', $cookie_domain);
 
}

  上面是實(shí)現(xiàn)cookie的記錄功能,下面是進(jìn)行讀取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(){
    //設(shè)置初始返回?cái)?shù)據(jù)
    $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]){
                //這里寫(xiě),你要查詢的sql語(yǔ)句
                $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]){
                //這里寫(xiě),你要查詢的sql語(yǔ)句
                $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;
    }
 
    //非法獲取數(shù)據(jù),直接返回
    return $return_data;
}

“php如何實(shí)現(xiàn)瀏覽記錄”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI