PHP and Cookies; a good mix!
(Page 5 out of 5)Cookies are really versatile, and can be used for a lot of different purposes. Many websites use cookies, and cookies can really make your website more personalized. Using cookies in PHP isn't hard at all, and you should be able to use them without any difficulty.
Before actively using cookies in your website, you must check whether the visitor has enabled them in their browser. If they don't have cookies enabled, you must either redirect to a non-cookies version of your website, or you can make sure your website also works without cookies.
You can download a sample script at http://phpit.net/demo/php%20and%20cookies/logger.zip, where cookies are used in a (somewhat) practical way. In this example, there is a logging module, called log.php and a display module, called history.php. Basically, you include the log.php in other PHP pages, and then you can view history.php to lookup all the pages you have viewed and how often. The example uses arrays, and stores them in cookies.
The examples in this article can be downloaded at http://phpit.net/demo/php%20and%20cookies/examples.zip.
If you have a really unique practical way of using cookies, please let me know at dennis [AT] nocertainty [DOT] com. I'd really like to hear about interesting ways of using cookies.
February 16th, 2006 at 3:09 pm
This site is very helpful to me.
I expect more examples about session and cookies from this site.
Thanks a lot.