Continued from page 1
<?php setcookie ('name', FALSE, time()-1000); ?>
Checking if cookies are enabled Before you start using cookies, you must make sure your visitor has cookies enabled. This can be done with a simply PHP checking script. Unfortunately,
PHP page needs to reload to check for cookies. But this can be done very transparently, and your visitor should hardly notice anything.
The following example will first set a test cookie, then reload
page, and finally check whether cookies are enabled.
<?php error_reporting (E_ALL ^ E_WARNING ^ E_NOTICE);
// Check if cookie has been set or not if ($_GET['set'] != 'yes') { // Set cookie setcookie ('test', 'test', time() + 60);
// Reload page header ("Location: checkcookies.php?set=yes"); } else { // Check if cookie exists if (!empty($_COOKIE['test'])) { echo "Cookies are enabled on your browser"; } else { echo "Cookies are <b>NOT</b> enabled on your browser"; } } ?>
Run
code above, and see what
output is. Check if cookies are enabled in your browser. If they're not enabled, then you can enable them by going to your browser's options. Unfortunately, this is different from each browser, so I can't give you exact instructions. But Google can.
Storing Arrays One feature of cookies that is often missed in articles is
ability to story arrays. Cookies can be used to store multi-dimensional arrays, which can be extremely useful to store data.
Consider
following code;
<?php setcookie ("name[first]", "Dennis", time() + (60*60*24)); setcookie ("name[last]", "Pallett", time() + (60*60*24)); ?>
You can then display these two cookies using
following code:
<?php echo "First Name: " . $_COOKIE['name']['first']; echo "<br />Last Name: " . $_COOKIE['name']['last']; ?>
The cookie 'name' is an array, and has multiple values. You can even go deeper and have multi-dimensional arrays, e.g. $_COOKIE['name']['test']['something']['value']. You could store whole arrays of data in cookies. But beware that you don't store too much data, there are certain size limits to cookies.
In Conclusion... 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
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://www.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
log.php in other PHP pages, and then you can view history.php to lookup all
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://www.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.

Dennis Pallett is a young tech writer, with much experience in ASP, PHP and other web technologies. He enjoys writing, and has written several articles and tutorials. To find more of his work, look at his websites at http://www.phpit.net, http://www.aspit.net and http://www.ezfaqs.com