【发布时间】:2018-08-19 09:26:49
【问题描述】:
我正在尝试使用 Genesis 主题在 Wordpress 中设置 cookie。我将代码放在 functions.php 文件的底部并调用“isset”来检查它是从页面模板中设置的。
add_action( 'init', 'my_setcookie' );
function my_setcookie() {
$cookie_name = "touchscreen";
$cookie_value = "touchscreen_1";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
}
通过页面模板检查:
if(!isset($_COOKIE[$touchscreen])) {
echo "My Cookie '" . $touchscreen . "' is not set!";
} else {
echo "My Cookie '" . $touchscreen . "' is set!<br>";
echo "Value is: " . $_COOKIE[$touchscreen];
}
浏览器输出是My Cookie '' is not set!
不确定是我使用的钩子还是 Genesis 本身哪里出错了?我在这里看到了另外几篇文章,但似乎没有任何效果。
更新,正在设置 cookie 我可以在 Firefox 下看到它,Storage / Cookies...所以问题似乎是在访问它。
【问题讨论】: