Cookie Monster

I have to agree with Blake Ross’ opinion that cookies are delicious delicacies. I eat, I mean use them as often as possible. I must admit that at first, I was afraid to touch the things, but now I can’t get enough of them. Most movable type users recognize the javascript on their individual entry archive template that bakes a cookie whenever a commenter wants to be remembered, but there are so many other great uses for cookies.

The portfolio section of my website is one place I knew that I would be using a cookie. The entire section consists of only 2 dynamic pages (portfolio.php, and portfoliobig.php) that pull all of the content on the page from a couple tables in a MySQL database. The default destination for the Folio link on my main navigation bar is the portfolio.php page displaying content from the latest item in my webdesign category. When you click on one of the icons at the bottom, the id for that item is passed through the query string to the portfoliobig.php page. When you click on one of the categories on the right side navigation though, you are sent to a page that uses the header() function in php to direct you back to the portfolio.php page while setting a cookie that lets the page know what category you would like to see. If that sounds too complicated, check out what it looks like:

setcookie("folioCat", 5, time()+60*60*24*30, "/", "" );

All I have to do is throw the setcookie function out there and put in the name of the cookie, folioCat; the value of the cookie, which is the id of the category I want to pull; the amount of time till it expires, which is the current time plus the number of seconds in a minute times the number of minutes in an hour times the number of hours in a day times 30 to make the cookie last 30 days; the directory the cookie will work for on the site, in this case it’s root (/); and the site which the cookie will work on,

Now that we have a cookie, the next line tells the browser to go to the portfolio.php page (it’s always a good idea to use the full URL here), and then we exit. The true beauty of the cookie is that if a visitor leaves the portfolio or closes the window, they will get the same category they were looking at when they return. See!? CoOoKiEs GoOD!!!

For my next cookie trick: I have a php include called linkrel which is what I am using to add the stylesheets to my site. As soon as I can whip up another fancy style for my site, I’m going to give you all a choice of which style you would like to greet you when you visit. I know, I know…it’s been done, but don’t spoil my fun.

One comment on “Cookie Monster

Birdman says:

Leave a Reply

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

Back to the top!