在 PHP 中输出 getcookie 变量而无需刷新浏览器
我使用以下代码输出没有 cookie 的内容块,如果已设置 cookie,则输出另一个数字。问题是 getcookie
变量在页面刷新或用户导航到下一页之前不起作用。
我很高兴使用标头重定向,但不确定将其放在这段代码中的何处(除非有人对代码本身有更好的解决方案):
if (is_page(817)) {
setcookie("phonecookie", 1, time()+3600, COOKIEPATH, COOKIE_DOMAIN);
}
if ($_COOKIE["phonecookie"] =="") {
echo "no cookie here";
} else {
echo "cookie stored!";
}
此外,如果访问者登陆 WordPress 中的特定页面,上面的代码会设置 cookie。
是否有另一种方法可以通过查询字符串来完成此操作,例如 example.com/?src=affiliate
I am using the following code to output a block of content without a cookie, and another number if the cookie has been set. Problem is that the getcookie
variable doesn't work until the page has been refreshed, or the user navigates to the next page.
I am happy to use the header redirect but not sure where to put it within this code (unless someone has a better solution to the code itself):
if (is_page(817)) {
setcookie("phonecookie", 1, time()+3600, COOKIEPATH, COOKIE_DOMAIN);
}
if ($_COOKIE["phonecookie"] =="") {
echo "no cookie here";
} else {
echo "cookie stored!";
}
Also, the code above sets the cookie if the visitor lands on a specific page within WordPress.
Is there another way to do it via query string e.g. example.com/?src=affiliate
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个
Try this
使用标头重定向回同一页面:
redirect=1
已添加到 URL,以在用户禁用 Cookie 时停止无限循环。Using the header to redirect back to the same page:
redirect=1
has been added to the url to stop an infinite loop if the user has cookies disabled.