In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to achieve browsing records in php". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The method of php to realize the browsing record is as follows: 1, setting the initial data; 2, obtaining the cookie record; 3, judging whether the browsing record exists; 4. Writing the browsing array to cookie;5 and reading the cookie record after serialization.
This article operating environment: Windows7 system, PHP7.1 version, DELL G3 computer
How does php implement browsing records?
Php implementation history browsing
In fact, the principle is very simple, which is to use cookie to implement records, and the point you need to pay attention to is to set the cookie length and record time you need to save. Here is the basic implementation of the ci framework.
If there are better ideas to achieve, you are welcome to comment and discuss.
/ * * @ desc set up cookie browsing history * @ date 2018-04-15 16:48:22 * @ param [string $type record browsing type [as view record Table 1 and PS view record Table 2] Int $id primary key id] * @ author 1245049149@qq.com * @ return [type] * / public function set_cookie_history ($type,$id) {/ / set initial data $set_limit = 5; / / capacity limit for browsing records / / initial data filtering if (! in_array ($type, ['as','ps'])) {return false } / / get cookie record $string = $type.$id; $history_array = unserialize ($_ COOKIE ['cookie_history']); if (! $history_array) $history_array = []; / / browsing record exists if (in_array ($string,$history_array)) {unset ($history_ array [array _ search ($string,$history_array)]) / / delete the existence of array_unshift ($history_array,$string); / / replace it in the first / / browse record does not exist} else {/ / does not exceed the record capacity limit, directly put it in the first if (count ($history_array) config- > item ('cookie_domain'); $history_array = serialize ($history_array) Setcookie ('cookie_history', $history_array, time () + $expire_time,' /', $cookie_domain);}
The above is the recording function of cookie, and the following is the method of reading cookie records:
/ * @ desc get cookie browsing record * @ date 2018-04-15 17:42:51 * @ param [type] * @ author 1245049149@qq.com * @ return [array $return_data] * / public function get_cookie_history () {/ / set initial return data $return_data = []; / / get cookie record $history_array = unserialize ($_ COOKIE ['cookie_history']) If (! $history_array) return $return_data; if ($history_array) {foreach ($history_array as $k = > $v) {/ / cut to determine whether it is as type $as_temp = explode ('as',$v) If ($as_temp & & $as_temp [1]) {/ / write here the sql statement you want to query $sql = "select field1,field2 from table_test1 where id= {$as_temp [1]}"; $res = $this- > db- > query ($sql)-> row_array () If ($res) $return_data [] = ['type' = >' as','data' = > $res];} / / cut to determine whether it is ps type $ps_temp = explode ('ps',$v) If ($ps_temp & & $ps_temp [1]) {/ / write here the sql statement you want to query $sql = "select field1,field2 from table_test2 where id= {$as_temp [1]}"; $res = $this- > db- > query ($sql)-> row_array () If ($res) $return_data [] = ['type' = >' ps','data' = > $res];}} return $return_data;} / / get data illegally and return directly to return $return_data;} "how to achieve browsing history by php". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.