如何通过使用cookie来跟踪用户搜索的项目


How to keep track of the user searched items by using cookies

在我的控制器中,我使用此代码将用户搜索的位置置于cookie中。

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

这是只保存一个地方,如果他搜索另一个地方,这将被重写,所以我如何保存他正在搜索的所有地方。

?

谢谢。

你可以这样做:

$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

我不知道这样做是不是像Prava说的那样,我认为这是一个好方法。

$visitingPlace = $this->input->post('place_visiting');
        $timestamp = now() + random_string('numeric', 5);
        $cookie[$timestamp] = array(
            'name' => 'searched_places',
            'value' => $visitingPlace,
            'expire' => '5184000',
        );
$this->input->set_cookie($cookie);