我的饼干行为不端。

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)

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.