This article mainly introduces how PHP saves browsing history web pages to cookies. it involves php's skills in reading cookies, converting strings, and saving cookies, for more information, see the following example. Share it with you for your reference. The details are as follows:
Save the browsing history page URL to the cookie. the general idea is as follows, which is different from the actual application.
The code is as follows:
<? Php
/*******
Note: cookies can only store strings in this instance. you need to save multiple URLs (historical access records). The idea is to convert the URL array into a string and then save the cookie. when reading the string, read the string again.
*******/
// Assume that the current URL is http: // localhost/php /? Id = 1
$ Id = $ _ GET ['id'];
If (isset ($ _ COOKIE ['his ']) {
$ Urls = $ _ COOKIE ['his ']; // read cookie
$ Arr = unserialize ($ urls); // returns the string to the original array.
$ Arr [] = $ _ SERVER ['request _ URI ']; // add the current page url to the array.
$ Arr = array_unique ($ arr); // remove duplicate
If (count ($ arr)> 10) {// only 10 access records are saved
Array_shift ($ arr );
}
$ Urls = serialize ($ arr); // save as a string,
Setcookie ('his ', $ urls); // save to cookie
} Else {
$ Url = $ _ SEVER ['request _ URI ']; // Obtain the URL of the current page
$ Arr [] = $ url; // save the current URL to the array
$ Urls = serialize ($ arr); // save as a string
Setcookie ('his ', $ urls); // save to cookie
}
Echo "previous page
"; // Previous Page for access testing
Echo "next page"; // next page for access testing
?>
Historical access page
<? Php foreach ($ arr as $ v) {?>
- "> <? Php echo $ v;?>
<? Php }?>
I hope this article will help you with php programming.