简体   繁体   English

除非我检查元素,否则不会设置 Cookies

[英]Cookies won't set unless I inspect element

So I am adding a cookie on a wordpress website in header.所以我在 header 的 wordpress 网站上添加了一个 cookie。

setcookie("var", $var, time()+3600, "/", "website.com");

The problem is the cookie is visible and usable in php only if I inspect element -> application -> cookies (in browser).问题是仅当我检查元素 -> 应用程序 -> cookies (在浏览器中)时,cookie 在 php 中可见且可用。 Otherwise I keep refreshing the page and going through the website with no cookie set.否则,我会不断刷新页面并浏览没有设置 cookie 的网站。 I know the cookie shouldn't be visible the first page load but even after the first load there is no cookie.我知道 cookie 在第一页加载时不可见,但即使在第一次加载之后也没有 cookie。

Not sure what's the problem at this point.不知道此时有什么问题。

So I am setting the cookie on a subfolder /sub which is a wordpress file.所以我在子文件夹 /sub 上设置 cookie,这是一个 wordpress 文件。 And I am planning to use it on the index.php There's no issue there since after I inspect it and the cookie shows up everything works as expected.我打算在 index.php 上使用它,因为在我检查它并且 cookie 显示一切都按预期工作之后,那里没有问题。

If it shows up in your dev tools, the cookie was set - period.如果它出现在您的开发工具中,则表明 cookie 已设置 - 句点。

You are actually just setting it, never querying it's value in your code.您实际上只是在设置它,而不是在代码中查询它的值。 So how would you know if the cookie was set?那么你怎么知道 cookie 是否被设置呢?

Just try var_dump($_COOKIE) - it should show the var cookie after you load the page a second time after the cookie has been set.只需尝试var_dump($_COOKIE) - 在设置 cookie 后第二次加载页面后,它应该会显示var cookie。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM