Remember an Action using Browser Cookies

So if you want to remember a user’s action, all you need are cookies.

Cookies, as defined by Wikipedia, is also known as web cookie, browser cookie, and HTTP cookie. It is a piece of text stored on a user’s computer by their web browser. A cookie can be used for authentication, storing site preferences, shopping cart contents, the identifier for a server-based session, or anything else that can be accomplished through storing text data.

Let’s focus on the server side for now.

//to signify that the user DID an action, we set a cookie with a numerical value of true
setcookie('user_did_an_action', 1);

//to verify that the user DID do an action, we check for it's existence
if (isset($_COOKIE['user_did_an_action'])) {
     //do something

see php manual for the usage of the setcookie method

basically, we call setcookie when an action is done, we then do a check if the cookie exists when we want to do something.

this code is useful for “Remember me” checkboxes. I hope this helped you a bit 🙂

Leave a Reply