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

Source: Internet
Author: User
Tags php foreach string back

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


This article explains how PHP implements a way to save a browsing history page URL to a cookie. Share to everyone for your reference. Specific as follows:

Save the Browsing history page URL to a cookie, with a general idea like the following code, which is somewhat different from the actual application.
Copy the Code code as follows: <?php
/*******
Note: The cookie can only save the string in this instance, need to save multiple URLs (historical access Records), the idea is to first convert the URL array into a string, and then save, read, and then loop read
*******/

First 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);//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) {//Save only 10 Access records
Array_shift ($arr);
}
$urls = serialize ($arr);//stored as a string,
Setcookie (' his ', $urls);//Save to Cookie
}else{
$url = $_sever[' Request_uri '];//gets the current page URL
$arr [] = $url;//Save the current URL to an array
$urls = serialize ($arr);//stored As String
Setcookie (' his ', $urls);//Save to Cookie
}

echo "Previous page

";//previous page, Access test
echo "Next page";//Next page, Access test

?>

History visit page



      <?php foreach ($arr as $v) {?>
    • "><?php Echo $v;?>

    • <?php}?>

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/947928.html www.bkjia.com true http://www.bkjia.com/PHPjc/947928.html techarticle PHP Implementation of the browsing history page URLs saved to the cookie method, Phpcookie This article describes the PHP implementation of the browsing history page URLs saved to the cookie method. Share for everyone to join us ...

  • 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.