Just like a browser, it can record the pages accessed, which can reduce the time. Next we will implement the browsing history function.
1. on the product or news page where you want to record the data browsing, record the information that the cookie needs to save, such as the following line of code, and pass the page ID, product name, price, thumbnail, and URL to cookie_history.
The code is as follows: |
Copy code |
Cookie_history ($ id, $ info ['title'], $ info ['price'], $ info ['Pic '], $ thisurl ); |
2. Add code in function. php
The code is as follows: |
Copy code |
/** + ---------------------------------------------------------- * Browsing records are sorted by time + ---------------------------------------------------------- */ Function my_sort ($ a, $ B ){ $ A = substr ($ a, 1 ); $ B = substr ($ B, 1 ); If ($ a = $ B) return 0; Return ($ a> $ B )? -1: 1; } /** + ---------------------------------------------------------- * Generate web browsing records + ---------------------------------------------------------- */ 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); // Set cookie If (! Cookie ('History ') {// The cookie is empty. Cookie ('History ', $ cookie_history ); } Else { $ New_history = array_merge (cookie ('History '), $ cookie_history); // add new browsing data Uksort ($ new_history, "my_sort"); // Sort by browsing time $ History = array_unique ($ new_history ); If (count ($ history)> 4 ){ $ History = array_slice ($ history, 0, 4 ); } Cookie ('History ', $ history ); } } /** + ---------------------------------------------------------- * Reading web browsing records + ---------------------------------------------------------- */ Function cookie_history_read (){ $ Arr = cookie ('History '); Foreach (array) $ arr as $ k => $ v ){ $ List [$ k] = json_decode ($ v, true ); } Return $ list; } |
3. Output information on the page for displaying browsing records
The code is as follows: |
Copy code |
$ This-> assign ('History ', cookie_history_read ()); |
In the template, use volist to display it.