thinkphp瀏覽曆史功能實現方法_php執行個體

來源:互聯網
上載者:User

本文執行個體講述了thinkphp瀏覽曆史功能實現方法,分享給大家供大家參考。具體實現方法分析如下:

曆史瀏覽功能都是使用了cookie功能記錄使用者資訊放到了本地了,這樣我們只要讀取儲存在cookies中的值就可以了,下面來給大家介紹一個基於thinkphp 實現瀏覽曆史功能例子。

就像瀏覽器一樣,能夠記錄訪問了哪些頁面,這樣能夠減少時間,下面我們實現瀏覽曆史的功能。

1.在你需要記錄瀏覽資料的產品或新聞頁面,記錄cookie需要儲存的資訊,例如下面這行代碼,把頁面ID,產品名稱,價格,縮圖,網址傳給cookie_history。

複製代碼 代碼如下:
cookie_history($id,$info['title'],$info['price'],$info['pic'],$thisurl);

2.function.php 裡面添加代碼

複製代碼 代碼如下:
/**
  +----------------------------------------------------------
 * 瀏覽記錄按照時間排序
  +----------------------------------------------------------
 */
function my_sort($a, $b){
$a = substr($a,1);
$b = substr($b,1);
if ($a == $b) return 0;
return ($a > $b) ? -1 : 1;
  }
/**
  +----------------------------------------------------------
 * 網頁瀏覽記錄產生
  +----------------------------------------------------------
 */
function cookie_history($id,$title,$price,$img,$url){
$dealinfo['title'] = $title;
$dealinfo['price'] = $price;
$dealinfo['img'] = $img;
$dealinfo['url'] = $url;
$time = 't'.NOW_TIME;
$cookie_history = array($time => json_encode($dealinfo));  //設定cookie
if (!cookie('history')){//cookie空,初始一個
cookie('history',$cookie_history);
}else{
$new_history = array_merge(cookie('history'),$cookie_history);//添加新瀏覽資料
uksort($new_history, "my_sort");//按照瀏覽時間排序
$history = array_unique($new_history);
if (count($history) > 4){
$history = array_slice($history,0,4);
}
cookie('history',$history);
}
}
/**
  +----------------------------------------------------------
 * 網頁瀏覽記錄讀取
  +----------------------------------------------------------
 */
function cookie_history_read(){
$arr = cookie('history');
foreach ((array)$arr as $k => $v){
$list[$k] = json_decode($v,true);
}
return $list;
}

3.在需要顯示瀏覽記錄的頁面輸出資訊

複製代碼 代碼如下:
$this->assign('history',cookie_history_read());

模板裡面用volist顯示出來就行了。

希望本文所述對大家的PHP程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.