PHP implements the method of saving browsing history page URLs to cookies, phpcookie_PHP tutorial

WBOY
Release: 2016-07-13 10:08:58
Original
846 people have browsed it

PHP implements the method of saving browsing history page URLs to cookies, phpcookie

The example in this article describes how PHP saves the URL of the browsing history page to a cookie. Share it with everyone for your reference. The details are as follows:

Save the browsing history page URL to a cookie. The general idea is as follows, which is slightly different from the actual application.

Copy code The code is as follows:
/*******
Note: Cookies can only save strings. In this example, multiple URLs (historical access records) need to be saved. The idea is to first convert the URL array into a string, then save it, and then read it in a loop
*******/

//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);//Convert the string back to the original array
$arr[] = $_SERVER['REQUEST_URI'];//The current page url is added to the array
$arr = array_unique($arr);//Remove duplicate
if(count($arr)>10){//Only save 10 access records
array_shift($arr);
}
$urls = serialize($arr);//Stored as string,
setcookie('his',$urls);//Save to cookie
}else{
$url = $_SEVER['REQUEST_URI'];//Get the current page URL
$arr[] = $url;//Save the current URL into the array
$urls = serialize($arr);//Stored as string
setcookie('his',$urls);//Save to cookie
}

echo "Previous page

";//Previous page, used for access testing
echo "Next page";//Next page, access test

?>

History visit page






I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/947928.htmlTechArticlePHP implements the method of saving the browsing history page URL to cookie, phpcookie. This article describes the example of PHP implementing the browsing history page. How to save URL to cookie. Share it with everyone for your reference...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template