我正在尝试将用户搜索存储在 cookie 中以向用户显示“最近搜索”。该表单包含 2 个参数:
- 关键词
- 地点
我可以创建 cookie 没问题,但我不能更新它并存储超过 1 个搜索。Indeed.co.uk 他们将搜索存储在 Cookie 中,格式如下:
"q=carpenter&l=london&ts=1506177856392:q=bricklayer&l=Southwark&ts=1506008036065:q=labourer&l=Southwark&ts=1506007988029"
如您所见,Indeed 存储多个 UNIQUE 搜索。
我正在使用 PHP 有人知道如何复制 Indeed 存储用户表单搜索的方式吗?我需要将查询作为数组。
谢谢
约翰
更新 - 代码尝试:
if (isset($_COOKIE["RS"])) {
// Get cookie value
$new = "key=$keyword&loc=$locationKey:";
//"q=carpenter&l=london&ts=1506177856392:q=bricklayer&l=Southwark&ts=1506008036065:q=labourer&l=Southwark&ts=1506007988029"
$prev_value = $_COOKIE["RS"];
$prev_value = stripslashes($prev_value);
$prev_value = json_decode($prev_value, true);
// Add current value to array and set cookie again
$prev_value = $prev_value."".$new;
$new_value = json_encode($prev_value, true);
setcookie('RS', $new_value, time()+3600);
}else {
// Set cookie to current value
$init_value = "key=$keyword&loc=$locationKey:";
$init_value = json_encode($init_value, true);
setcookie('RS', $init_value, time()+3600);
}