Php 带字符串的Cookie数组返回false

Php 带字符串的Cookie数组返回false,php,wordpress,cookies,session-cookies,Php,Wordpress,Cookies,Session Cookies,我正在尝试将当前页面的URL添加到名为“$visitedPages”的cookie中。作为参考,Wordpress函数“get_permalink()”返回URL 在下面的示例中,var_dump返回“false”。然而,如果我将第2行的“get_permalink()”替换为“get_the_ID()”,它返回整数页面ID,这一切都可以正常工作 我尝试从url中剥离特殊字符,但它仍然返回“false”,因此我怀疑这个问题与解码cookie中的字符串有关 // define the new va

我正在尝试将当前页面的URL添加到名为“$visitedPages”的cookie中。作为参考,Wordpress函数“get_permalink()”返回URL

在下面的示例中,var_dump返回“false”。然而,如果我将第2行的“get_permalink()”替换为“get_the_ID()”,它返回整数页面ID,这一切都可以正常工作

我尝试从url中剥离特殊字符,但它仍然返回“false”,因此我怀疑这个问题与解码cookie中的字符串有关

// define the new value to add to the cookie
$currentPage = get_the_permalink(get_the_ID());

// if the cookie exists, read it and unserialize it. If not, create a blank array
if(isset($_COOKIE['visitedPages'])) {
    $visitedPagesSerialised = $_COOKIE['visitedPages'];
    $visitedPages = unserialize($visitedPagesSerialised)
    var_dump($visitedPages);
} else {
    $visitedPages = array();
}

// add the current page id to the array and serialize
$visitedPages[] = $currentPage;
$newCookieSerialized = serialize($visitedPages);

// save the cookie for 30 days
setcookie('visitedPages', $newCookieSerialized, time() + (86400 * 30), "/");
?>

尝试获取ID,并将其添加到permalink函数中。我假设当你说你使用了get_the_ID()时,意味着你正在用get ID函数替换permalink函数。试着把它们串联起来使用

$page_ID = get_the_ID();
$currentPage = get_the_permalink($page_ID);

尝试获取ID,并将其添加到permalink函数中。我假设当你说你使用了get_the_ID()时,意味着你正在用get ID函数替换permalink函数。试着把它们串联起来使用

$page_ID = get_the_ID();
$currentPage = get_the_permalink($page_ID);

在json_解码之前,我需要使用stripslashes()从cookie中删除转义引号。我不知道为什么json_解码本身不这么做

这是工作代码。注意:最好使用完全相同的代码,但使用json_encode()和json_decode()而不是serialize()和unserialize(),因此我也对其进行了更改,但原理是相同的

// define the new value to add to the cookie
$currentPage = get_the_permalink(get_the_ID());

// if the cookie exists, read it and unserialize it. If not, create a blank array
if(isset($_COOKIE['visitedPages'])) {
    $visitedPagesSerialised = stripslashes($_COOKIE['visitedPages']);
    $visitedPages = json_decode($visitedPagesSerialised)
    var_dump($visitedPages);
} else {
    $visitedPages = array();
}

// add the current page id to the array and serialize
$visitedPages[] = $currentPage;
$newCookieSerialized = json_encode($visitedPages);

// save the cookie for 30 days
setcookie('visitedPages', $newCookieSerialized, time() + (86400 * 30), "/");

在json_解码之前,我需要使用stripslashes()从cookie中删除转义引号。我不知道为什么json_解码本身不这么做

这是工作代码。注意:最好使用完全相同的代码,但使用json_encode()和json_decode()而不是serialize()和unserialize(),因此我也对其进行了更改,但原理是相同的

// define the new value to add to the cookie
$currentPage = get_the_permalink(get_the_ID());

// if the cookie exists, read it and unserialize it. If not, create a blank array
if(isset($_COOKIE['visitedPages'])) {
    $visitedPagesSerialised = stripslashes($_COOKIE['visitedPages']);
    $visitedPages = json_decode($visitedPagesSerialised)
    var_dump($visitedPages);
} else {
    $visitedPages = array();
}

// add the current page id to the array and serialize
$visitedPages[] = $currentPage;
$newCookieSerialized = json_encode($visitedPages);

// save the cookie for 30 days
setcookie('visitedPages', $newCookieSerialized, time() + (86400 * 30), "/");

对不起,那是个打字错误。我现在修改了。permalink正在恢复正常-这里的问题是cookie数组。所以我理解。您实际上是想通过检查cookie来查看站点上的页面是否已被访问,并查看cookie中是否存在URL。然后,如果该页面未被访问,则添加/设置cookie。是吗?没错!如果cookie存储的是页面ID(整数),而不是页面URL(字符串),即使我去掉了符号,代码也可以工作。那么为什么不只使用页面ID呢?似乎它完成了同样的事情,并且有效:)我需要能够检测查询字符串。无法仅从页面ID执行此操作:(抱歉,这是一个输入错误。我现在已经修改。永久链接返回正常-这里的问题是cookie数组。我明白了。您实际上是想通过检查cookie来查看您网站上的某个页面是否已被访问,并查看cookie中是否存在URL。如果该页面未被访问,则添加/设置cookie。是这样吗对吗?没错!如果cookie存储页面ID(整数),而不是页面URL(字符串),即使我去掉了符号,代码也会工作。那么为什么不使用页面ID呢?似乎它完成了同样的事情,并且工作:)我需要能够检测查询字符串。不能仅从页面ID执行此操作:(