This article mainly introduces how PHP can save browsing history web pages to cookies, involving php's reading of cookies.
This article mainly introduces how PHP can save browsing history web pages to cookies, involving php's reading of cookies.
This example describes how PHP can save the web site browsing history to a cookie. 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 :? 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); // The storage 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.