Home >Backend Development >PHP Tutorial >How to browse history in php_PHP tutorial

How to browse history in php_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-13 10:04:131093browse

How to browse history in php

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:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

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

/**

* 商品历史浏览记录

* $data 商品记录信息

*/

private function _history($data)

{

if(!$data || !is_array($data))

{

return false;

}

//判断cookie类里面是否有浏览记录

if($this->_request->getCookie('history'))

{

$history = unserialize($this->_request->getCookie('history'));

array_unshift($history, $data); //在浏览记录顶部加入

/* 去除重复记录 */

$rows = array();

foreach ($history as $v)

{

if(in_array($v, $rows))

{

continue;

}

$rows[] = $v;

}

/* 如果记录数量多余5则去除 */

while (count($rows) > 5)

{

array_pop($rows); //弹出

}

setcookie('history',serialize($rows),time()+3600*24*30,'/');

}

else

{

$history = serialize(array($data));

setcookie('history',$history,time()+3600*24*30,'/');

}

}

1 2

3

4 5

67 8 9 10 11 12
13
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,'/'); } }
I hope this article will be helpful to everyone’s PHP programming design. http://www.bkjia.com/PHPjc/966368.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/966368.htmlTechArticleHow to browse history in php This article mainly introduces the method of browsing history in php, involving the use of cookies by php Skills, very practical value, friends in need can refer to this article...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn