带字符串的 Cookie 数组 returns false
Cookie array with strings returns false
我正在尝试将当前页面的 URL 添加到名为“$visitedPages”的 cookie。作为参考,Wordpress函数'get_permalink()' returns URL.
在下面,var_dumpreturns'false'。而如果我将第 2 行的 'get_permalink()' 替换为 'get_the_ID()',其中 returns 整数页面 ID,则一切正常。
我尝试从 url 中删除特殊字符,但它仍然 returns '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,并将其添加到固定链接函数中。我假设当您说您使用 get_the_ID() 时,这意味着您正在用获取 ID 函数替换永久链接函数。尝试串联使用它们。
$page_ID = get_the_ID();
$currentPage = get_the_permalink($page_ID);
我需要在 json_decode 之前使用 stripslashes() 从 cookie 中删除转义引号。为什么 json_decode 自己不这样做,我不知道。
这是工作代码。注意:最好使用完全相同的代码,但使用 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), "/");
我正在尝试将当前页面的 URL 添加到名为“$visitedPages”的 cookie。作为参考,Wordpress函数'get_permalink()' returns URL.
在下面,var_dumpreturns'false'。而如果我将第 2 行的 'get_permalink()' 替换为 'get_the_ID()',其中 returns 整数页面 ID,则一切正常。
我尝试从 url 中删除特殊字符,但它仍然 returns '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,并将其添加到固定链接函数中。我假设当您说您使用 get_the_ID() 时,这意味着您正在用获取 ID 函数替换永久链接函数。尝试串联使用它们。
$page_ID = get_the_ID();
$currentPage = get_the_permalink($page_ID);
我需要在 json_decode 之前使用 stripslashes() 从 cookie 中删除转义引号。为什么 json_decode 自己不这样做,我不知道。
这是工作代码。注意:最好使用完全相同的代码,但使用 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), "/");