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.

1
2
3
4
5
6
7
//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 :)

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">