PHP saves browsing history web pages to cookies

Source: Internet
Author: User
Tags php foreach
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.