目前,我有一个搜索栏,它将用户的搜索查询保存在cookie中。为此,我将用户的所有输入保存在一个数组中。现在的问题是,如果用户再次键入相同的内容,它将被复制,并将复制的值保存在数组中。我希望有一种方法,使复制的值不会添加到已设置的cookie中:
$input_search = json_decode(($this->input->post('keyword_search')));
if(isset($input_search))
foreach($input_search as $searchvals){
$searchvals->name = $searchvals->value;
unset($searchvals->value);
$searchval = json_encode($searchvals);
if (!isset($_COOKIE['lpsearch_history'])){
setcookie('lpsearch_history',$searchval,time() +3600 *365,'/');
}else {
$locations[] = json_decode($_COOKIE['lpsearch_history'], true);
$arrsearchval = json_decode($searchval, true);
if(!in_array($arrsearchval, $locations))
$locations[] = $arrsearchval;
$areas = json_encode($locations);
setcookie('lpsearch_history',$areas,time() +3600 *365,'/');
}
}
现在,它会给出如下输出:
[[[[{"type":"community","devslug":"downtown-dubai","name":"Downtown Dubai"}],
{"type":"community","devslug":"downtown-dubai","name":"Downtown Dubai"}],
{"type":"community","devslug":"palm-jumeirah","name":"Palm Jumeirah"}],
{"type":"community","devslug":"palm-jumeirah","name":"Palm Jumeirah"}]
1条答案
按热度按时间ia2d9nvy1#
要防止cookie被复制,您需要匹配Cookie名称及其内容
现在,如果您的cookie内容来自“动态”源,如
user input
或rand()
函数,您可以将cookie内容存储在$_SESSION
中,并使用它来验证cookie是否存在