Php 在cookie中保存查询字符串

Php 在cookie中保存查询字符串,php,Php,在每次请求我的页面时,我都希望将查询字符串值存储在cookie中。我的意思是当: $_GET['id'] = 28 to get 28. 下次: $_GET['id'] is 36. 我想得到36,再加上28。我怎样才能做到这一点 $value=$_COOKIE['TestCookie']; setcookie("TestCookie", $value+$_GET['value'], time()+(10 * 365 * 24 * 60 * 60)); 这会解决你的问题。我将cooki

在每次请求我的页面时,我都希望将查询字符串值存储在cookie中。我的意思是当:

$_GET['id'] = 28 to get 28. 
下次:

$_GET['id'] is 36. 
我想得到36,再加上28。我怎样才能做到这一点

$value=$_COOKIE['TestCookie'];
setcookie("TestCookie", $value+$_GET['value'], time()+(10 * 365 * 24 * 60 * 60));

这会解决你的问题。我将cookie的过期时间设置为非常长,您可以修改它。

您可以使用您提到的cookie,但根据工作流程,您可能希望使用
$\u SESSION
,因为它通常更安全。客户端可以修改cookie,并在访问之间保持。如果这是你想要的行为,那么这将是你最好的选择

<?php
// The intval() function helps to ensure that numbers are being used, floatval() could work too
// Get current cookie value
$currentValue = intval($_COOKIE['CookieCounter']);
// Get new passed value
$incrementBy = intval($_GET['id']);
// Set the cookie and have it saved for 30 days
setcookie('CookieCounter', str($currentValue + $incrementBy), time()+60*60*24*30); 

您看过
setcookie
函数了吗?谢谢你的回答。我要试一试。我想让用户最后查看帖子/项目。我该不该用饼干取决于你的意图。如果要在很长一段时间内跟踪帖子总数,那么我会使用我答案的最后一部分并将其存储在数据库中(因为你提到了“帖子”,所以你可能有一个)。谢谢你的回答。
<?php
// Start the session
session_start();
// Check if the session value is already set
if (!isset($_SESSION['count'])) {
  $_SESSION['count'] = intval($_GET['id']);
} else {
// Increment the session value
  $_SESSION['count'] += intval($_GET['id']);
}