如何通过使用Cookie跟踪用户搜索的项目 [英] How to keep track of the user searched items by using cookies

查看:92
本文介绍了如何通过使用Cookie跟踪用户搜索的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的控制器中,我使用此代码将用户搜索的位置转换为Cookie。

In my controller I am using this code to take user searched place in to a cookie.

$visitingPlace = $this->input->post('place_visiting');
        $cookie = array(
            'name' => 'searched_places',
            'value' => $visitingPlace,
            'expire' => '5184000',
        );
        $this->input->set_cookie($cookie);

这只是保存一个地方,如果他搜索另一个地方,这将被覆盖,我保存所有他正在寻找的地方。 ?

this is only save one place if he searched for another place this is going to be override so how can I save all the places he is searching for. ?

谢谢。

推荐答案

您可以这样做:

$visitingPlace = $this->input->post('place_visiting');
$places         = unserialize( $this->input->cookie('searched_places') );   //all the searched cookies here
$places[]       = $visitingPlace;   //set the newly searched in the array
$cookie = array(
    'name' => 'searched_places',
    'value' => serialize( $places ),
    'expire' => '5184000',
);
$this->input->set_cookie($cookie);

首先检索所有cookie值,取消序列化,将新搜索放入数组,然后设置cookie。

First retrieve all the cookie values, unserialize them, place the new search in the array and then set the cookie again.

这篇关于如何通过使用Cookie跟踪用户搜索的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆