Detailed description of viewing records using cookies in php

Source: Internet
Author: User
When we are working on a project, some modules are usually the browsing record list. but how can we implement the browsing record function in PHP, now we use cookies to implement the browsing record list in some modules when we are doing projects. but how can we implement the browsing record function in PHP, now we use cookies to implement


Let's analyze the ideas first.

Access page

Obtain the product data list in the storage browsing record

If the currently browsed item is not included in the history, it is added. If yes, it is updated.

If you know the idea, you can directly view the code.


The complete code is as follows:

 5) {// bring up the last unit of the array until its length is less than or equal to 5. array_pop ($ history) ;}// concatenate the array with commas (,) into a string to write the COOKIE, and set the expiration time to 30 days setcookie ('shop [history] ', implode (', ', $ history), $ cur_time + 3600*24*30 );} else {// if the COOKIE is empty, the ID of the currently browsed item is written into the COOKIE, which occurs only when the website is browsed for the first time: setcookie ('shop [history] ', $ id, $ cur_time + 3600*24*30);} // the IDs of the browsed items are recorded in the COOKIE, the following describes how to use the data in the COOKIE to obtain the data in the COOKIE in the format of 1, 2, 3, and 4. of course, it can also be 0 $ history = isset ($ _ COOK IE ['shop'] ['History '])? $ _ COOKIE ['shop '] ['History']: 0; // write an SQL statement, use IN to query the product list of these IDs $ SQL _history = "SELECT * FROM 'goods' WHERE 'goods _ id' in ({$ history })"; // execute the SQL statement and return the data list $ goods_history = $ db-> getAll ($ SQL _history); if ($ goods_history) {$ tpl-> assign ('goods _ history ', $ goods_history) ;}?>

The following mysql operations that obtain product information based on the storage ID can be obtained based on your own code, you can use the original ecology, or you can use the database query method of the framework, you can modify it as appropriate

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.