Cookies

Cookies allow you to store session specifif information client side. This includes encrypted passwords, shopping trolly content, user names etc.

Cookies are controlled by a visitors web browser. Users can disable cookie support and also remove cookies when ever the wish. Most browsers also support a private browser mode (like Google’s incognito mode, which removes all cookies once the browser window is closed.

Setting the lifespan of a cookie

Cookies can be configured to have a set lifespan, and will get automatically removed once the lifespan is reached.

/* This cookie will last a year */
put cookie "yearCookie" for "/" with "This cookie will last 1 year" until (the seconds + 60 * 60 * 24 * 365)
/* This cookie will last until the browser is closed, this isthe default expiry time for a cookie */
put cookie "sessionCookie" for "/" with "This cookie will be removed when you close the browser window"

Retrieving Cookies

Cookies can be retrieved using the $_COOKIE keyword. $_COOKIE stores all your cookiev data in an array, just like $_POST and $_GET. You can access the cookies for the 2 examples like this:

put $_COOKIE["yearCookie"]
// and
put $_COOKIE["sessionCookie"]

Leave a Reply