Home >Backend Development >PHP Tutorial >How to browse history in php_PHP tutorial
This article mainly introduces the method of browsing history in php, involving the skills of php operating cookies. It is of great practical value. Friends who need it can For reference
The example in this article describes how to browse history in php. Share it with everyone for your reference. The specific implementation method is as follows:
?
3 4 513 14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
/** * Product browsing history * $data product record information */ private function _history($data) { if(!$data || !is_array($data)) { return false; } //Determine whether there are browsing records in the cookie class if($this->_request->getCookie('history')) { $history = unserialize($this->_request->getCookie('history')); array_unshift($history, $data); //Add to the top of the browsing history /* Remove duplicate records */ $rows = array(); foreach ($history as $v) { if(in_array($v, $rows)) { continue; } $rows[] = $v; } /* Remove if the number of records exceeds 5 */ while (count($rows) > 5) { array_pop($rows); //pop } setcookie('history',serialize($rows),time()+3600*24*30,'/'); } else { $history = serialize(array($data)); setcookie('history',$history,time()+3600*24*30,'/'); } } |