【发布时间】:2019-11-04 17:59:45
【问题描述】:
我正在尝试将当前页面的 URL 添加到名为“$visitedPages”的 cookie 中。作为参考,Wordpress 函数“get_permalink()”返回 URL。
在下面,var_dump 返回“假”。而如果我将第 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), "/");
?>
【问题讨论】:
标签: php wordpress cookies session-cookies