dongxing8009 2013-11-17 01:43
浏览 30
已采纳

我的饼干行为不端。

Warning: Reading this may make you smile and/or laugh or/at very least chuckle. Mild humor injected for your benefit. Making you smile is my way of showing gratitude for your time.

The cookie is being set successfully, or is it?

setcookie("REF", $referer, strtotime( '+30 days' ) );
print_r($_COOKIE);

When coming from a referring site, I get [REF] => site, which is working properly. Next, I open a new tab and go to the URL directly. Again, I get [REF] => site - great it appears to be working!

But wait, don't get too excited. That "appearance" of it working is apparently misleading. It's an illusion; deja-vu of that damned kool-aid machine in the dessert --- all over again. Anyways, I hit f5 and refresh the page and now the madness begins.

Guess what happens? Did you guess? Here, I'll just tell you; the cookie is no longer set. Array() is empty. What the frick?

Tested in Chrome and Firefox and the result is the same in both.

Question:
How do I make the cookie persist until expired? Why/How does refreshing the page break the cookie? Bonus: Is it possible for a cookie set in Chrome to automatically be set in Firefox? (I'm assuming, NO way! But sometimes I like to think ANYTHING is possible)

  • 写回答

1条回答 默认 最新

  • dtot74529 2013-11-17 02:04
    关注

    Since no one posted an answer. I will do it for the benefit of the community.

    For the cookie to not be reset on each subsequent page load, you must use an if statement to check if a cookie already exists before giving out a new one. (I guess we don't want to make people/browsers fat, or we're just stingy with the cookies).

    The working code is:

    If ($_COOKIE == null) { 
        setcookie("REF", $referer, strtotime( '+30 days' ) );
    }
    

    Special thanks to @Quentin for pointing that out.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000