In the process of working on a little side project (which shall be revealed shortly!) I found a need to set a cookie. I don’t think I’ve ever had to do it before, but it seemed simple enough. Until it didn’t work. Off to Google! There I found this tip from Scratch99.com on properly setting the site path, which doesn’t appear to work with WordPress 3.0.4 (or I may just be an idiot). I was a bit disappointed, but it gave me a good excuse to go spelunking through the core.
As always, this yields wonderful, fun things that make my life easier. WordPress has a constant that defines the site path, specifically for cookies. Ironically, this is named SITECOOKIEPATH.
Without more blabbering, here’s the code. Enjoy. =)
setcookie("your-cookie-name", 1, time()+3600, SITECOOKIEPATH);
I’m using this to see if an ad has already been displayed to a user in the recent past. If you’re planning something similar, here’s how it will work out.
if(!isset($_COOKIE['your-cookie-name'])) {
// do some PHP stuff here
setcookie("your-cookie-name", 1, time()+3600, SITECOOKIEPATH);
}
Here’s the PHP.net info on setting cookies, if you’re unfamiliar with the process or just enjoy reading technical documentation.
Feedback